C# Write to Excel [Without Using Interop] Code Example Tutorial

Follow step-by-step examples of how to create, open, and save Excel files with C#, and apply basic operations like getting sum, average, count, and more. IronXL.Excel is a stand-alone .NET software library for reading a wide range of spreadsheet formats. It does not require Microsoft Excel to be installed, nor depends on Interop.


Overview

Use IronXL to Open and Write Excel Files

Open, write, save, and customize Excel files with the easy-to-use IronXL C# library.

Download a sample project from GitHub or use your own, and follow the tutorial.

  1. Install the 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

In this tutorial, we will walk you through:

  • Installing IronXL.Excel: how to install IronXL.Excel to an existing project.
  • Basic Operations: basic operation steps with Excel to Create or Open workbook, select sheet, select cell, and save the workbook.
  • Advanced Sheet Operations: how to utilize different manipulation capabilities like adding headers or footers, mathematical operations, and other features.

Open an Excel File: Quick Code

:path=/static-assets/excel/content-code-examples/tutorials/csharp-open-write-excel-file-1.cs
using IronXL;

WorkBook workBook = WorkBook.Load("test.xlsx");
WorkSheet workSheet = workBook.DefaultWorkSheet;
IronXL.Range range = workSheet["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 (workSheet["A11"].DecimalValue == total)
{
    Console.WriteLine("Basic Test Passed");
}
Imports IronXL

Private workBook As WorkBook = WorkBook.Load("test.xlsx")
Private workSheet As WorkSheet = workBook.DefaultWorkSheet
Private range As IronXL.Range = workSheet("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 workSheet("A11").DecimalValue = total Then
	Console.WriteLine("Basic Test Passed")
End If
$vbLabelText   $csharpLabel

Write and Save Changes to the Excel File: Quick Code

:path=/static-assets/excel/content-code-examples/tutorials/csharp-open-write-excel-file-2.cs
workSheet["B1"].Value = 11.54;

// Save Changes
workBook.SaveAs("test.xlsx");
workSheet("B1").Value = 11.54

' Save Changes
workBook.SaveAs("test.xlsx")
$vbLabelText   $csharpLabel

Step 1

1. Install the IronXL C# Library FREE

Start using IronXL in your project today with a free trial.

First Step:
green arrow pointer


IronXL.Excel provides a flexible and powerful library for opening, reading, editing, and saving Excel files in .NET. It can be installed and used on all of the .NET project types, like Windows applications, ASP.NET MVC, and .NET Core Application.

Install the Excel Library to your Visual Studio Project with NuGet

The first step will be to install IronXL.Excel. To add the IronXL.Excel library to the project, we have two ways: NuGet Package Manager or NuGet Package Manager Console.

To add IronXL.Excel library to our project using NuGet, we can do it using a visualized interface, NuGet Package Manager:

  1. Using mouse -> right-click on project name -> Select manage NuGet Package Manage NuGet Package
  2. From the browse tab -> search for IronXL.Excel -> Install Search for IronXL
  3. And we are done Complete Installation

Install Using NuGet Package Manager Console

  1. From tools -> NuGet Package Manager -> Package Manager Console Package Manager Console
  2. Run command
Install-Package IronXL.Excel

Install Package IronXL

Manually Install with the DLL

You may also choose to manually install the DLL to your project or to your global assembly cache.


How To Tutorials

2. Basic Operations: Create, Open, Save

2.1. Sample Project: HelloWorld Console Application

Create a HelloWorld Project in Visual Studio.

  1. Open Visual Studio Open Visual Studio
  2. Choose Create New Project Create New Project
  3. Choose Console App (.NET framework) Choose Console App
  4. Give the sample the name “HelloWorld” and click create Name the Project
  5. Now the console application is created Console Application Created
  6. Add IronXL.Excel to your project -> click install Add IronXL
  7. Add code to read the first cell in the first sheet from an Excel file and print it
using IronXL;

var workbook = WorkBook.Load("example.xlsx");
var sheet = workbook.DefaultWorkSheet;
Console.WriteLine(sheet["A1"].Text);
using IronXL;

var workbook = WorkBook.Load("example.xlsx");
var sheet = workbook.DefaultWorkSheet;
Console.WriteLine(sheet["A1"].Text);
Imports IronXL

Private workbook = WorkBook.Load("example.xlsx")
Private sheet = workbook.DefaultWorkSheet
Console.WriteLine(sheet("A1").Text)
$vbLabelText   $csharpLabel

...

Further Reading

To learn more about working with IronXL, you may wish to look at other tutorials within this section and also the examples on our homepage, which most developers find sufficient to get started. Our API Reference contains specific references to the WorkBook class.

Frequently Asked Questions

How can I open an Excel file in C# without using Interop?

You can use IronXL's WorkBook.Load method to open XLS, XLSX, or CSV files in C# without needing Microsoft Excel or Interop.

What are the steps to write data to an Excel file in C#?

To write data to an Excel file in C#, use IronXL to create a workbook and worksheet, set values in specific cells using worksheet["A1"].Value = "Your Value", and save the workbook with the SaveAs method.

How can I manipulate Excel sheets using IronXL?

With IronXL, you can add, rename, or delete sheets, set headers and footers, and perform mathematical calculations directly on the spreadsheet data.

Is it possible to read cell values from an Excel file using C#?

Yes, using IronXL, you can read cell values with syntax like sheet["A1"].Text to retrieve the text from a specific cell in an Excel file.

How do I install IronXL in a .NET project?

You can install IronXL in your .NET project using the NuGet Package Manager by searching for IronXL.Excel or by using the Package Manager Console command Install-Package IronXL.Excel.

Can IronXL be used in ASP.NET MVC projects?

Yes, IronXL is compatible with ASP.NET MVC projects, allowing you to handle Excel file operations within your web applications.

What file formats does IronXL support for Excel operations?

IronXL supports reading and writing Excel formats such as XLS, XLSX, and CSV, enabling flexible data handling in C# applications.

Where can I find code examples for using IronXL?

Code examples for using IronXL can be found in the tutorial on the IronXL website and in the sample projects available on GitHub.

What are the advantages of using IronXL for Excel file manipulation?

IronXL allows developers to manage Excel files in C# without needing Microsoft Excel or Interop, offering a straightforward API for creating, reading, and editing Excel documents.

Chaknith Bin
Software Engineer
Chaknith works on IronXL and IronBarcode. He has deep expertise in C# and .NET, helping improve the software and support customers. His insights from user interactions contribute to better products, documentation, and overall experience.