How to Open Excel File in C#
This tutorial will use IronXL as a main tool to open and read Excel files in C# projects.
IronXL Excel Library
IronXL is a .NET library that prioritizes ease of use, accuracy, and speed for its users. It helps you open, read, create, and edit Excel files with lightning-fast performance and without any errors. It works without MS Office Interop, which makes it a powerful tool for developers.
IronXL is compatible with all .NET Frameworks along with Linux, MacOS, Docker, Azure, and AWS. It can be used to create Console, Web, and Desktop Applications such as Blazor and MAUI for modern Web Apps. It supports different workbook formats like XLS and XLSX files, XSLT and XLSM, CSV, and TSV.
Some Important IronXL Features
- Open, read, and search data in different formats (XLS/XLSX file/CSV/TSV).
- Export Excel Worksheets to XLS/XLSX/CSV/TSV/JSON.
- Encrypting and decrypting XLSX/XLSM/XLTX files with passwords.
- Work with Excel sheets as
System.Data.DataSet
andSystem.Data.DataTable
objects. - Excel file formulas are recalculated automatically each time a sheet is edited.
- Easy spreadsheet data editing with an intuitive cell-range-based syntax (e.g.,
WorkSheet["A1:B10"]
). - Sort Cell Ranges, Columns, and Rows.
- Styling Cells - Font, Font Size, Background color, Border, Alignment, and Numbering formats.
How to open an Excel file in C#?
Prerequisites
To use IronXL in C# applications, install the following components on your local computer:
- Visual Studio - It is the official IDE for developing C# .NET applications. You can download and install Visual Studio from the Microsoft website. You can also use Jetbrains ReSharper & Rider.
- IronXL - It is the Excel library that helps to work with Excel sheets at a given path in C#. It must be installed in your C# application before using it. You can download it from the NuGet website or from Manage NuGet packages in Visual Studio tools. You can also download the .NET Excel DLL file directly.
Adding Necessary Namespaces
Once Visual Studio and IronXL are installed, add the necessary IronXL namespaces by including the following line at the top of your C# file:
// Add reference to the IronXL library
using IronXL;
// Add reference to the IronXL library
using IronXL;
' Add reference to the IronXL library
Imports IronXL
Open an Existing Excel file in C#
Excel files, also known as workbooks, consist of multiple worksheets, each containing cell values. To open and read an Excel file, load it using the WorkBook
class's Load
method.
// Supported Excel spreadsheet formats for reading include: XLSX, XLS, CSV, and TSV
WorkBook workbook = WorkBook.Load("test.xlsx");
// Supported Excel spreadsheet formats for reading include: XLSX, XLS, CSV, and TSV
WorkBook workbook = WorkBook.Load("test.xlsx");
' Supported Excel spreadsheet formats for reading include: XLSX, XLS, CSV, and TSV
Dim workbook As WorkBook = WorkBook.Load("test.xlsx")
This initializes the workbook as a WorkBook
instance. To open a specific WorkSheet
, retrieve it from the WorkSheets
collection:
// Access the first worksheet in the workbook
WorkSheet sheet = workbook.WorkSheets.First();
// Access the first worksheet in the workbook
WorkSheet sheet = workbook.WorkSheets.First();
' Access the first worksheet in the workbook
Dim sheet As WorkSheet = workbook.WorkSheets.First()
This accesses the first sheet in the Excel file, ready for reading and writing.
Excel File
Read Excel Files in C#
Once the Excel file is opened, it is ready for reading data. Reading data from Excel files in C# using IronXL is straightforward. You can read cell values by specifying the cell reference.
The following code retrieves the value of a cell:
// Select the cell using Excel notation and retrieve its integer value
int cellValue = sheet["C2"].IntValue;
// Display the value in the console
Console.WriteLine(cellValue);
// Select the cell using Excel notation and retrieve its integer value
int cellValue = sheet["C2"].IntValue;
// Display the value in the console
Console.WriteLine(cellValue);
' Select the cell using Excel notation and retrieve its integer value
Dim cellValue As Integer = sheet("C2").IntValue
' Display the value in the console
Console.WriteLine(cellValue)
The output is as follows:
Read Excel
To read data from a range of cells, use a loop to iterate through the specified range:
// Iterate through a range of cells and display their address and text content
foreach (var cell in sheet["A2:A6"])
{
Console.WriteLine("Cell {0} has value '{1}'", cell.AddressString, cell.Text);
}
// Iterate through a range of cells and display their address and text content
foreach (var cell in sheet["A2:A6"])
{
Console.WriteLine("Cell {0} has value '{1}'", cell.AddressString, cell.Text);
}
' Iterate through a range of cells and display their address and text content
For Each cell In sheet("A2:A6")
Console.WriteLine("Cell {0} has value '{1}'", cell.AddressString, cell.Text)
Next cell
Each value in the cell range A2:A6
is accessed and printed to the console.
Read Range of Cells
For more detailed reading and writing examples, check the Excel reading tutorial in C#.
Create a New Workbook
IronXL also facilitates creating new workbooks for data saving and retrieving.
You can create a new Excel file with a single line of code:
// Create a new workbook with the XLSX format
WorkBook workBook = new WorkBook(ExcelFileFormat.XLSX);
// Create a new workbook with the XLSX format
WorkBook workBook = new WorkBook(ExcelFileFormat.XLSX);
' Create a new workbook with the XLSX format
Dim workBook As New WorkBook(ExcelFileFormat.XLSX)
Next, create a worksheet and add data to it.
Create a New Worksheet
// Create a worksheet named "GDPByCountry" in the workbook
WorkSheet workSheet = workBook.CreateWorkSheet("GDPByCountry");
// Create a worksheet named "GDPByCountry" in the workbook
WorkSheet workSheet = workBook.CreateWorkSheet("GDPByCountry");
' Create a worksheet named "GDPByCountry" in the workbook
Dim workSheet As WorkSheet = workBook.CreateWorkSheet("GDPByCountry")
This code adds a worksheet named "GDPByCountry" to the workbook, allowing you to add cell values.
To set a value for a specific cell, use the code below:
// Set the value of cell A1 to "Example"
workSheet["A1"].Value = "Example";
// Set the value of cell A1 to "Example"
workSheet["A1"].Value = "Example";
' Set the value of cell A1 to "Example"
workSheet("A1").Value = "Example"
The final output is:
Add Value to Cell
Summary
This article demonstrates how to open and read Excel files, such as XLS and XLSX, in C# using IronXL. IronXL doesn't require Microsoft Excel to be installed on the system for Excel-related tasks.
IronXL provides a comprehensive solution for Excel-related tasks programmatically, including formula calculation, string sorting, trimming, finding and replacing, merging and unmerging, saving files, and more. You can also set cell data formats.
IronXL is available for a free 30-day trial and can be licensed for commercial use. IronXL's Lite package starts from $749.
Frequently Asked Questions
How can I open Excel files in C# without using Interop?
You can open Excel files in C# without using Interop by utilizing the IronXL library. Use the WorkBook.Load
method to load an Excel file into a WorkBook
instance, which allows you to access and manipulate data within the file.
What file formats are compatible with this C# Excel library?
IronXL supports a variety of Excel file formats including XLS, XLSX, CSV, and TSV. This allows developers to open, read, and write these formats flexibly within their C# applications.
Can I edit Excel files in C# using this library?
Yes, you can edit Excel files using IronXL. After loading a workbook, you can modify data, add new worksheets, and then save changes back to the file or export it in various formats.
How do I install this library for use in my C# project?
To install IronXL in your C# project, you can use NuGet Package Manager in Visual Studio to add the library. Alternatively, you can download the .NET Excel DLL and reference it in your project.
Is it possible to encrypt Excel files using this library?
Yes, IronXL allows you to encrypt and decrypt Excel files. You can secure your Excel documents with passwords to protect sensitive data during file operations.
Does this library support formula recalculations in Excel sheets?
IronXL supports automatic formula recalculations, ensuring that any changes to the data automatically update the formulas, just like in Excel.
How can I read specific cell values in an Excel worksheet using this library?
To read specific cell values using IronXL, you can reference the cell using Excel notation. For example, sheet["A1"].StringValue
will retrieve the string value from cell A1.
Can this library be used across different operating systems?
Yes, IronXL is compatible with multiple operating systems, including Windows, Linux, and macOS. It also supports deployment in Docker, Azure, and AWS environments.
What are the advantages of using this library over MS Office Interop?
IronXL offers several advantages over MS Office Interop, such as not requiring Excel to be installed on the system, better performance in server environments, and ease of use with modern .NET applications.
Is there a free trial available for this C# Excel library?
Yes, IronXL provides a 30-day free trial, allowing you to test its features and capabilities before deciding on a commercial license for your projects.