我把.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);
}
答案 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);
}