A Guide to Reading and Writing Excel Files in C
透過 Iron Software 提供的 IronXL 程式庫,在 C# 及其他 .NET 語言中讀取與建立 Excel (XLS、XLSX 及 CSV) 檔案變得輕而易舉。
IronXL 無需在您的伺服器上安裝 Excel Interop。 IronXL 提供比 Microsoft.Office.Interop.Excel 更快且更直觀的 API。
IronXL 支援以下平台:
- 適用於 Windows 和 Azure 的 .NET Framework 4.6.2 及以上版本
- 適用於 Windows、Linux、macOS 及 Azure 的 .NET Core 2 及以上版本
- .NET 5、.NET 6、.NET 7、.NET 8、Mono、Maui 及 Xamarin
安裝 IronXL
Firstly install IronXL, using our NuGet package or by downloading the DLL. IronXL classes can be found in the IronXL namespace.
安裝 IronXL 最簡單的方法是使用 Visual Studio 的 NuGet 套件管理員: 套件名稱是 IronXl.Excel。
Install-Package IronXL.Excel
讀取 Excel 文件
透過 IronXL,僅需幾行程式碼即可從 Excel 檔案中擷取資料。
:path=/static-assets/excel/content-code-examples/get-started/get-started-1.cs
using IronXL;
// Supported spreadsheet formats for reading include: XLSX, XLS, CSV and TSV
WorkBook workBook = WorkBook.Load("data.xlsx");
WorkSheet workSheet = workBook.WorkSheets.First();
// Select cells easily in Excel notation and return the calculated value, date, text or formula
int cellValue = workSheet["A2"].IntValue;
// Read from Ranges of cells elegantly.
foreach (var cell in workSheet["A2:B10"])
{
Console.WriteLine("Cell {0} has value '{1}'", cell.AddressString, cell.Text);
}
Imports IronXL
' Supported spreadsheet formats for reading include: XLSX, XLS, CSV and TSV
Private workBook As WorkBook = WorkBook.Load("data.xlsx")
Private workSheet As WorkSheet = workBook.WorkSheets.First()
' Select cells easily in Excel notation and return the calculated value, date, text or formula
Private cellValue As Integer = workSheet("A2").IntValue
' Read from Ranges of cells elegantly.
For Each cell In workSheet("A2:B10")
Console.WriteLine("Cell {0} has value '{1}'", cell.AddressString, cell.Text)
Next cell
建立新的 Excel 文件
IronXL 提供一個快速且簡易的介面,可透過 C# 或 VB.NET 生成 Excel 文件。
:path=/static-assets/excel/content-code-examples/get-started/get-started-2.cs
using IronXL;
// Create new Excel WorkBook document.
WorkBook workBook = WorkBook.Create(ExcelFileFormat.XLSX);
workBook.Metadata.Author = "IronXL";
// Add a blank WorkSheet
WorkSheet workSheet = workBook.CreateWorkSheet("main_sheet");
// Add data and styles to the new worksheet
workSheet["A1"].Value = "Hello World";
workSheet["A2"].Style.BottomBorder.SetColor("#ff6600");
workSheet["A2"].Style.BottomBorder.Type = IronXL.Styles.BorderType.Double;
// Save the excel file
workBook.SaveAs("NewExcelFile.xlsx");
Imports IronXL
' Create new Excel WorkBook document.
Private workBook As WorkBook = WorkBook.Create(ExcelFileFormat.XLSX)
workBook.Metadata.Author = "IronXL"
' Add a blank WorkSheet
Dim workSheet As WorkSheet = workBook.CreateWorkSheet("main_sheet")
' Add data and styles to the new worksheet
workSheet("A1").Value = "Hello World"
workSheet("A2").Style.BottomBorder.SetColor("#ff6600")
workSheet("A2").Style.BottomBorder.Type = IronXL.Styles.BorderType.Double
' Save the excel file
workBook.SaveAs("NewExcelFile.xlsx")
匯出為 CSV、XLS、XLSX、JSON 或 XML
IronXL 還允許您將資料儲存或匯出為多種常見的結構化試算表格式。
:path=/static-assets/excel/content-code-examples/get-started/get-started-3.cs
// Export to many formats with fluent saving
workSheet.SaveAs("NewExcelFile.xls");
workSheet.SaveAs("NewExcelFile.xlsx");
workSheet.SaveAsCsv("NewExcelFile.csv");
workSheet.SaveAsJson("NewExcelFile.json");
workSheet.SaveAsXml("NewExcelFile.xml");
' Export to many formats with fluent saving
workSheet.SaveAs("NewExcelFile.xls")
workSheet.SaveAs("NewExcelFile.xlsx")
workSheet.SaveAsCsv("NewExcelFile.csv")
workSheet.SaveAsJson("NewExcelFile.json")
workSheet.SaveAsXml("NewExcelFile.xml")
設定儲存格與範圍的樣式
您可以使用 IronXl.Range.Style 物件對 Excel 儲存格和範圍套用格式。
:path=/static-assets/excel/content-code-examples/get-started/get-started-4.cs
// Set cell's value and styles
workSheet["A1"].Value = "Hello World";
workSheet["A2"].Style.BottomBorder.SetColor("#ff6600");
workSheet["A2"].Style.BottomBorder.Type = IronXL.Styles.BorderType.Double;
' Set cell's value and styles
workSheet("A1").Value = "Hello World"
workSheet("A2").Style.BottomBorder.SetColor("#ff6600")
workSheet("A2").Style.BottomBorder.Type = IronXL.Styles.BorderType.Double
排序範圍
Using IronXL,您可以利用 Range 物件輕鬆對 Excel 儲存格範圍進行排序。
:path=/static-assets/excel/content-code-examples/get-started/get-started-5.cs
using IronXL;
using Range = IronXL.Range;
WorkBook workBook = WorkBook.Load("test.xls");
WorkSheet workSheet = workBook.WorkSheets.First();
// This is how we get range from Excel worksheet
Range range = workSheet["A2:A8"];
// Sort the range in the sheet
range.SortAscending();
workBook.Save();
Imports IronXL
Dim workBook As WorkBook = WorkBook.Load("test.xls")
Dim workSheet As WorkSheet = workBook.WorkSheets.First()
' This is how we get range from Excel worksheet
Dim range As Range = workSheet("A2:A8")
' Sort the range in the sheet
range.SortAscending()
workBook.Save()
公式編輯
修改 Excel 公式就像賦予一個以"="符號開頭的值一樣簡單。 公式將立即進行計算。
:path=/static-assets/excel/content-code-examples/get-started/get-started-6.cs
// Set a formula
workSheet["A1"].Value = "=SUM(A2:A10)";
// Get the calculated value
decimal sum = workSheet["A1"].DecimalValue;
' Set a formula
workSheet("A1").Value = "=SUM(A2:A10)"
' Get the calculated value
Dim sum As Decimal = workSheet("A1").DecimalValue
為何選擇 IronXL?
IronXL 提供一個開發者友善的 API,用於在 .NET 環境中讀取和寫入 Excel 文件。 其運作無需在伺服器上安裝 Microsoft Excel 或 Excel Interop,使 Excel 檔案處理過程快速、輕量且無需繁瑣操作。
後續進行
若要進一步了解功能與特性,建議參閱格式與 MSDN 文件相似的 .NET API 參考指南。

