我已经编写了一个代码来写入OpenXML中的excel文件。这仅第二次有效,打开文件时出现错误,因为“我们发现output.xlsx中的某些内容有问题”
public static void WriteToExcel(string fileName, string SKU_Address, string
SKU_Value, string ItemName_Address, string Item_Name_Value)
{
SpreadsheetDocument spreadsheetDocument =
SpreadsheetDocument.Open(fileName, true);
// Add a WorkbookPart to the document.
WorkbookPart workbookpart = spreadsheetDocument.WorkbookPart;
// Add a WorksheetPart to the WorkbookPart.
WorksheetPart worksheetPart =
workbookpart.WorksheetParts.FirstOrDefault();
Worksheet worksheet = worksheetPart.Worksheet;
SheetData sheetData =
worksheetPart.Worksheet.Elements<SheetData>().First();
Row row = new Row();
//intialize a cell and add values to the required cell based on address
Cell cell1 = new Cell() { CellReference = SKU_Address, DataType = CellValues.String, CellValue = new CellValue(SKU_Value) };
row.Append(cell1);
//intialize a cell and add values to the required cell based on address
Cell cell2 = new Cell() { CellReference = ItemName_Address, DataType = CellValues.String, CellValue = new CellValue(Item_Name_Value) };
row.Append(cell2);
sheetData.Append(row);
worksheetPart.Worksheet.Save();
// Close the document.
spreadsheetDocument.Close();
}
我想我在其他地方缺少什么。请帮忙。