Using VS2012, C#, OpenXML 2.5, I created a test utility that adds a page break (Row Break) to a worksheet using (pseudo):
objRB = New RowBreaks();
objRowBreaks.Append(objRB);
objRowBreaks.ManualBreakCount++;
objRowBreaks.Count++;
Works just fine. Then I added an image (manually) in my Excel page footer. Then I run the program that appends the Row Break again, and when I attempt to open the workbook afterwards, I get an error: "We found a problem with some content in 'YourSheet.xlsx'.
Do you want us to try to recover as much as we can? If you trust the source of this workbook, click Yes."
But nothing is recoverable and I end up with an empty workbook.
After some analysis with the Open XML 2.5 Productivity Tool, I discovered that the problem is the positioning of the elements within the workbook's XML. When youmanually add a page break to the same workbook and save it, the worksheet elements are ordered as follows:
<rowBreaks count="1" manualBreakCount="1"><brk id="24" max="16383" man="1" /></rowBreaks><legacyDrawingHF r:id="rId2" />
just before the worksheet element close (</worksheet>) and all is well.
However, when I programmatically add the row/page break (as described above), the XML elements are output with the RowBreaks at the end:
<x:legacyDrawingHF r:id="rId2" /><x:rowBreaks count="1" manualBreakCount="1"><x:brk id="24" max="16383" man="1" /></x:rowBreaks>
This causes the corruption error described above. If I manually edit the XML to reverse the position of the RowBreaks and the LegacyDrawingHF, the workbook opens just fine.
Finally, a question: Does anyone know how to programmatically make sure that the RowBreaks element gets positioned before the LegacyDrawingHeaderFooter element? Or know of any other way around this problem?
Thanks.