One of the most common jobs when working with spreadsheets in .NET is reading Excel files and extracting data for processing, reporting, or database import. Many developers assume they need Microsoft Excel installed or use heavy Office Interop automation, but that's not the case. With IronXL, a compact and powerful Excel library for .NET, you can easily work with.xls and.xlsx files without depending on Microsoft Office.
In this guide, we'll explore how to read Excel files in C# using IronXL, process numeric values, work with worksheets, handle exceptions, and even convert data into other formats like CSV or DataTables.
Why Use IronXL for Reading Excel Files?
- ✅ No Excel installed required: Works without Microsoft Office.
- ✅ Supports all major Excel formats: .xls, .xlsx, and .csv.
- ✅ Works with .NET Framework & .NET Core: Compatible with Windows, Linux, macOS.
- ✅ Easy-to-use API: Quickly read, create, and save Excel files.
- ✅ Powerful data handling: Import, query, and convert spreadsheets into DataSet or DataTable.
This makes IronXL the perfect C# Excel reader library for developers looking to import or display data without unnecessary complexity.
Installation
To get started, install the IronXL package from NuGet:
dotnet add package IronXL.Excel
Or via Package Manager:
Install-Package IronXL.Excel
Example: Reading an Excel File in C#
Here's a simple C# program that reads data from an Excel file and displays it in the console. For this example, we'll use an example Excel sheet with the following data:
using IronXL;
using System;
using System.Data;
class Program
{
static void Main(string[] args)
{
// Load the Excel file
var workbook = WorkBook.Load("test.xlsx");
// Get the first worksheet
var worksheet = workbook.WorkSheets[0];
Console.WriteLine("Reading Excel File...");
// Loop through rows and cells
foreach (var row in worksheet.Rows)
{
foreach (var cell in row.Columns)
{
Console.Write(cell.Value + "t"); // Display values
}
Console.WriteLine();
}
Console.WriteLine("Done!");
}
}
What this code does:
- Uses WorkBook.Load(filename) to open an Excel file.
- Accesses a worksheet with WorkSheets[index].
- Iterates through rows and cells with foreach to read the Excel file.
- Displays data values in the console.
Code Output
Reading Data into a DataTable
If you're working with databases, you'll likely need a DataTable or DataSet. IronXL makes this easy. Let's try it using this Excel data:
using IronXL;
using System.Data;
class Program
{
static void Main(string[] args)
{
WorkBook workbook = WorkBook.Load("employees.xlsx");
WorkSheet sheet = workbook.WorkSheets[0];
// Convert worksheet to DataTable
DataTable dt = sheet.ToDataTable(true);
Console.WriteLine("Imported Data from Excel:");
foreach (DataRow row in dt.Rows)
{
foreach (var item in row.ItemArray)
{
Console.Write(item + "t");
}
Console.WriteLine();
}
}
}
This example shows how to:
- Import data from an Excel worksheet into a DataTable.
- Iterate through rows and display numeric values or strings.
- Work with structured data for saving into a database.
Output
Handling Different Excel Formats
IronXL supports multiple formats:
- XLSX - Modern Excel files.
- XLS - Legacy Excel 97-2003 files.
- CSV - Lightweight comma-separated files.
Example of loading different formats:
var xlsx = WorkBook.Load("report.xlsx");
var xls = WorkBook.Load("legacy.xls");
var csv = WorkBook.LoadCSV("data.csv");
Error Handling and Exceptions
When reading Excel files, you may encounter missing files, null values, or unsupported formats. IronXL provides clear error messages and exception handling:
try
{
var workbook = WorkBook.Load("missing.xlsx");
var sheet = workbook.WorkSheets.First();
}
catch (Exception ex)
{
Console.WriteLine("Error reading Excel file: " + ex.Message);
}
This ensures your program won't crash if a file, sheet, or cell object is invalid.
Importing and Saving Data
You can also modify and save Excel files after reading them. For example, let's edit the student data file from earlier in the article:
WorkBook workbook = WorkBook.Load("test.xlsx");
var sheet = workbook.DefaultWorkSheet;
// Update a cell value
sheet["D2"].Value = 95;
// Save changes
workbook.SaveAs("test_updated.xlsx");
IronXL allows full read + write automation, making it a complete Excel solution for .NET applications.
Output
As you can see from this output, the number which was previously 88, has now been updated with just a few lines of code.
Advanced Features of IronXL
IronXL goes beyond simply reading and writing Excel files. It provides a wide range of advanced capabilities that make it a complete spreadsheet solution for .NET developers.
1. Query Data Using LINQ
With IronXL, Excel worksheets can be queried just like collections in C#. Developers can use familiar LINQ syntax to filter rows, sort results, and retrieve only the data they need. This makes it easy to treat an Excel sheet almost like a database table, enabling more powerful data analysis and reporting.
2. Convert Excel to CSV, JSON, or PDF
IronXL supports seamless conversion of spreadsheets into multiple formats. You can transform Excel workbooks into CSV files for lightweight data exchange, JSON for integration with APIs and modern web applications, or PDF for sharing professional reports. This flexibility means the same Excel data can be repurposed across different platforms and workflows.
3. Handle Formulas, Formatting, and Cell Styles
IronXL not only reads the values in a spreadsheet but also edits formulas. This ensures calculations created in Excel remain accurate when processed through C#. In addition, developers can apply formatting and cell styles programmatically, such as bold fonts, colors, or number formats, to generate polished and professional-looking spreadsheets.
4. Work with Large Spreadsheets Efficiently
Performance is a key consideration when dealing with big datasets. IronXL is optimized to handle large Excel files with tens of thousands of rows without relying on Microsoft Office. It is suitable for server-side applications, cloud environments, and data-intensive workflows where speed and memory efficiency are critical.
Together, these advanced features make IronXL a powerful tool for any developer who needs more than just basic Excel file access. It enables automation, reporting, and data transformation at scale, without requiring Excel installed on the system.
Conclusion
With IronXL, reading Excel files in C# is simple, fast, and doesn't require Microsoft Office. You can load spreadsheets, read rows and cells, import into DataTable, handle exceptions, and save changes with just a few lines of code.
Whether you're building a .NET Core web API, a Windows desktop app, or a data-processing service, IronXL provides the tools you need to seamlessly work with Excel files.
👉 Try the IronXL free trial today and simplify your Excel automation workflow!