Creating Excel worksheets using Parallel.For and EPPlus

[亡魂溺海] 提交于 2019-12-12 15:28:17

问题


I am using the EPPlus library to create an Excel workbook with many worksheets. I was wondering if it is safe to build the worksheets in parallel. I could not find a mention in the (limited) documentation if the library supports this kind of behavior:

package = new ExcelPackage();
int start = 1;
int end = 100;

Parallel.For(start, end; s =>
{
    var worksheet = package.Workbook.Worksheets.Add("Worksheet" + s.ToString());
    //routine to populate data here
});

回答1:


Take a short look for the source code: https://github.com/JanKallman/EPPlus/blob/master/EPPlus/ExcelWorksheets.cs

As you can see, the Add method calls the AddSheet method that uses lock(...) to make the operation thread-safe, so yes, you can use the Parallel.For.



来源:https://stackoverflow.com/questions/34911085/creating-excel-worksheets-using-parallel-for-and-epplus

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!