C# 和 VB.NET 应用程序中的 # Excel 电子表格文件

读取和创建 Excel (XLS、XLSX 和 CSV) 使用 Iron Software 公司的 IronXL 软件库,在 C# 和所有其他 .NET 语言中创建文件变得非常容易。

IronXL 不需要在服务器或 Interop 上安装 Excel。IronXL 提供的 API 比 Microsoft.Office.Interop.Excel更快、更直观。

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

首先安装 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#

为单元格和区域设计样式

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 为.NET开发人员提供了读写 excel 文档的简易 API。

IronXL 无需在服务器上安装 Microsoft Excel 或 Excel Interop 即可访问 Excel 文档。这样,在.NET中处理Excel文件就变得非常简单快捷。

向前迈进

要从 IronXL 中获得更多信息,我们建议您阅读 IronXL 中的文档。 .NET API 参考 的 MSDN 格式。