1

I am using openxml-sdk to generate an excel file and upon opening of the file it tells me that the file is corrupt and offers to repair it. The repair does the following:

Removed Records: Cell information from /xl/worksheets/sheet.xml part
Repaired Records: Cell information from /xl/worksheets/sheet.xml part

This repair results in the loss of some data, I have looked into the XML and discovered that the data seems to be removed because the row has been written out of order when a merged cell is encountered.

<x:sheetData>
    <x:row r="8">
         ...
    </x:row>
    <x:row r="16">
         ...
    </x:row>
    <x:row r="9">
         ...
    </x:row>
    <x:row r="10">
         ...
    </x:row>
    ...
</x:sheetData>
<x:mergeCells>
    ...
<x:mergeCell ref="C8:C16" />
    ...
</x:mergeCells>

So as the above shows, there is a merge of C8:C16, wherever a merge like this happens it seems to write the row for the first and last then continue will all in the middle. However, when opening this excel doesn't seem to be able to cope and removes all out of order rows (9-15).

I have confirmed this by manually rearranging the rows in the XML then rezipping. It will then display all the data.

I am guessing this happens because I create row 16 earlier then row 9 so that I can do the merge. Is there a way to reorder the rows or get excel to handle this without having to create all the rows in advance?

4

1 回答 1

1

就 Excel 而言 - 您无法使 Excel 接受以错误顺序写出的行。您必须以某种方式编写代码,以便以正确的顺序写出行。

-埃里克

于 2013-03-04T22:13:08.500 回答