Reputation: 519
I am creating a workbook with a worksheet and a table. Afterwards I want to change the title of the header for each column. I have already tried different ways, but when I open the xmls file, excel shows an error message(after pressing repair file it opens). Maybe someone can give a hint. Here is a sample where I produce 2 files, one opens and the other has problem. I am using the latest version of the lib:
public class Test
{
public string Test1 { get; set; }
public string Test2 { get; set; }
}
public void MyTest()
{
List<Test> testList = new List<Test>
{
new Test{Test1 = "T1",Test2 = "T2"},
new Test{Test1 = "T1",Test2 = "T2"}
};
var wb1 = new XLWorkbook();
var ws = wb1.Worksheets.Add("Mysheet");
ws.Cell(1, 1).InsertTable(testList);
wb1.SaveAs(@"C:\Test\Test1.xlsx");
var wb2 = new XLWorkbook();
var ws2 = wb2.Worksheets.Add("Mysheet");
var tb2 = ws2.Cell(1, 1).InsertTable(testList);
const string newHeader = "TestNew";
for (int i = 0; i < tb2.Fields.Count(); i++)
{
tb2.Field(i).Name = string.Format(
"{0} {1}",
newHeader,
i
);
}
wb2.SaveAs(@"C:\Test\Test2.xlsx");
}
Upvotes: 2
Views: 6834
Reputation: 519
I have contacted them and it seems that they fixed this bug in the source code but not in the release. Thank you very much for your interest on this question.
Upvotes: 0
Reputation: 17560
There seems to be a bug in ClosedXML. When you change Field(...).Name
it only changes the displayed value of the cell with the column header but not the field name in the actual table code. (You can check that by opening Test2.xlsx
as a zip file and look at xl\worksheets\sheet.xml
and xl\tables\table.xml
.)
Here is a discussion of that bug. The author of ClosedXML claims that it is fixed but I tested it with the latest version and still got the bug.
You should post there and clarify with the author.
Upvotes: 2