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.74k stars 1.44k forks source link

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

Open matteo-fantin opened 4 months ago

matteo-fantin commented 4 months 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
    // [...]
}
wuzxc1230123 commented 2 months ago

fix