Search code examples
c#.netexcelopenxmlopenxml-sdk

Open XML: We found a problem with some content in 'myfile.xlsx'


I am creating an Excel file using the Open XML SDK.

Worksheet newWs = new Worksheet()
{
    MCAttributes = new MarkupCompatibilityAttributes() { Ignorable = "x14ac" }
};

When I add a SheetViews instance as follows,

SheetViews sheetViews = new SheetViews();
SheetView sheetView = new SheetView();
Selection selection = new Selection() { ActiveCell = "B1" };
sheetView.Append(selection);
sheetViews.Append(sheetView);
newWs.Append(sheetViews);

I get an error as shown below (and also ActiveCell is not working):

We found a problem with some content in 'myfile.xlsx'. Do you want us to try to recover as much as we can? If you trust the source of this workbook, click Yes.


Solution

  • This was issue with ordering of the excel xml elements.

    I had applied sheetviews after applying styles.

    Which openxml didn't like.

    So I get to know about this by using XML SDK productivity tool. Which helped making correct order.