OpenXML电子表格删除行导致excel不可读的内容错误

我试图清除Excel模板中的行。 之前的代码通过并创build基于模板的工作簿。 该代码生成没有错误的罚款excel文件。 只有在添加这个部分的时候才会遇到问题:

Sheet theSheet = workbookPart.Workbook.Descendants<Sheet>() .Where(s => s.Name == task).FirstOrDefault(); if (theSheet != null) { WorksheetPart worksheetPart = (WorksheetPart)workbookPart.GetPartById(theSheet.Id); SheetData sheetData = worksheetPart.Worksheet.GetFirstChild<SheetData>(); var rows = sheetData.Elements<Row>().Where(r => r.RowIndex > 1).ToArray(); for (int x = 0; x < rows.Count(); x++) { ((Row)rows[x]).Remove(); } worksheetPart.Worksheet.Save(); } 

它成功清除了行。 但是,当我在Excel中打开文件时收到以下错误:

Excel在“excel.xlsx”中发现不可读的内容。 你想恢复这个工作簿的内容?…

点击是给出以下详细信息:

 <repairedRecords summary="Following is a list of repairs:"> <repairedRecord>Repaired Records: Cell information from /xl/worksheets/sheet1c.xml part</repairedRecord> <repairedRecord>Repaired Records: Cell information from /xl/worksheets/sheet1d.xml part</repairedRecord> <repairedRecord>Repaired Records: Cell information from /xl/worksheets/sheet1b.xml part</repairedRecord> <repairedRecord>Repaired Records: Cell information from /xl/worksheets/sheet1a.xml part</repairedRecord> <repairedRecord>Repaired Records: Cell information from /xl/worksheets/sheet26.xml part</repairedRecord> <repairedRecord>Repaired Records: Cell information from /xl/worksheets/sheet1f.xml part</repairedRecord> <repairedRecord>Repaired Records: Cell information from /xl/worksheets/sheet1e.xml part</repairedRecord> </repairedRecords> 

如果我打开“Open XML SDK 2.5生产力工具”中的Excel文件并validation它。 它给了更多的信息:

 Error Node Type: Worksheet Error Part: /xl/worksheets/sheet1a.xml (this is the only line chat changes and it corresponds to the above errors) Error Node Path: /x:worksheet[1] Related Node Type: OpenXmlUnknownElement Related Part: Description: The element has invalid child element 'http://schemas.openxmlformats.org/sheadsheetml/2006/main:row'. 

如果我打开此代码正在修改的原始Excel文件,sheet1a / sheet1b等不存在。 他们来自哪里? 有什么我失踪? 当我正在做的是删除行时,这些工作表如何包含无效的行元素? 谢谢你的任何build议。

编辑:修剪sheet1a.xml的forms:

  <?xml version="1.0" encoding="utf-8"?> <x:worksheet xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:x="http://schemas.openxmlformats.org/spreadsheetml/2006/main"> <x:dimension ref="A1:AK180" /> <x:sheetViews> <x:sheetView workbookViewId="0" /> </x:sheetViews> <x:sheetFormatPr defaultRowHeight="15" /> <x:cols> <x:col min="1" max="1" width="13.85546875" bestFit="1" customWidth="1" /> </x:cols> <x:sheetData> <x:row> <x:cr="A1" t="inlineStr"> <x:is> <x:t>TestResultFileId</x:t> </x:is> </x:c> </x:row> <x:row r="2"> <x:cr="A2" t="inlineStr"> <x:is> <x:t>6F2DFA01-27EE-E211-8250-0025906392BB</x:t> </x:is> </x:c> </x:row> </x:sheetData> <x:row r="1" spans="1:37"> <x:cr="A1" t="s"> <x:v /> </x:c> </x:row> <x:conditionalFormatting sqref="A1:AK1048576"> <x:cfRule type="expression" dxfId="7" priority="1"> <x:formula /> </x:cfRule> </x:conditionalFormatting> <x:pageMargins left="0.7" right="0.7" top="0.75" bottom="0.75" header="0.3" footer="0.3" /> </x:worksheet> 

基于工作表的XML,这部分:

 <x:row r="1" spans="1:37"> <x:cr="A1" t="s"> <x:v /> </x:c> </x:row> 

不应该存在于SheetData元素之外。 事实上,似乎是重复的,因为

 <x:row> <x:cr="A1" t="inlineStr"> <x:is> <x:t>TestResultFileId</x:t> </x:is> </x:c> </x:row> 

也存在。 请注意,“实际”标题行没有分配RowIndex,但根据“A1”的CellReference,此特定行在第1行。

请注意这一点:

 var rows = sheetData.Elements<Row>().Where(r => r.RowIndex > 1).ToArray(); 

可能会忽略没有RowIndex分配的任何Row对象(我没有testing这个虽然…)。 哪可能发生。 Excel应该已经分配了一个值,但任何第三方软件都没有这样做(因为Open XML规范声明RowIndex是一个可选属性)。

我不知道为什么在SheetData外有Row对象。 检查原始模板Excel文件没有“SheetData对象之外的行对象”情况。 如果是这样,那么原来的模板文件首先是错误的。

您可能需要考虑将第一行存储在单独的variables中的选项。 然后消除SheetData的所有子元素。 然后追加()第一行。这可能会更容易。 你可以像这样消灭孩子(在这里插入坏父母笑话):

 sheetData.RemoveAllChildren();