OpenXML - 表创建,如何创建表而不需要 excel 修复它们

Jac*_*rol 3 c# excel openxml-sdk

我正在创建多个电子表格(单独的文件),每个电子表格包含多个工作表,当我在 Excel 中打开其中一个电子表格的输出文件时,它会询问我是否要修复它(这只在我添加表格时才会发生),并显示修复了什么:

Repaired Records: Table from /xl/tables/table1.xml part (Table)
Run Code Online (Sandbox Code Playgroud)

修复后,文件格式正确,但由于这是自动化的,我无法回复使用 Excel 修复这些文件。仅当我创建表时才会出现此问题。

例如:我使用以下参数调用 Define Table 方法来创建一个包含 11 行(包含第 2-12 行)和 8 列(包含第 1-8 行)的表:

DefineTable(worksheetPart, 2, 12, 1, 8);
Run Code Online (Sandbox Code Playgroud)

DefineTable方法如下所示:

private static void DefineTable(WorksheetPart worksheetPart, int rowMin, int rowMax, int colMin, int colMax)
    {
        TableDefinitionPart tableDefinitionPart = worksheetPart.AddNewPart<TableDefinitionPart>("rId" + (worksheetPart.TableDefinitionParts.Count() + 1));
        int tableNo = worksheetPart.TableDefinitionParts.Count();

        string reference = ((char)(64 + colMin)).ToString() + rowMin + ":" + ((char)(64 + colMax)).ToString() + rowMax;

        Table table = new Table() { Id = (UInt32)tableNo, Name = "Table" + tableNo, DisplayName = "Table" + tableNo, Reference = reference, TotalsRowShown = false };
        AutoFilter autoFilter = new AutoFilter() { Reference = reference };

        TableColumns tableColumns = new TableColumns() { Count = (UInt32)(colMax - colMin + 1) };
        for (int i = 0; i < (colMax - colMin + 1); i++)
        {
            tableColumns.Append(new TableColumn() { Id = (UInt32)(i + 1), Name = "Column" + i });
        }

        TableStyleInfo tableStyleInfo = new TableStyleInfo() { Name = "TableStyleLight1", ShowFirstColumn = false, ShowLastColumn = false, ShowRowStripes = true, ShowColumnStripes = false };

        table.Append(autoFilter);
        table.Append(tableColumns);
        table.Append(tableStyleInfo);

        tableDefinitionPart.Table = table;

        TableParts tableParts = new TableParts() { Count = (UInt32)1 };
        TablePart tablePart = new TablePart() { Id = "rId" + tableNo };

        tableParts.Append(tablePart);

        worksheetPart.Worksheet.Append(tableParts);
    }
Run Code Online (Sandbox Code Playgroud)

我不确定为什么该表的构建不正确,我将不胜感激任何可以解决此问题的帮助。

我还将包括修复之前和之后的 table1.xml:修复之前的 table1.xml:

<?xml version="1.0" encoding="utf-8" ?>
<x:table id="1" name="Table1" displayName="Table1" ref="A2:H12" 
totalsRowShown="0" 
xmlns:x="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
 <x:autoFilter ref="A2:H12"/>
 <x:tableColumns count="8">
  <x:tableColumn id="1" name="Column0"/>
  <x:tableColumn id="2" name="Column1"/>
  <x:tableColumn id="3" name="Column2"/>
  <x:tableColumn id="4" name="Column3"/>
  <x:tableColumn id="5" name="Column4"/>
  <x:tableColumn id="6" name="Column5"/>
  <x:tableColumn id="7" name="Column6"/>
  <x:tableColumn id="8" name="Column7"/>
 </x:tableColumns>
 <x:tableStyleInfo name="TableStyleLight1" showFirstColumn="0" 
showLastColumn="0" showRowStripes="1" showColumnStripes="0"/>
</x:table>
Run Code Online (Sandbox Code Playgroud)

修复后的table1.xml:

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<table xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" 
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
xmlns:xr="http://schemas.microsoft.com/office/spreadsheetml/2014/revision" 
xmlns:xr3="http://schemas.microsoft.com/office/spreadsheetml/2016/revision3" 
mc:Ignorable="xr xr3" xr:uid="{00000000-000C-0000-FFFF-FFFF00000000}" id="1" 
name="Table1" displayName="Table1" ref="A2:H12" totalsRowShown="0">
 <autoFilter xr:uid="{00000000-0009-0000-0100-000001000000}" ref="A2:H12"/>
 <tableColumns count="8">
  <tableColumn xr3:uid="{00000000-0010-0000-0000-000001000000}" id="1" 
name="Column0"/>
  <tableColumn xr3:uid="{00000000-0010-0000-0000-000002000000}" id="2" 
name="Column1"/>
  <tableColumn xr3:uid="{00000000-0010-0000-0000-000003000000}" id="3" 
name="Column2"/>
  <tableColumn xr3:uid="{00000000-0010-0000-0000-000004000000}" id="4" 
name="Column3"/>
  <tableColumn xr3:uid="{00000000-0010-0000-0000-000005000000}" id="5" 
name="Column4"/>
  <tableColumn xr3:uid="{00000000-0010-0000-0000-000006000000}" id="6" 
name="Column5"/>
  <tableColumn xr3:uid="{00000000-0010-0000-0000-000007000000}" id="7" 
name="Column6"/>
  <tableColumn xr3:uid="{00000000-0010-0000-0000-000008000000}" id="8" 
name="Column7"/>
 </tableColumns>
 <tableStyleInfo name="TableStyleLight1" showFirstColumn="0" 
showLastColumn="0" showRowStripes="1" showColumnStripes="0"/>
</table>
Run Code Online (Sandbox Code Playgroud)

提前致谢。

小智 6

我与同样的问题斗争了好几天。我正在使用 Open XML 动态创建 Excel 电子表格。一切正常,直到我尝试添加一张桌子。一旦我这样做了,我在打开电子表格时不断收到有关它需要修复的警告。

我最终通过将列标题文本添加到电子表格数据来解决了该问题。例如,如果我在单元格 A1:C7 上有一个表,定义了 2 列(名称为“Column1”和“Column2”),那么在添加表和列的同时,我还将文本“Column1”添加到单元格 A1 和“Column2” ” 到单元格 B1。