C# Read XLSX File
When working with various Excel formats, it often requires reading the data and manipulating it programmatically. In the following tutorial, we will learn how to read data from an Excel spreadsheet in C# using a convenient tool, IronXL.
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
Step 1
1. Get IronXL for Your 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
How to Tutorial
2. Load WorkBook
WorkBook is the class of IronXL whose object provides full access to the Excel file and its whole functions. For example, if we want to access an Excel file, we'd 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.comIn 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.
3. Access Specific WorkSheet
To access a specific WorkSheet of an Excel file, IronXL provides the WorkSheet class. It can be used in a few different ways:
: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.comAfter getting ExcelSheet workSheet, you can get any type of data from it and perform all Excel functions on it.
4. Access Data from WorkSheet
Data can be accessed from ExcelSheet workSheet with this process:
: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.comIt is also possible to 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 will display the values from cell A2 to A10.
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 will display the following result:
With the Excel file Sample.xlsx :
We can see how effortless it is to use Excel file data in your project using these methodologies.
5. Perform Functions on Data
It is very straightforward to access filtered data from an Excel WorkSheet by applying aggregate functions like Sum, Min, or Max using the following code:
: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.comIf you want more details, check out our in-depth tutorial on How to Write C# Excel Files with specifics on aggregate functions.
: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 will display the following output:
And this is how the Excel file Sample.xlsx will look:
6. Read Excel WorkSheet as DataTable
Using IronXL, it is very easy to operate with an Excel WorkSheet as a DataTable.
: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.comIf we want to use the first row of ExcelSheet as DataTable ColumnName then:
: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.comThus, the Boolean parameter of ToDataTable() sets the first row as the column names of your DataTable. By default, its value is False.
: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.
7. Read Excel File as DataSet
IronXL provides a very 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.
: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 will look like this:
And the Excel file Sample.xlsx will look like this:
In the above example, we see that we can easily parse an Excel file into a DataSet and employ it with every WorkSheet of an Excel file as a DataTable. Dive more 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 very convenient to access each cell value of every WorkSheet of an Excel file.
For more on how to Read Excel Files Without Interop check out the code here.
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 can I read an XLSX file in C# without using Interop?
To read an XLSX file in C# without using Interop, you can use the IronXL library. After installing IronXL, you can load an Excel workbook using WorkBook.Load() method and access its contents.
How do I load an Excel Workbook in C#?
You can load an Excel Workbook in C# using IronXL by calling WorkBook.Load('yourfile.xlsx'), which opens the specified file for manipulation.
What methods are available for accessing worksheets in an Excel file?
In IronXL, you can access worksheets using GetWorkSheet method by name or index, as well as properties like DefaultWorkSheet or First.
How can I manipulate data in an Excel worksheet using C#?
You can manipulate data in an Excel worksheet using IronXL by accessing cell values with syntax like ws['A1'].ToString() for strings or ws['B1'].Int32Value for integers, and applying functions such as Sum on cell ranges.
Can I perform aggregate operations on Excel data in C#?
Yes, IronXL allows performing aggregate operations on Excel data in C# using methods like Sum(), Min(), and Max() on specified cell ranges.
Is it possible to convert an Excel worksheet to a DataTable?
Yes, you can convert an Excel worksheet to a DataTable in C# using IronXL's ToDataTable() method, with an option to treat the first row as column names.
How can you use an Excel workbook as a DataSet in C#?
With IronXL, an Excel workbook can be utilized as a DataSet by calling the ToDataSet() method, enabling comprehensive data manipulation.
What are the advantages of using a library for Excel file manipulation in C#?
Using IronXL for Excel file manipulation in C# offers simplicity and efficiency, supporting various Excel formats, enabling operations on data, and providing features like converting worksheets to DataTables.
Where can I find additional resources for using IronXL?
You can find more resources and documentation on IronXL's API Reference page, which includes detailed information on its features, classes, and methods.










