在 C# 和 VB.NET 應用程式中使用 Excel 試算表檔案

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

讀取和建立 Excel (XLS、XLSX 和 CSV) 使用 Iron Software 的 IronXL 軟體庫在 C# 和所有其他 .NET 語言中處理檔案非常簡單。

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、Mobile 和 Xamarin

安裝 IronXL

首先,使用我們的 NuGet 套件安裝 IronXL,或者通過 下載DLL. IronXL classes 可在 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
VB   C#

建立新的 Excel 文件

要在 C# 或 VB.NET 中建立 Excel 文件;IronXL 提供了一個簡單、快速的介面。

: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")
VB   C#

將數據匯出為CSV、XLS、XLSX、JSON或XML

我們還可以將數據保存或匯出為多種常見的結構化電子表格文件格式。

: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")
VB   C#

樣式儲存格和範圍

Excel 儲存格和範圍可以使用 IronXL.Range.Style 物件進行樣式設置。

: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
VB   C#

排序範圍

使用 IronXL,我們可以使用範圍對 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()
VB   C#

編輯公式

編輯 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
VB   C#

為什麼選擇 IronXL?

IronXL 提供簡單易用的 API 給開發者讀寫 .NET 的 Excel 文件。

IronXL 不需要安裝 Microsoft Excel 在您的伺服器上,也不需要使用 Excel Interop 來訪問 Excel 文件。這使得在 .NET 中處理 Excel 文件變得非常快速和簡單。

前進

為了更多瞭解 IronXL, 我們建議您閱讀內部文檔 .NET API 參考資料 以MSDN格式。