C# 中读取和写入 Excel 文件的指南
使用 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.csusing 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.csusing 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.csusing 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 为 .NET 中的开发者提供了一个对 Excel 文档读写友好的 API。 它无需在服务器上安装 Microsoft Excel 或 Excel Interop 即可工作,使 Excel 文件处理快速、轻便且无麻烦。
前行
要了解更多特性和功能,我们建议您查看格式与 MSDN 文档类似的.NET API 参考文档。






