C# Read XLSX File
To read XLSX files in C#, use IronXL's WorkBook.Load method to open Excel files and access worksheets to read cell data, perform calculations, and convert to DataTable or DataSet formats programmatically.
Quickstart: Load a workbook and access a worksheet effortlessly
With IronXL, you can load an XLSX file using the WorkBook.Load method in a single line. Then access its first or named worksheet instantly and begin reading cell values.
Get started making PDFs with NuGet now:
Install IronXL with NuGet Package Manager
Copy and run this code snippet.
IronXL.WorkBook workbook = IronXL.WorkBook.Load("your-file.xlsx");Deploy to test on your live environment
Read .XLSX Files C#
- Get IronXL for your project
- Load a WorkBook
- Access data from a WorkSheet
- Apply functions like Sum, Min, & Max
- Read a WorkSheet as a DataTable, DataSet, and more
How Do I Get IronXL for My Project?
Use IronXL in your project for a simple way to work with Excel file formats in C#. You can either install IronXL via direct download or alternatively you can use NuGet Install for Visual Studio. The software is free for development.
Install-Package IronXL.Excel
Before diving into reading XLSX files, explore the comprehensive IronXL documentation to understand all available features. IronXL supports both .xls and .xlsx formats, making it versatile for legacy and modern Excel files.
How to Tutorial
How Do I Load a WorkBook?
WorkBook is the class of IronXL whose object provides full access to the Excel file and all its functions. For example, to access an Excel file, use the code:
:path=/static-assets/excel/content-code-examples/how-to/c-sharp-read-xlsx-file-load-workbook.csusing IronXL;
// Load the workbook
WorkBook workBook = WorkBook.Load("sample.xlsx"); // Excel file pathIRON VB CONVERTER ERROR developers@ironsoftware.comWhy use the WorkBook.Load() method?
In the above code, the WorkBook.Load() function loads sample.xlsx into workBook. Any type of function can be performed on workBook by accessing the specific WorkSheet of an Excel file. The WorkBook.Load method automatically detects the file format, whether it's XLS, XLSX, XLSM, XLTX, or CSV. For more advanced loading scenarios, check out the detailed guide on loading spreadsheets.
How Do I Access a Specific WorkSheet?
To access a specific WorkSheet of an Excel file, IronXL provides the WorkSheet class. It can be used in several different ways:
What are the different ways to access a worksheet?
:path=/static-assets/excel/content-code-examples/how-to/c-sharp-read-xlsx-file-get-worksheet.csusing IronXL;
// Access sheet by name
WorkSheet workSheet = workBook.GetWorkSheet("Sheet1");IRON VB CONVERTER ERROR developers@ironsoftware.comworkBook is the WorkBook that is declared in the above portion.
OR
:path=/static-assets/excel/content-code-examples/how-to/c-sharp-read-xlsx-file-worksheet-index.csusing IronXL;
// Access sheet by index
WorkSheet workSheet = workBook.WorkSheets[0];IRON VB CONVERTER ERROR developers@ironsoftware.comOR
:path=/static-assets/excel/content-code-examples/how-to/c-sharp-read-xlsx-file-default-worksheet.csusing IronXL;
// Access the default worksheet
WorkSheet workSheet = workBook.DefaultWorkSheet;IRON VB CONVERTER ERROR developers@ironsoftware.comOR
:path=/static-assets/excel/content-code-examples/how-to/c-sharp-read-xlsx-file-first-worksheet.csusing IronXL;
using System.Linq;
// Access the first worksheet
WorkSheet workSheet = workBook.WorkSheets.First();IRON VB CONVERTER ERROR developers@ironsoftware.comOR
:path=/static-assets/excel/content-code-examples/how-to/c-sharp-read-xlsx-file-first-or-default-worksheet.csusing IronXL;
using System.Linq;
// Access the first or default worksheet
WorkSheet workSheet = workBook.WorkSheets.FirstOrDefault();IRON VB CONVERTER ERROR developers@ironsoftware.comWhen should I use each worksheet access method?
Each method has its ideal use case:
- GetWorkSheet("name"): When you know the exact sheet name
- WorkSheets[index]: For iterating through sheets programmatically
- DefaultWorkSheet: Quick access when working with single-sheet files
- First() or FirstOrDefault(): Safe options when sheet names might change
After getting ExcelSheet workSheet, you can get any type of data from it and perform all Excel functions on it. For more complex worksheet operations, refer to the guide on opening Excel worksheets in C#.
How Do I Access Data from a WorkSheet?
Data can be accessed from ExcelSheet workSheet with this process:
What data types can I read from cells?
:path=/static-assets/excel/content-code-examples/how-to/c-sharp-read-xlsx-file-get-data.csusing IronXL;
// Accessing data as a string
string dataString = workSheet["A1"].ToString();
// Accessing data as an integer
int dataInt = workSheet["B1"].Int32Value;IRON VB CONVERTER ERROR developers@ironsoftware.comIronXL provides various value accessors for different data types:
- StringValue: For text data
- Int32Value: For integers
- DoubleValue: For decimals
- DateTimeValue: For dates
- BoolValue: For true/false values
How do I read multiple cells at once?
You can also get data from multiple cells of a specific column:
foreach (var cell in workSheet["A2:A10"])
{
Console.WriteLine("Value is: {0}", cell.Text);
}foreach (var cell in workSheet["A2:A10"])
{
Console.WriteLine("Value is: {0}", cell.Text);
}For Each cell In workSheet("A2:A10")
Console.WriteLine("Value is: {0}", cell.Text)
Next cellThis displays the values from cell A2 to A10. For more advanced range selection techniques, visit the select range tutorial.
What does a complete implementation look like?
A complete code example of the specifics above is provided here:
:path=/static-assets/excel/content-code-examples/how-to/c-sharp-read-xlsx-file-log-data.csusing IronXL;
using System;
// Load an Excel file
WorkBook workBook = WorkBook.Load("sample.xlsx");
WorkSheet workSheet = workBook.GetWorkSheet("Sheet1");
// Specify the range
foreach (var cell in workSheet["B2:B10"])
{
Console.WriteLine("Value is: {0}", cell.Text);
}IRON VB CONVERTER ERROR developers@ironsoftware.comIt displays the following result:
With the Excel file Sample.xlsx:
These methodologies show how effortless it is to use Excel file data in your project. For practical examples of reading Excel files without Interop, explore the read Excel examples.
How Can I Perform Functions on Data?
Access filtered data from an Excel WorkSheet by applying aggregate functions like Sum, Min, or Max using the following code:
Which aggregate functions are available?
:path=/static-assets/excel/content-code-examples/how-to/c-sharp-read-xlsx-file-aggregate-function.csusing IronXL;
// Apply aggregate functions
decimal sum = workSheet["G2:G10"].Sum(); // Sum of cells from G2 to G10
decimal min = workSheet["G2:G10"].Min(); // Minimum value in cells from G2 to G10
decimal max = workSheet["G2:G10"].Max(); // Maximum value in cells from G2 to G10IRON VB CONVERTER ERROR developers@ironsoftware.comIronXL supports numerous mathematical functions including:
- Average(): Calculate the mean value
- Count(): Count non-empty cells
- CountIf(): Count cells meeting criteria
- Median(): Find the middle value
- StdDev(): Calculate standard deviation
How do I implement multiple functions together?
For more details, check out our in-depth tutorial on How to Write C# Excel Files with specifics on aggregate functions. You can also explore the complete list of math functions available in IronXL.
:path=/static-assets/excel/content-code-examples/how-to/c-sharp-read-xlsx-file-min-max.csusing IronXL;
using System;
// Load the Excel workbook
WorkBook workBook = WorkBook.Load("sample.xlsx");
// Get the specified WorkSheet
WorkSheet workSheet = workBook.GetWorkSheet("Sheet1");
// Calculate sum, minimum, and maximum for a range of cells
decimal sum = workSheet["G2:G10"].Sum();
decimal min = workSheet["G2:G10"].Min();
decimal max = workSheet["G2:G10"].Max();
// Output results
Console.WriteLine("Sum is: {0}", sum);
Console.WriteLine("Min is: {0}", min);
Console.WriteLine("Max is: {0}", max);IRON VB CONVERTER ERROR developers@ironsoftware.comThis code displays the following output:
And this is how the Excel file Sample.xlsx looks:
How Do I Read Excel WorkSheet as DataTable?
Using IronXL, it is easy to work with an Excel WorkSheet as a DataTable. This feature is particularly useful when you need to integrate Excel data with existing data processing pipelines or bind data to UI controls.
What is the basic conversion method?
:path=/static-assets/excel/content-code-examples/how-to/c-sharp-read-xlsx-file-datatable.csusing IronXL;
using System.Data;
// Convert worksheet to DataTable
DataTable dt = workSheet.ToDataTable();IRON VB CONVERTER ERROR developers@ironsoftware.comHow do I use the first row as column headers?
To use the first row of ExcelSheet as DataTable ColumnName:
:path=/static-assets/excel/content-code-examples/how-to/c-sharp-read-xlsx-file-datatable-header.csusing IronXL;
using System.Data;
// Convert worksheet to DataTable with the first row as column names
DataTable dt = workSheet.ToDataTable(true);IRON VB CONVERTER ERROR developers@ironsoftware.comThe Boolean parameter of ToDataTable() sets the first row as the column names of your DataTable. By default, its value is False. This is especially useful when working with structured data that includes headers.
How do I iterate through the DataTable?
:path=/static-assets/excel/content-code-examples/how-to/c-sharp-read-xlsx-file-print-datatable.csusing IronXL;
using System;
using System.Data;
// Load the Excel workbook
WorkBook workBook = WorkBook.Load("sample.xlsx");
// Get the specified WorkSheet
WorkSheet workSheet = workBook.GetWorkSheet("Sheet1");
// Convert WorkSheet to DataTable
DataTable dt = workSheet.ToDataTable(true); // Use first row as column names
// Iterate through rows and columns and display data
foreach (DataRow row in dt.Rows) // Access rows
{
for (int i = 0; i < dt.Columns.Count; i++) // Access columns of corresponding row
{
Console.Write(row[i] + " ");
}
Console.WriteLine();
}IRON VB CONVERTER ERROR developers@ironsoftware.comUsing the above code, every cell value of the WorkSheet can be accessed and used as required. For more advanced DataTable operations, see the guide on importing and exporting as DataSet.
How Do I Read Excel File as DataSet?
IronXL provides a simple function to use a complete Excel file (WorkBook) as a DataSet. Use the ToDataSet method to turn the whole workbook into DataSet. In this example, we will see how to use the Excel file as a DataSet.
How do I convert a workbook to DataSet?
:path=/static-assets/excel/content-code-examples/how-to/c-sharp-read-xlsx-file-excel-to-dataset.csusing IronXL;
using System;
using System.Data;
// Load the Excel workbook
WorkBook workBook = WorkBook.Load("sample.xlsx");
// Convert the WorkBook to a DataSet
DataSet ds = workBook.ToDataSet();
// Iterate through tables in the DataSet and display table names
foreach (DataTable dt in ds.Tables)
{
Console.WriteLine(dt.TableName);
}IRON VB CONVERTER ERROR developers@ironsoftware.comThe output of the above code looks like this:
And the Excel file Sample.xlsx looks like this:
How do I access each cell value across all worksheets?
In the above example, we can easily parse an Excel file into a DataSet and work with every WorkSheet of an Excel file as a DataTable. Dive deeper into how to parse Excel as a DataSet here featuring code examples.
Let's see one more example of how to access each cell value of all ExcelSheets. Here, we can access each cell value of every WorkSheet of an Excel file.
:path=/static-assets/excel/content-code-examples/how-to/c-sharp-read-xlsx-all-excel-sheets.cs// THIS CODE SNIPPET IS NOT AVAILABLE!' THIS CODE SNIPPET IS NOT AVAILABLE!Using the above example, it is convenient to access each cell value of every WorkSheet of an Excel file. This approach is particularly useful when dealing with multi-sheet workbooks where data is distributed across different tabs.
For more on how to Read Excel Files Without Interop check out the code here. The API reference documentation provides comprehensive details about all available methods and properties at the IronXL API Reference.
Tutorial Quick Access
API Reference for IronXL
Read more about IronXL's features, classes, method fields, namespaces, and enums in the documentation.
API Reference for IronXLFrequently Asked Questions
How do I read XLSX files in C#?
To read XLSX files in C#, use IronXL's WorkBook.Load method. Simply load the Excel file with IronXL.WorkBook.Load("your-file.xlsx"), then access worksheets to read cell data, perform calculations, and convert to DataTable or DataSet formats programmatically.
What file formats are supported when loading Excel files?
IronXL supports multiple Excel file formats including .xls, .xlsx, .xlsm, .xltx, and .csv files. The WorkBook.Load method automatically detects the file format, making it versatile for both legacy and modern Excel files.
How can I install the Excel reading library for my C# project?
You can install IronXL either through direct download or via NuGet Install for Visual Studio. The software is free for development purposes and provides a simple way to work with Excel file formats in C#.
What are the different ways to access a specific worksheet?
IronXL provides multiple ways to access worksheets: you can use workBook.GetWorkSheet("SheetName") to access by name, workBook.WorkSheets[0] to access by index, or workBook.WorkSheets.First() to get the first worksheet.
Can I perform calculations on the Excel data after loading it?
Yes, after loading an XLSX file with IronXL, you can apply various functions like Sum, Min, and Max on the data. The library provides full access to Excel file functionality through the WorkBook object.
Is it possible to convert Excel worksheet data to other formats?
Yes, IronXL allows you to read a WorkSheet and convert it to various formats including DataTable, DataSet, and more. This makes it easy to integrate Excel data into your C# applications.












