For many, IronXL for .Net is the most efficient way to open, edit and export XLSX files in C#

C# Excel XLSX

27th May 2019 by Elijah Williams

Use C# to Open & Write an Excel File

Use C# to Open & Write an Excel File in .Net

How to open an Excel file in C#

  1. Install IronXL Excel Library from NuGet or the DLL download
  2. Use the WorkBook.Load method to read any XLS, XLSX or CSV document.
  3. Get Cell values using intuitive syntax: sheet["A11"].DecimalValue

Install the Excel Library to your Visual Studio Project

IronXL.Excel provides a versatile, advanced, and efficient library for opening, reading, editing and saving Excel files in .NET.

The first step will be to install IronXL.Excel, and this is most easily achieved using our NuGet package, although you may also choose to manually install the DLL to your project or to your global assembly cache.

 PM > Install-Package  IronXL.Excel

Open an Excel File

using IronXL;
using System;

WorkBook workbook = WorkBook.Load("test.xlsx");
WorkSheet sheet = workbook.DefaultWorkSheet;

Range range = sheet["A2:A8"];

decimal total = 0;

//iterate over range of cells
foreach (var cell in range)
{
    Console.WriteLine("Cell {0} has value '{1}'", cell.RowIndex, cell.Value);

    if (cell.IsNumeric)
    {
        //Get decimal value to avoid floating numbers precision issue
        total += cell.DecimalValue;
    }
}

//check formula evaluation
if (sheet["A11"].DecimalValue == total)
{
    Console.WriteLine("Basic Test Passed");
}
Imports IronXL
Imports System

Private workbook As WorkBook = WorkBook.Load("test.xlsx")
Private sheet As WorkSheet = workbook.DefaultWorkSheet

Private range As Range = sheet("A2:A8")

Private total As Decimal = 0

'iterate over range of cells
For Each cell In range
	Console.WriteLine("Cell {0} has value '{1}'", cell.RowIndex, cell.Value)

	If cell.IsNumeric Then
		'Get decimal value to avoid floating numbers precision issue
		total += cell.DecimalValue
	End If
Next cell

'check formula evaluation
If sheet("A11").DecimalValue = total Then
	Console.WriteLine("Basic Test Passed")
End If
VB   C#

Write and Save Changes to the Excel File

sheet["B1"].Value = 11.54;

//Save Changes
workbook.SaveAs("test.xlsx");
sheet("B1").Value = 11.54

'Save Changes
workbook.SaveAs("test.xlsx")
VB   C#

Summary

In summary, IronXL.Excel is a stand alone .Net software library library for reading a wide range of spreadsheet formats. It does not require Microsoft Excel to be installed, nor depend on Interop.

Further Reading

To learn more about working with IronXL, you may wish to look at the other tutorials within this section, and also the examples on our homepage; which most developers find enough to get them started.

Our Object Reference with specific reference to the WorkBook class.

The .Net Excel library can revolutionise bring data into and out of  web applications and business systems.

Elijah Williams

Product Development Engineer

Elijah is an engineer on a Development and Testing team responsible for a large corporate financial reporting system. Elijah has been an early adopter of the IronXL Library, featuring it in the core of their Excel reporting interface.