sheet1.AutoSizeColumn(Columns);//npoi自适应宽度(写入数据后调用)
//自定义自适应宽度(优化导出性能,提升导出速度)
for (int i = 0; i < iColumns; i++)
{
int columnWidth = sheet1.GetColumnWidth(i) / 256;
for (int j = 0; j < iRows; j++)
{
IRow currentRow = sheet1.GetRow(j);
if (currentRow.GetCell(i) != null)
{
ICell currentCell = currentRow.GetCell(i);
int length = Encoding.UTF8.GetBytes(currentCell.ToString()).Length;//获取内容宽度
if (columnWidth < length)
{
columnWidth = length;
}
if (length > 255)
{
columnWidth = 255;
}
}
}
sheet1.SetColumnWidth(i, columnWidth * 256);
}