在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支持以下平台:

  • .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
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#

设置单元格和范围的样式

可以使用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
VB   C#

排序范围

使用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()
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 为 .NET 开发者提供了一个简单的 API,用于读取和写入 Excel 文档。

IronXL无需在您的服务器上安装Microsoft Excel或使用Excel Interop即可访问Excel文档。 这使得在 .NET 中处理 Excel 文件变得非常快速和简单。

向前发展

要更好地利用 IronXL,我们建议您阅读文档。.NET API 参考的 MSDN 格式。