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));
}
}
}