NPOI AutoSizeColumn没有正确调整大小

时间:2016-03-07 15:58:23

标签: c# xlsx npoi

我把.AutoSizeColumn放在写入方法

之前
int numberOfColumns = sheet.GetRow(rowcount - 1).PhysicalNumberOfCells;
for (int i = 0; i <= numberOfColumns; i++)
{
    sheet.AutoSizeColumn(i);
    GC.Collect();
} 

using (var fileData = new FileStream(@"C:\Temp\Contatti.xlsx", FileMode.Create))
{
   wb.Write(fileData);
}

this is an example of the result

1 个答案:

答案 0 :(得分:1)

问题也很明显,PhysicalNumberOfCells可以返回1,即使你有一个单元格,也可以在'Z'列中说。有LastCellNum属性,您代替PhysicalNumberOfCells

        int lastColumNum = sheet.GetRow(0).LastCellNum;
        for (int i = 0; i <= lastColumNum; i++)
        {
            sheet.AutoSizeColumn(i);
            GC.Collect();
        }

        using (var fileData = new FileStream(@"D:\Contatti.xlsx", FileMode.Create))
        {
            wb.Write(fileData);
        }