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

问题描述 投票:0回答:1

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

Repaired Records: Table from /xl/tables/table1.xml part (Table)

修复后,文件格式正确,但由于这是自动化的,我无法回复使用excel来修复这些文件。它只会在我创建表时导致此问题。

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

DefineTable(worksheetPart, 2, 12, 1, 8);

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);
    }

我不确定为什么桌子的构造不正确,我将不胜感激,我可以帮助解决这个问题。

我还将在修复之前和之后包括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>

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>

提前致谢。

c# excel openxml-sdk
1个回答
0
投票

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

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

© www.soinside.com 2019 - 2024. All rights reserved.