我使用此代码将数据加载到工作表(C#,EPPlus 4.04)中并自动设置列宽:
workSheet.Cells["A1"].LoadFromCollection(itemsToExport, PrintHeaders: true);
workSheet.Cells[workSheet.Dimension.Address].AutoFitColumns();
显着的性能损失似乎与工作表中的记录数一致。例如。在15K记录上有2秒的差异,我必须创建多个文件。
有没有办法加快速度而不诉诸异步处理等?
Ps,在填充数据之前设置AutoFitColumns不起作用,列将保持宽度对于数据来说太小。
答案 0 :(得分:3)
/// <summary>
/// Set the column width from the content of the range.
/// Note: Cells containing formulas are ignored if no calculation is made.
/// Wrapped and merged cells are also ignored.
/// </summary>
/// <param name="MinimumWidth">Minimum column width</param>
/// <param name="MaximumWidth">Maximum column width</param>
public void AutoFitColumns(double MinimumWidth, double MaximumWidth)
遗憾的是,所有单元格都将在循环中处理。也许我应该使用像Courier这样均匀间隔的字体并自己计算宽度。
EPPlus库中的AutoFitColumns可以使用一些额外的注意力,因为它的代码复杂度为35 atm(但保持良好的工作!)。