将大对象写入 excel 太浪费时间了。有任何解决这个问题的方法吗?
Writing large object into excel is taking hell out of time. Any way to fix this?
我有一个 excel 文件的预定义格式,我正在将我的对象写入该 excel 中,这会花费大量时间并且应用程序将进入陈旧模式。
我正在使用 EpPlus 库。
请找到下面的代码,让我知道是否可以改进。
FileInfo fileInfo = new FileInfo(xslxpath);
using (ExcelPackage excelPackage = new ExcelPackage(fileInfo))
{
int excelRow = 2;
ExcelWorksheet worksheet1 = excelPackage.Workbook.Worksheets.First();
foreach (var rows in objDetails)
{
worksheet1.Cells[excelRow, 1].Value = rows.ID.ToString();
worksheet1.Cells[excelRow, 2].Value = rows.Name.ToString();
worksheet1.Cells[excelRow, 3].Value = rows.Days.ToString();
worksheet1.Cells[excelRow, 4].Value = rows.Amount.ToString();
worksheet1.Cells[excelRow, 5].Value = rows.Reason.ToString();
WriteToLogFile(LogFile, excelRow.ToString() + " " + rows.IPNumber.ToString());
worksheet1.Cells.AutoFitColumns();
excelRow++;
WriteToLogFile(LogFile, excelRow.ToString() + " " + rows.IPNumber.ToString());
}
excelPackage.Save();
}
worksheet1.Cells.AutoFitColumns();
你应该在循环后调用一次,而不是每一行。
我有一个 excel 文件的预定义格式,我正在将我的对象写入该 excel 中,这会花费大量时间并且应用程序将进入陈旧模式。
我正在使用 EpPlus 库。
请找到下面的代码,让我知道是否可以改进。
FileInfo fileInfo = new FileInfo(xslxpath);
using (ExcelPackage excelPackage = new ExcelPackage(fileInfo))
{
int excelRow = 2;
ExcelWorksheet worksheet1 = excelPackage.Workbook.Worksheets.First();
foreach (var rows in objDetails)
{
worksheet1.Cells[excelRow, 1].Value = rows.ID.ToString();
worksheet1.Cells[excelRow, 2].Value = rows.Name.ToString();
worksheet1.Cells[excelRow, 3].Value = rows.Days.ToString();
worksheet1.Cells[excelRow, 4].Value = rows.Amount.ToString();
worksheet1.Cells[excelRow, 5].Value = rows.Reason.ToString();
WriteToLogFile(LogFile, excelRow.ToString() + " " + rows.IPNumber.ToString());
worksheet1.Cells.AutoFitColumns();
excelRow++;
WriteToLogFile(LogFile, excelRow.ToString() + " " + rows.IPNumber.ToString());
}
excelPackage.Save();
}
worksheet1.Cells.AutoFitColumns();
你应该在循环后调用一次,而不是每一行。