nissl-lab / npoi

a .NET library that can read/write Office formats without Microsoft Office installed. No COM+, no interop.
Apache License 2.0
5.65k stars 1.42k forks source link

CreateColumn return System.ArgumentOutOfRangeException: Index was out of range #1384

Open matteo-fantin opened 1 month ago

matteo-fantin commented 1 month ago

2.7.0

Issue Description

When I try to create a new column on table, the method CreateColumn return the following error:

System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index')
   at System.Collections.Generic.List`1.get_Item(Int32 index)
   at NPOI.XSSF.UserModel.XSSFTable.CreateColumn(String columnName, Int32 columnIndex)

Codes

private static void CreateCustomSheet(IWorkbook workbook, List<MyCustomClass> values)
{
    // Create Sheet
    XSSFSheet sheet = (XSSFSheet)workbook.CreateSheet("SheetName");
    sheet.TabColor = new XSSFColor(SixLabors.ImageSharp.Color.Orange);

    // Create table
    XSSFTable table = sheet.CreateTable();
    table.Name = "TableName";
    table.CreateColumn("ColumnName1", 0); // <-- Here
    table.CreateColumn("ColumnName2", 1);
    table.CreateColumn("ColumnName3", 2);
    table.StyleName = XSSFBuiltinTableStyleEnum.TableStyleLight9.ToString();

    // Fill in the data
    // [...]
}