在C#與VB.NET應用程序中的Excel試算表文件
讀取和創建 Excel (XLS、XLSX 和 CSV)使用 Iron Software 的 IronXL 軟體庫輕鬆處理 C# 和所有其他 .NET 語言中的文件。
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、Mobile 和 Xamarin
安裝 IronXL
首先安裝 IronXL,使用我們的 NuGet 包或通过 下載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文件
要在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")
導出為 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")
設定儲存格和範圍的樣式
可以使用 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文件。
IronXL 不需要在您的服務器上安裝 Microsoft Excel 或 Excel Interop 即可訪問 Excel 文件。 這使得在 .NET 中處理 Excel 文件變得非常快速和簡單。
向前邁進
要更充分地利用IronXL,我們鼓勵您閱讀相關文檔。.NET API 參考資料以MSDN格式。