I'm creating multiple spreadsheets (separate files), each containing multiple sheets, and when I open the output file of one of these spreadsheets in excel it asks if I want to repair it (this only happens when I add tables), and shows what was repaired:
Repaired Records: Table from /xl/tables/table1.xml part (Table)
After repairing, the file is in the correct format, but as this is being automated I can't reply on using excel to repair these files. It only causes this problem when I create a table.
For example: I call the Define Table method using the following parameters to create a table of 11 Rows (Rows 2-12 inclusive) and 8 Columns (1-8 inclusive):
DefineTable(worksheetPart, 2, 12, 1, 8);
The DefineTable method is shown below:
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);
}
I'm not sure why the table is being constructed incorrectly, I would appreciate any help I can get to fix this.
I will also include table1.xml before and after being repaired: table1.xml Before Repair:
<?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 After Repair:
<?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>
Thanks in advance.
I fought with this same issue for several days. I was dynamically creating an Excel spreadsheet using Open XML. It all worked fine until I tried adding a table. Once I did that I kept getting the warning when opening the spreadsheet about it needing repair.
I finally fixed the problem by adding the column heading text to the spreadsheet data. For example if I had a table on cells A1:C7 with 2 columns defined (names of "Column1" and "Column2"), along with adding the table and columns, I also added the text "Column1" to cell A1 and "Column2" to cell B1.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With