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.
How to Write an Excel File in C#
- Download the Write Excel C# Library
- Create and open a new CSV or XML Excel file as an Excel workbook
- Save and export your Excel workbook
- Apply Advanced Operations in your multiple Excel worksheets
- Integrate with Excel Database
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.
- Install the IronXL Excel Library from NuGet or the DLL download
- Use the
WorkBook.Load
method to read any XLS, XLSX, or CSV document. - 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;
// Load the workbook from an Excel file.
WorkBook workBook = WorkBook.Load("test.xlsx");
// Get the default worksheet from the workbook.
WorkSheet workSheet = workBook.DefaultWorkSheet;
// Define a range of cells from A2 to A8 in the worksheet.
IronXL.Range range = workSheet["A2:A8"];
// Initialize a variable to keep track of the total sum of numeric values in the range.
decimal total = 0;
// Iterate over each cell in the defined range.
foreach (var cell in range)
{
// Print out the row index and value of each cell.
Console.WriteLine("Cell {0} has value '{1}'", cell.RowIndex, cell.Text);
// Check if the cell contains a numeric value.
if (cell.IsNumeric)
{
// Add the cell's numeric value to the total.
// Using DecimalValue to avoid floating point precision issues.
total += cell.DecimalValue;
}
}
// Check if a cell (A11) contains the same total as calculated from the range.
// This line assumes A11 is supposed to have a sum formula or manually set value that equals the sum of A2:A8.
// Ensure A11 has a numeric value before accessing its DecimalValue.
if (workSheet["A11"].IsNumeric && workSheet["A11"].DecimalValue == total)
{
// Print a success message if the calculated total matches the value in A11.
Console.WriteLine("Basic Test Passed");
}
Imports IronXL
' Load the workbook from an Excel file.
Private workBook As WorkBook = WorkBook.Load("test.xlsx")
' Get the default worksheet from the workbook.
Private workSheet As WorkSheet = workBook.DefaultWorkSheet
' Define a range of cells from A2 to A8 in the worksheet.
Private range As IronXL.Range = workSheet("A2:A8")
' Initialize a variable to keep track of the total sum of numeric values in the range.
Private total As Decimal = 0
' Iterate over each cell in the defined range.
For Each cell In range
' Print out the row index and value of each cell.
Console.WriteLine("Cell {0} has value '{1}'", cell.RowIndex, cell.Text)
' Check if the cell contains a numeric value.
If cell.IsNumeric Then
' Add the cell's numeric value to the total.
' Using DecimalValue to avoid floating point precision issues.
total += cell.DecimalValue
End If
Next cell
' Check if a cell (A11) contains the same total as calculated from the range.
' This line assumes A11 is supposed to have a sum formula or manually set value that equals the sum of A2:A8.
' Ensure A11 has a numeric value before accessing its DecimalValue.
If workSheet("A11").IsNumeric AndAlso workSheet("A11").DecimalValue = total Then
' Print a success message if the calculated total matches the value in A11.
Console.WriteLine("Basic Test Passed")
End If
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
// The following code snippet demonstrates how to set a cell value in an Excel worksheet
// and then save the workbook as a new file named "test.xlsx".
// Ensure you have the necessary library references and using directives
// for interacting with Excel files, such as using a library like EPPlus or ClosedXML.
// Assuming 'workSheet' is an initialized variable representing the worksheet where we want to set the value.
// Example: var workSheet = package.Workbook.Worksheets.Add("Sheet1");
// Assuming 'workBook' is an initialized variable representing the workbook that contains the worksheet.
// Example: var workBook = new ExcelPackage().Workbook;
// Set the value of cell B1 in the worksheet to 11.54
workSheet.Cells["B1"].Value = 11.54;
// Save the current workbook to a new file named "test.xlsx"
workBook.SaveAs(new FileInfo("test.xlsx"));
// Note: Ensure to handle any necessary disposal of resources and exception handling as per your application's need.
' The following code snippet demonstrates how to set a cell value in an Excel worksheet
' and then save the workbook as a new file named "test.xlsx".
' Ensure you have the necessary library references and using directives
' for interacting with Excel files, such as using a library like EPPlus or ClosedXML.
' Assuming 'workSheet' is an initialized variable representing the worksheet where we want to set the value.
' Example: var workSheet = package.Workbook.Worksheets.Add("Sheet1");
' Assuming 'workBook' is an initialized variable representing the workbook that contains the worksheet.
' Example: var workBook = new ExcelPackage().Workbook;
' Set the value of cell B1 in the worksheet to 11.54
workSheet.Cells("B1").Value = 11.54
' Save the current workbook to a new file named "test.xlsx"
workBook.SaveAs(New FileInfo("test.xlsx"))
' Note: Ensure to handle any necessary disposal of resources and exception handling as per your application's need.
Step 1
1. Install the IronXL C# Library FREE
Start using IronXL in your project today with a free trial.
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:
- Using mouse -> right-click on project name -> Select manage NuGet Package
- From the browse tab -> search for IronXL.Excel -> Install
- And we are done
Install Using NuGet Package Manager Console
- From tools -> NuGet Package Manager -> Package Manager Console
- Run command
Install-Package IronXL.Excel -Version 2019.5.2
Manually Install with the DLL
You may also choose to manually install the DLL to your project or to your global assembly cache.
Install-Package IronXL.Excel
How To Tutorials
2. Basic Operations: Create, Open, Save
2.1. Sample Project: HelloWorld Console Application
Create a HelloWorld Project in Visual Studio.
- Open Visual Studio
- Choose Create New Project
- Choose Console App (.NET framework)
- Give the sample the name “HelloWorld” and click create
- Now the console application is created
- Add IronXL.Excel to your project -> click install
- 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)
...
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
What is IronXL?
IronXL is a stand-alone .NET software library for reading and writing a wide range of spreadsheet formats without requiring Microsoft Excel or Interop.
How can I write to an Excel file in C# using IronXL?
To write to an Excel file using IronXL, you need to first create a workbook and worksheet, then set values in specific cells, and finally save the workbook using the SaveAs method.
Do I need Microsoft Excel to use IronXL?
No, IronXL does not require Microsoft Excel to be installed on your machine as it operates independently.
How do I install IronXL in a Visual Studio project?
IronXL can be installed in a Visual Studio project using NuGet Package Manager or the Package Manager Console by searching for IronXL.Excel and installing it.
What types of Excel files can IronXL handle?
IronXL can read and write XLS, XLSX, and CSV document formats.
How do I access cell values in an Excel sheet using IronXL?
You can access cell values using intuitive syntax like sheet["A1"].Text to get the text from cell A1.
What are the basic operations I can perform with IronXL?
With IronXL, you can create, open, edit, and save Excel files, perform basic mathematical operations, and manipulate sheets such as adding headers and footers.
Can I use IronXL in different .NET project types?
Yes, IronXL can be used in various .NET project types, including Windows applications, ASP.NET MVC, and .NET Core applications.
Is there a sample project available for IronXL?
Yes, a sample project is available on GitHub, which can help you get started quickly with IronXL by providing example code.
Where can I find the API reference for IronXL?
The API reference for IronXL is available on the IronSoftware website, detailing all features, namespaces, classes, methods, fields, and enums.