C# Excel教學:學習使用IronXL函式庫(無互通性)

C# 中讀取和寫入 Excel 檔案的指南

This article was translated from English: Does it need improvement?
Translated
View the article in English

使用 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

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);
}
$vbLabelText   $csharpLabel

建立新的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");
$vbLabelText   $csharpLabel

匯出格式為 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");
$vbLabelText   $csharpLabel

設定單元格和區域的樣式

您可以使用 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;
$vbLabelText   $csharpLabel

排序範圍

使用 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();
$vbLabelText   $csharpLabel

編輯公式

修改 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;
$vbLabelText   $csharpLabel

為什麼選擇 IronXL?

IronXL 為 .NET 中的開發者提供了一個對 Excel 文件讀寫友善的 API。 它無需在伺服器上安裝 Microsoft Excel 或 Excel Interop 即可運作,使 Excel 文件處理快速、輕巧且無麻煩。

展望未來

若要了解更多功能和功能,我們建議您查看格式與 MSDN 文件類似的.NET API 參考文件。

柯蒂斯·週
技術撰稿人

Curtis Chau擁有卡爾頓大學電腦科學學士學位,專長於前端開發,精通Node.js、TypeScript、JavaScript和React。他熱衷於打造直覺美觀的使用者介面,喜歡使用現代框架,並擅長撰寫結構清晰、視覺效果出色的使用者手冊。

除了開發工作之外,柯蒂斯對物聯網 (IoT) 也抱有濃厚的興趣,致力於探索硬體和軟體整合的創新方法。閒暇時,他喜歡玩遊戲和製作 Discord 機器人,將他對科技的熱愛與創造力結合。

準備好開始了嗎?
Nuget 下載 1,802,965 | 版本: 2025.12 剛剛發布