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