使用 C# 讀寫 Excel 文件指南
使用 Iron Software 的 IronXL 軟體庫,在 C# 和所有其他 .NET 語言中讀取和創建 Excel(XLS、XLSX 和 CSV)文件變得簡單。
IronXL 不需要在您的伺服器上安裝 Excel Interop。 IronXL 提供比 Microsoft.Office.Interop.Excel 更快且更直觀的 API。
IronXL支援以下平台:
- .NET Framework 4.6.2 及以上版本適用於 Windows 和 Azure
- .NET Core 2 以上版本適用於 Windows、Linux、MacOS 與 Azure
-
.NET 5、.NET 6、.NET 7、.NET 8、Mono、Maui 和 Xamarin
安裝 IronXL
首先,使用我們的NuGet套件安裝IronXL或下載DLL。 IronXL 類別可以在 IronXL
命名空間中找到。
安裝 IronXL 最簡單的方式是使用 Visual-Studio 的 NuGet 包管理器:
套件名稱是IronXL.Excel。
Install-Package IronXL.Excel
https://www.nuget.org/packages/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
排序範圍
使用IronXL,您可以使用Range物件輕鬆排序一範圍的Excel儲存格。
:path=/static-assets/excel/content-code-examples/get-started/get-started-5.cs
using IronXL; 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 Private workBook As WorkBook = WorkBook.Load("test.xls") Private workSheet As WorkSheet = workBook.WorkSheets.First() ' This is how we get range from Excel worksheet Private 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 檔案處理變得快速、輕量且無需煩惱。
向前邁進
若要探索更多功能和能力,我們建議查看.NET API 參考資料,格式類似於 MSDN 文檔。