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.
Quickstart: Create, Write & Save Excel in a Snap
Ready to generate Excel files in under a minute? This example uses IronXL to create a workbook, write a value to a cell, and save the file—all with minimal fuss and zero reliance on Interop. It's the fastest way to get started with Excel file operations in C#.
-
using NuGet 套件管理員安裝 https://www.nuget.org/packages/IronXL.Excel
PM > Install-Package IronXL.Excel -
請複製並執行此程式碼片段。
var workbook = IronXl.WorkBook.Create(IronXl.ExcelFileFormat.XLSX); workbook.CreateWorkSheet("Data")["A1"].Value = "Fast Start"; workbook.SaveAs("quick.xlsx"); -
部署至您的生產環境進行測試
立即透過免費試用,在您的專案中開始使用 IronXL
Minimal Workflow (5 steps)
- 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.ExcelLibrary from NuGet or the DLL download - Use the
WorkBook.Loadmethod 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 installIronXl.Excelto 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
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")
Step 1
1. Install the IronXl.Excel Library FREE
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

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.
- 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.Excelto 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.
常見問題
如何在不使用 Interop 的情況下,於 C# 中開啟 Excel 檔案?
您可以使用 IronXL 的 WorkBook.Load 方法,在 C# 中開啟 XLS、XLSX 或 CSV 檔案,無需 Microsoft Excel 或 Interop。
在 C# 中將資料寫入 Excel 檔案的步驟為何?
若要在 C# 中將資料寫入 Excel 檔案,請使用 IronXL 建立工作簿和工作表,並透過 worksheet["A1"].Value = "Your Value",並透過 SaveAs 方法儲存工作簿。
如何使用 IronXL 操作 Excel 試算表?
透過 IronXL,您可以新增、重新命名或刪除試算表,設定頁首與頁尾,並直接對試算表資料進行數學運算。
是否可以使用 C# 從 Excel 檔案讀取儲存格值?
是的,使用 IronXL,您可以透過類似 sheet["A1"].Text 來從 Excel 檔案中的特定儲存格擷取文字。
如何在 .NET 專案中安裝 IronXL?
您可透過 NuGet 套件管理員在 .NET 專案中安裝 IronXL,方法是搜尋 IronXL.Excel 或透過套件管理主控台指令 Install-Package IronXL.Excel.
IronXL 能否用於 ASP.NET MVC 專案?
是的,IronXL 與 ASP.NET MVC 專案相容,讓您能在網頁應用程式中處理 Excel 檔案操作。
IronXL 支援哪些 Excel 操作的檔案格式?
IronXL 支援讀寫 XLS、XLSX 和 CSV 等 Excel 格式,可在 C# 應用程式中實現靈活的資料處理。
哪裡可以找到 IronXL 的程式碼範例?
有關 using IronXL 的使用範例程式碼,可參閱 IronXL 網站上的教學指南,以及 GitHub 上的範例專案。
Using IronXL to perform Excel file manipulation has the following advantages:
IronXL 讓開發人員能夠在 C# 中管理 Excel 檔案,無需 Microsoft Excel 或 Interop,並提供直觀的 API 來建立、讀取及編輯 Excel 文件。

