使用Parallel.For和EPPlus创建Excel工作表 [英] Creating Excel worksheets using Parallel.For and EPPlus
本文介绍了使用Parallel.For和EPPlus创建Excel工作表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用 EPPlus 库来创建包含许多工作表的Excel工作簿。我想知道并行构建工作表是否安全。如果库支持这种行为,我在(有限的)文档中找不到提及内容:
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
});
推荐答案
简单看一下源代码: https://github.com/JanKallman/EPPlus/blob/master/EPPlus/如您所见,ExcelWorksheets.cs
Add方法调用AddSheet方法,该方法使用lock(...)使操作成为线程安全的,因此,可以使用Parallel.For。
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.
这篇关于使用Parallel.For和EPPlus创建Excel工作表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文