I have written a code to write to an excel file in OpenXML. this works for only one time for the second time, I am getting an error while opening the file as "We found a problem with some content in 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);
-
- WorkbookPart workbookpart = spreadsheetDocument.WorkbookPart;
-
- WorksheetPart worksheetPart = workbookpart.WorksheetParts.FirstOrDefault();
- Worksheet worksheet = worksheetPart.Worksheet;
- SheetData sheetData = worksheetPart.Worksheet.Elements<SheetData>().First();
- Row row = new Row();
-
- Cell cell1 = new Cell()
- {
- CellReference = SKU_Address,
- DataType = CellValues.String,
- CellValue = new CellValue(SKU_Value)
- };
- row.Append(cell1);
-
- 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();
-
- spreadsheetDocument.Close();
- }
I guess I am missing something somewhere else. please help.