Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating Excel worksheets using Parallel.For and EPPlus

Tags:

c#

epplus

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
});
like image 680
codechurn Avatar asked Sep 16 '25 07:09

codechurn


1 Answers

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.

like image 109
stratever Avatar answered Sep 17 '25 21:09

stratever