C# 无法将单元格插入到打开的XML文档中
无法将单元格作为OpenXml Excel文档插入到打开的XML文档中。我认为问题在于C# 无法将单元格插入到打开的XML文档中,c#,excel,openxml,openxml-sdk,C#,Excel,Openxml,Openxml Sdk,无法将单元格作为OpenXml Excel文档插入到打开的XML文档中。我认为问题在于单元格.Remove()实际上并没有清除单元格。我不确定,因为文件上说它被修改了 public static void InsertCell(uint rowIndex, int columnIndex, string value, SheetData sheetData, bool appendRow, EnumValue<CellValues> dataType = null, uint? st
单元格.Remove()
实际上并没有清除单元格。我不确定,因为文件上说它被修改了
public static void InsertCell(uint rowIndex, int columnIndex, string value, SheetData sheetData, bool appendRow, EnumValue<CellValues> dataType = null, uint? styleIndex = null)
{
//Row row = null;
// Check if the worksheet contains a row with the specified row index.
var row = sheetData.Elements<Row>().FirstOrDefault(r => r.RowIndex == rowIndex);
if (row == null)
{
row = new Row() { RowIndex = rowIndex };
sheetData.Append(row);
}
// Convert column index to column name for cell reference.
var columnName = GetExcelColumnName(columnIndex);
var cellReference = columnName + rowIndex; // e.g. A1
// Check if the row contains a cell with the specified column name.
var cell = row.Elements<Cell>().FirstOrDefault(c => c.CellReference.Value == cellReference);
//We need to delete the cell if it does exist as the InsertAt function does not delete the existing cell if it has been found
if (cell != null)
{
cell.Remove();
//cell.CellValue.Remove();
}
if (cell == null || cell.CellValue == null)
{
if (dataType == null)
dataType = new EnumValue<CellValues>(CellValues.SharedString);
var newCell = new Cell() { CellReference = cellReference, CellValue = new CellValue(value), DataType = dataType };
if (styleIndex != null)
newCell.StyleIndex = styleIndex;
if (row.ChildElements.Count < columnIndex)
row.AppendChild(newCell);
else
row.InsertAt(newCell, (int)columnIndex);
}
}
public static void InsertCell(uint rowIndex,int columnIndex,string value,SheetData SheetData,bool appendRow,EnumValue dataType=null,uint?styleIndex=null)
{
//行=空;
//检查工作表是否包含具有指定行索引的行。
var row=sheetData.Elements().FirstOrDefault(r=>r.RowIndex==RowIndex);
if(行==null)
{
行=新行(){RowIndex=RowIndex};
sheetData.Append(行);
}
//将列索引转换为单元格引用的列名。
var columnName=GetExcelColumnName(columnIndex);
var cellReference=columnName+rowIndex;//例如A1
//检查该行是否包含具有指定列名的单元格。
var cell=row.Elements().FirstOrDefault(c=>c.CellReference.Value==CellReference);
//如果该单元格确实存在,我们需要删除它,因为如果找到它,InsertAt函数不会删除现有单元格
如果(单元格!=null)
{
单元格。删除();
//cell.CellValue.Remove();
}
if(cell==null | | cell.CellValue==null)
{
if(数据类型==null)
数据类型=新的枚举值(CellValues.SharedString);
var newCell=new Cell(){CellReference=CellReference,CellValue=new CellValue(value),DataType=DataType};
if(styleIndex!=null)
newCell.StyleIndex=StyleIndex;
if(row.ChildElements.Count
仅供参考,这是调用代码
public static string AddToMaterialsRegister(SpreadsheetDocument spreadsheetDocument, com.upvise.client.Query query, JSONObject[] forms)
{
WorkbookPart workbookPart = spreadsheetDocument.WorkbookPart;
WorksheetPart worksheetPart = workbookPart.WorksheetParts.First();
SheetData sheetData = worksheetPart.Worksheet.Elements<SheetData>().First();
Stylesheet styleSheet = workbookPart.WorkbookStylesPart.Stylesheet;
var stringTable = workbookPart.GetPartsOfType<SharedStringTablePart>().FirstOrDefault();
string text = "";
uint currentRow = GetLastUsedRow(sheetData);
for (int i = 0; i < forms.Length; i += 1)
{
long epochDate = forms[i].getLong("date");
epochDate += 10 * 60 * 60 * 1000; //Add 10 hours to get to the QLD timezone
//The date from the form + 1970
epochDate = (epochDate / 1000 / 60 / 60 / 24) + 25569;
JObject customObj = JObject.Parse(forms[i].getString("value"));
string amount = (string)customObj["F4"];
if (!amount.Equals("") && amount != null)
{
switch ((string)customObj["F5"])
{
case "1":
amount += "T";
break;
case "0":
amount += "m3";
break;
}
}
else
amount = "";
// Not important
string tipValue = forms[i].getString("templateid") == "" ? (string)customObj["F18"] : (string)customObj["F19"];
//Grab the photos from the form
var files = query.selectFiles("Forms.forms", forms[i].getString("id") + ":F22");
//int dateIndex = InsertSharedStringItem(epochDate.ToString(), stringTable);
int materialIndex = InsertSharedStringItem((string)customObj["F1"], stringTable);
int amountIndex = InsertSharedStringItem(amount, stringTable);
int deliveredIndex = InsertSharedStringItem((string)customObj["F6"] + " " + (tipValue != null ? tipValue : ""), stringTable);
int certIndex = InsertSharedStringItem(files.Length > 0 ? "Y" : "", stringTable);
int formNameIndex = InsertSharedStringItem(forms[i].getString("name"), stringTable);
InsertCell(currentRow, 1, epochDate.ToString(), sheetData, false, new EnumValue<CellValues>(CellValues.Number), 2);
InsertCell(currentRow, 2, materialIndex.ToString(), sheetData, false, new EnumValue<CellValues>(CellValues.SharedString), 3);
InsertCell(currentRow, 3, amountIndex.ToString(), sheetData, false, new EnumValue<CellValues>(CellValues.SharedString), 3);
InsertCell(currentRow, 4, deliveredIndex.ToString(), sheetData, false, new EnumValue<CellValues>(CellValues.SharedString), 4);
InsertCell(currentRow, 5, certIndex.ToString(), sheetData, false, new EnumValue<CellValues>(CellValues.SharedString), 4);;
InsertCell(currentRow, 6, formNameIndex.ToString(), sheetData, true, new EnumValue<CellValues>(CellValues.SharedString), 4);
currentRow += 1;
}
//spreadsheetDocument.Save();
//spreadsheetDocument.Close();
//worksheetPart.Worksheet.Save();
//workbookPart.Workbook.Save();
return forms.Length + " forms were added";
}
公共静态字符串addToMaterialRegister(电子表格文档电子表格文档,com.upvise.client.Query查询,JSONObject[]表单)
{
WorkbookPart WorkbookPart=电子表格文档.WorkbookPart;
WorksheetPart WorksheetPart=workbookPart.WorksheetParts.First();
SheetData SheetData=worksheetPart.Worksheet.Elements().First();
样式表样式表=workbookPart.WorkbookStylesPart.Stylesheet;
var stringTable=workbookPart.GetPartsOfType().FirstOrDefault();
字符串文本=”;
uint currentRow=GetLastUsedRow(图纸数据);
对于(int i=0;i0?“Y”:“”,stringTable);
int formNameIndex=InsertSharedStringItem(表单[i].getString(“名称”),stringTable);
InsertCell(currentRow,1,epochDate.ToString(),sheetData,false,新枚举值(CellValues.Number),2);
InsertCell(currentRow,2,materialIndex.ToString(),sheetData,false,新枚举值(CellValues.SharedString),3);
InsertCell(currentRow,3,amountIndex.ToString(),sheetData,false,新枚举值(CellValues.SharedString),3);
InsertCell(currentRow,4,deliveredIndex.ToString(),sheetData,false,新枚举值(CellValues.SharedString),4);
InsertCell(currentRow,5,certIndex.ToString(),sheetData,false,新的EnumValue(CellValues.SharedString),4);;
InsertCell(currentRow,6,formNameIndex.ToString(),sheetData,true,新的EnumValue(CellValues.SharedString),4);
currentRow+=1;
}
//spreadsheetDocument.Save();
//电子表格文档。关闭();
//worksheetPart.Worksheet.Save();
//workbookPart.Workbook.Save();
返回表单。长度+“已添加表单”;
}
当我运行代码时,它不会向xlsx添加行
任何帮助都将不胜感激
我注释掉了Row=null代码>因为每次我插入一个新单元格时都会擦除该行