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);
- // 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
().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();
- }
I guess I am missing something somewhere else. please help.
Leon DPosted Mar 10, 2019, 8:56 PM
Madan ShekarPosted Mar 8, 2019, 12:03 AM