Search code examples
c#epplus

Creating Excel worksheets using Parallel.For and 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
});

Solution

  • 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.