How to Load a Spreadsheet in .NET
Aspose.Cells FOSS for .NET lets you open existing XLSX spreadsheets with a single constructor call and provides a fault-tolerant loading path for files that have minor structural corruption. Install the library from NuGet with dotnet add package Aspose.Cells_FOSS.
Step-by-Step Guide
Step 1: Install the Package
dotnet add package Aspose.Cells_FOSSVerify the package is present:
dotnet list package | grep Aspose.Cells_FOSSStep 2: Import the Namespace
Add this directive at the top of your C# file:
using Aspose.Cells_FOSS;Step 3: Load an XLSX File
Pass the file path to the Workbook constructor. The library reads the XLSX file and populates the in-memory workbook object.
using Aspose.Cells_FOSS;
var wb = new Workbook("data.xlsx");
Console.WriteLine("Loaded " + wb.Worksheets.Count + " sheet(s)");
Console.WriteLine("First sheet: " + wb.Worksheets[0].Name);
Console.WriteLine("A1 value: " + wb.Worksheets[0].Cells["A1"].StringValue);Step 4: Load with Fault-Tolerant Repair Options
For files that may have ZIP or XML corruption, supply a LoadOptions object with TryRepairPackage and TryRepairXml set to true. Wrap the call in a try/catch for WorkbookLoadException in case the file is unrecoverable.
using Aspose.Cells_FOSS;
var opts = new LoadOptions
{
TryRepairPackage = true,
TryRepairXml = true,
};
try
{
var wb = new Workbook("possibly-corrupt.xlsx", opts);
Console.WriteLine("Loaded successfully");
}
catch (WorkbookLoadException ex)
{
Console.WriteLine("Unrecoverable file: " + ex.Message);
}Step 5: Check Load Diagnostics
After loading, inspect Workbook.LoadDiagnostics to see whether the library applied any repairs. HasRepairs is true if any repair was applied; HasDataLossRisk is true if data may have been discarded. Iterate Issues to access individual DiagnosticEntry records.
using Aspose.Cells_FOSS;
var opts = new LoadOptions { TryRepairPackage = true, TryRepairXml = true };
var wb = new Workbook("file.xlsx", opts);
var diag = wb.LoadDiagnostics;
if (diag.HasRepairs)
{
Console.WriteLine("Repairs were applied. Data loss risk: " + diag.HasDataLossRisk);
foreach (var entry in diag.Issues)
Console.WriteLine($" [{entry.Severity}] {entry.Code}: {entry.Message}");
}
else
{
Console.WriteLine("File loaded cleanly — no repairs.");
}Step 6: Access Worksheet Data
After loading, access worksheets by index or name, and read cell values using Cell.StringValue or Cell.Value.
using Aspose.Cells_FOSS;
var wb = new Workbook("data.xlsx");
// By index
var sheet = wb.Worksheets[0];
Console.WriteLine("Sheet: " + sheet.Name);
Console.WriteLine("A1: " + sheet.Cells["A1"].StringValue);
// By name
var named = wb.Worksheets["Report"];
if (named != null)
Console.WriteLine("Report!B2: " + named.Cells["B2"].StringValue);Common Issues and Fixes
WorkbookLoadException is thrown even with repair options enabled.
This means the file’s ZIP structure is too damaged for the repair algorithms to recover. Verify the file is a valid ZIP archive (e.g. try unzip -t file.xlsx). If the file is completely corrupt, it cannot be opened by any library.
The loaded workbook has fewer sheets than expected.
A repair operation may have dropped worksheets that referenced missing parts. Check LoadDiagnostics.Issues for entries with DataLossRisk = true and notify the user.
Cell values are empty after load.
Ensure you are accessing the correct worksheet index or name. Use wb.Worksheets.Count to confirm the expected number of sheets. Also check that the XLSX file is not password-protected (password-protected files are not currently supported).
Workbook constructor hangs on a large file.
The constructor is synchronous and loads the entire file into memory. For very large files, run the load on a background thread to keep the UI responsive.
DiagnosticEntry.Severity shows Error but HasRepairs is false.
HasRepairs reflects whether a repair was successfully applied, not whether errors were detected. An Error-severity entry with RepairApplied = false means the issue was detected but could not be repaired — treat this as a possible data integrity warning.
Frequently Asked Questions
Can I load an XLSX file from a Stream instead of a file path?
Yes. The Workbook constructor has overloads that accept a Stream object. Pass your stream alongside a LoadOptions instance if fault-tolerant loading is needed.
Does the library support loading password-protected XLSX files?
Password-protected files are not in the current API surface. Attempting to load one will result in a WorkbookLoadException.
How do I check which sheet is active in the loaded workbook?
Read Workbook.Worksheets.ActiveSheetName to get the name of the sheet that was last active when the file was saved.
Can I load multiple XLSX files concurrently?
Yes. Each Workbook instance is independent. You can instantiate multiple Workbook objects on separate threads without shared state.
What happens if a formula cell is loaded and the cached result is absent?
Cell.StringValue returns an empty string. Cell.Formula still returns the formula string. The cached value is populated only when Excel (or a compatible application) opens and saves the file.