Concurrent Reading Worksheets in Workbook

For my use case, I need to read through an entire workbook as quickly as possible. I’d like to open the workbook once, and then in parallel read through each worksheet.

The example code I put together below “works”, but I am not sure if there are hidden gotchas with how things are implemented under the hood. Is this approach safe?

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using GemBox.Spreadsheet;

class Program
{
    static void Main()
    {
        // Set the license key (use FreeLimit for testing purposes)
        SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY");

        string filePath = "sample.xlsx";
        var allRows = new ConcurrentBag<List<string>>();

        // Load the workbook
        var workbook = ExcelFile.Load(filePath);

        // Process each sheet concurrently
        Parallel.ForEach(workbook.Worksheets, sheet =>
        {
            foreach (var row in sheet.Rows)
            {
                var rowData = row.AllocatedCells.Select(cell => cell.Value?.ToString() ?? string.Empty).ToList();
                allRows.Add(rowData);
            }
        });

        // Display collected data
        foreach (var row in allRows)
        {
            Console.WriteLine(string.Join(", ", row));
        }
    }
}

Hi,

You can only read the worksheets concurrently, so this should work for you.

However, I conducted a benchmark comparison between the regular List<List<string>> and concurrent ConcurrentBag<List<string>> reading to evaluate their performance.

While concurrent processing does show a slight performance improvement (approximately 4–8%), the difference is relatively small. Also, the actual performance gain varies depending on the structure and size of the processed Excel files.

Anyway, I hope this helps, let me know if you need anything else.

Regards,
Mario

1 Like