我正在使用EPPlus将.csv文件转换为.xlsx,并使用ZXing将其中一列转换为条形码图像。一切工作正常,但AddPicture的性能会导致问题。.csv文件可能有成千上万条记录,记录数量越大,应用程序运行得越慢。我已经将问题缩小到了AddPicture函数,并了解到它会将图像复制到临时文件夹中,插入到电子表格中,并删除临时图像,这是大量磁盘I / O。如果我注释掉AddPicture函数,则应用程序在1-2秒内运行。启用AddPicture函数会使运行时间指数级增加(基于记录数量),以至于无法使用。
for (var r = 2; r < (workSheet.Dimension.End.Row + 1); r++)
{
//set row height
workSheet.Row(r).Height = 50;
//create barcode string
string myBarcode = String.Format("{0}", workSheet.Cells[r, 7].Text);
//create image
string newFileName = "C:/DUMP/barcodes/" + myBarcode + ".jpeg";
if (!File.Exists(newFileName))
{
createBarcode(myBarcode, 100, 40);
}
//add image to worksheet
var picture = workSheet.Drawings.AddPicture(myBarcode, new FileInfo (newFileName));
//set image position
int eppRow = r - 1;
picture.SetPosition(eppRow, 10, 7, 5);
//enable "TwoCell" to maintain column anchors
picture.EditAs = OfficeOpenXml.Drawing.eEditAs.TwoCell;
}
我最初尝试在内存中创建图像并直接插入电子表格中。如上所示的版本将图像写入磁盘,并仅在必要时创建新图像。无论哪个版本处理大量记录时都表现不佳。
EPPlus很好地完成了.csv文件的转换,ZXing可以快速创建图像。它们运行得非常顺畅。但是,使用AddPicture将图像插入Excel文件会导致性能问题。是否有其他关于如何解决此问题的建议?