1
Answer

Creating Excel file using open XML

Photo of Mark

Mark

1y
878
1

I have been using the code example here for creating-excel-file-using-openxml this is simple enough code and it does work nicely i can send my data and save it as an .xlsx  file. However when i open the file it is corrupted and needs to be opend as repaired. when i do open it all the data is there the formatting/styles are nissing but all the data is there.

looking at the log file i see this

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<recoveryLog xmlns="------schemas.openxmlformats.org/spreadsheetml/2006/main"><logFileName>error290880_01.xml</logFileName><summary>Errors were detected in file 'D:\M36_Results_22_04_2024 2_46_34 PM.xlsx'</summary><repairedRecords><repairedRecord>Repaired Records: Format from /xl/styles.xml part (Styles)</repairedRecord><repairedRecord>Repaired Records: Cell information from /xl/worksheets/sheet1.xml part</repairedRecord></repairedRecords></recoveryLog>

any help appreciated. i am new to openXML

Answers (1)

1
Photo of Naimish Makwana
132 13.8k 203.1k 1y

I understand that you’re having trouble with an Excel file created using OpenXML. The file appears to be corrupted when opened in Excel, but the data is intact after the file is repaired. The log file indicates issues with the styles and cell information.

This issue might be due to Excel expecting a fully formatted file when using OpenXML1. That means just having a file with a workbook won’t suffice, it has to have the workbook, worksheets, and at least one sheet for it to open properly1.

Another common issue is not creating a new row for each iteration when adding data1. Essentially, you might be creating a single row with multiple rows inside of it1.

Here’s a simplified example of how to create a new row for each iteration:

foreach (var temp in tempList) 
{
    if (cellIdex == 12)     
    {
        cellIdex = 0;
        rowIdex = rowIdex + 1;
        row = new Row { RowIndex = rowIdex };
        sheetData.AppendChild(row);
    }
    cell = CreateTextCell(ColumnLetter(cellIdex), rowIdex, tempToString(), 3);
    row.AppendChild(cell);
    cellIdex = cellIdex + 1;
}

 

The most important part to note is the row = new Row {RowIndex = rowIdex};1.

If you’re still facing issues, you might want to try using the Open XML SDK Validator to check for any errors2.

Thanks