This project has moved. For the latest updates, please go here.

Changing table column header texts

Mar 28, 2014 at 1:09 PM
Edited Mar 28, 2014 at 1:09 PM
Hello,

thanks for the effort you've put into ClosedXML.

However, I'm struggling with tables on sheets. In the code (please see below, VS2012, .NET 4.0, ClosedXML 0.69.1.0) I load an XLSX file which only contains a table with headers on a sheet. If I then change a column header text via the cell's Value property or SetValue method, Excel 2013 complains about invalid data in the saved XLSX file when opening it. What is the suggested way to change a table column's header text? Does it matter whether the table's header cell is accessed via the worksheet's Cell(...) method or via the table object's Cell(...) method?
I tried the example from https://closedxml.codeplex.com/discussions/406341 and it does work but unfortunately there's a template XLSX file with an existing table I need to alter and populate with data. I also tried to change the header text using the output file of the linked example as template. The Open XML 2.0 SDK Productivity Tools validation only points out four invalid margins in the XLSX file. A diff on both XLSX file contents does not reveal anything unexpected.

Any help is appreciated. Thanks a lot in advance!

Kind regards,
Kristian


Code excerpt:
using (XLWorkbook workbook = new XLWorkbook(@"template.xlsx"))
{
    // Access sheet and table by indices because this example only contains one sheet and one table.
    IXLWorksheet sheet = workbook.Worksheet(1);
    IXLTable table = sheet.Table(0);
    workbook.SaveAs(@"resultOriginal.xlsx");
    // Change first column header text.
    table.Cell(1, 1).Value = "Name";
    workbook.SaveAs(@"result.xlsx");
}
May 27, 2014 at 12:23 PM
Tried working with the table fields. While I was debugging I noticed that the fieldnames still contains column28 as the name of the last field even though I set the name of the field to Card by this line .
tbl.Field(tbl.Fields.Last.Index).Name = ScheduleFields.HasCard
Coordinator
May 27, 2014 at 1:42 PM
You're right, it should change the field name on the sheet. I just committed a fix for it. Pick up the latest source code.