我正在创建多个电子表格(单独的文件),每个电子表格包含多个工作表,当我在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>
谢谢。
答案 0 :(得分:0)
我为同一天的问题奋斗了几天。我正在使用Open XML动态创建Excel电子表格。一切正常,直到我尝试添加表。一旦这样做,我就在打开需要修复的电子表格时不断收到警告。
我最终通过将列标题文本添加到电子表格数据中解决了该问题。例如,如果我在单元格A1:C7上有一个表,其中定义了2列(名称为“ Column1”和“ Column2”),并添加了表和列,我还向单元格A1和“ Column2”中添加了文本“ Column1” “到单元格B1。
答案 1 :(得分:0)
您需要为指定的列值添加单元格值,并且只需定义一次 TableParts
。
此外,为了重复使用偏移列,必须将列 id 添加到 colMin
变量中。
有了这些更改,它应该可以工作了。
public static Table 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)(colMin + i), Name = "Column" + i }); //changed i+1 -> colMin + i
//Add cell values (shared string)
}
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 = (TableParts)worksheetPart.Worksheet.ChildElements.Where(ce => ce is TableParts).FirstOrDefault(); // Add table parts only once
if (tableParts is null )
{
tableParts = new TableParts();
tableParts.Count = (UInt32)0;
worksheetPart.Worksheet.Append(tableParts);
}
tableParts.Count += (UInt32)1;
TablePart tablePart = new TablePart() { Id = "rId" + tableNo };
tableParts.Append(tablePart);
return table;
}