如何在 C# 中打开 Excel 文件并写入数据
本文将探讨 IronXL 库,以演示如何在 C# 控制台应用程序中打开 Microsoft Excel 文件并向其中写入数据。
IronXL - 一个 Excel 库
IronXL是一个 .NET Excel 库,它简化了在 C# 应用程序中创建、读取和编辑 Excel 文件的过程。 它具有卓越的性能和精准的输出。 该库支持所有Excel工作簿文件格式,包括XLS、XLSX、XLSM、CSV和TSV。此外,它还允许将数据保存或导出为JSON、HTML、二进制、字节数组、数据集或数据表等格式。
借助 IronXL,开发人员可以无缝地处理工作表和单元格区域。 它提供了在工作表中编辑公式并轻松重新计算公式的功能。 按范围、列或行对数据进行排序非常简单。 该库提供了修改布局的功能,例如冻结窗格、自动调整行/列大小以及添加/删除行/列。
IronXL 还支持使用用户密码和编辑权限来保护Excel 文件。 另一个值得注意的功能是能够从 Excel 工作表中添加、删除和提取图像。 该库提供各种 Excel 函数,支持多种单元格数据格式。 这些特性使 IronXL 成为处理 Excel 文件最用户友好的 API 之一。
IronXL 的一个显著优势是它不需要在计算机上安装 Microsoft Excel,从而消除了对 Office Interop 或任何其他依赖项的需求。 它兼容多个平台,支持 .NET 7、6 和 5。它还兼容 .NET Core 2 和 3,以及 .NET Framework 4.5 及更高版本,可用于处理 Excel 电子表格。
创建控制台应用程序
建议使用最新版本的 Visual Studio IDE 来创建应用程序。 Visual Studio 是 C# 开发的官方 IDE,假设您已经安装了它。 如果您还没有安装 Visual Studio,可以从Microsoft Visual Studio 官方网站下载。
按照以下步骤创建一个名为"DemoApp"的新项目。
- 打开 Visual Studio,然后单击"创建新项目"
如何在 C# 中打开 Excel 文件并写入数据,图 1:新建项目 新项目
- 选择"控制台应用程序",然后单击"下一步"。
如何在 C# 中打开 Excel 文件并写入数据,图 2:新建项目类型 新项目类型
- 输入项目名称
如何在 C# 中打开 Excel 文件并写入数据,图 3:新建项目名称 新项目名称
- 选择 .NET 版本。 选择稳定版本 .NET 6.0。
安装 IronXL 库
项目创建完成后,需要将 IronXL 库安装到项目中才能使用它。 按照以下步骤安装。
- 从解决方案资源管理器或工具中打开"管理解决方案的 NuGet 程序包"。
如何在 C# 中打开 Excel 文件并写入数据,图 5:NuGet 包管理器 NuGet包管理器
- 浏览 IronXL 库并选择当前项目。 点击安装。
如何在 C# 中打开 Excel 文件并写入数据,图 6:在 NuGet 包管理器 UI 中搜索并安装 IronXL 包 在 NuGet 包管理器 UI 中搜索并安装 IronXL 包
在Program.cs文件顶部添加以下命名空间
using IronXL;using IronXL;Imports IronXL在 C# 中打开现有的 Excel 文件
IronXL 提供了打开现有 Excel 文件的功能,您也可以创建新的 Excel 文件。本示例将使用 C# IronXL 打开一个现有文件。
// Supported spreadsheet formats for reading XLSX, XLS, XLSM, XLTX, CSV and TSV
WorkBook workBook = WorkBook.Load("sample.xlsx");// Supported spreadsheet formats for reading XLSX, XLS, XLSM, XLTX, CSV and TSV
WorkBook workBook = WorkBook.Load("sample.xlsx");' Supported spreadsheet formats for reading XLSX, XLS, XLSM, XLTX, CSV and TSV
Dim workBook As WorkBook = WorkBook.Load("sample.xlsx")现在,让我们选择它的第一个工作表。 您可以按索引号或名称选择工作表。 DefaultWorkSheet属性可以帮助获取第一个工作表。
// Select worksheet at index 0
WorkSheet workSheet = workBook.WorkSheets[0];
// Select worksheet by name
WorkSheet ws = workBook.GetWorkSheet("Sheet1");
// Get any existing worksheet
WorkSheet firstSheet = workBook.DefaultWorkSheet;// Select worksheet at index 0
WorkSheet workSheet = workBook.WorkSheets[0];
// Select worksheet by name
WorkSheet ws = workBook.GetWorkSheet("Sheet1");
// Get any existing worksheet
WorkSheet firstSheet = workBook.DefaultWorkSheet;' Select worksheet at index 0
Dim workSheet As WorkSheet = workBook.WorkSheets(0)
' Select worksheet by name
Dim ws As WorkSheet = workBook.GetWorkSheet("Sheet1")
' Get any existing worksheet
Dim firstSheet As WorkSheet = workBook.DefaultWorkSheet上面的代码从 Excel 工作簿中获取第一个工作表。 要创建包含数据的新 Excel 文件,请查看此代码示例页面。
现在,让我们使用 IronXL 对象库将数据写入 Excel 文件。
使用 C# 将数据写入 Excel 文件
使用 IronXL 将数据写入 Excel 文件非常简单。 实现此目的的方法有很多种,但最简单的方法是使用 Excel 单元格引用。
// Access A1 cell and write the value
workSheet["A1"].Value = "Value using cell reference";// Access A1 cell and write the value
workSheet["A1"].Value = "Value using cell reference";' Access A1 cell and write the value
workSheet("A1").Value = "Value using cell reference"也可以将数据写入一系列单元格。 以下代码将数据从单元格 B1 写入 B5。
// Write the same value to cells from B1 to B5
workSheet["B1:B5"].Value = "Range value";// Write the same value to cells from B1 to B5
workSheet["B1:B5"].Value = "Range value";' Write the same value to cells from B1 to B5
workSheet("B1:B5").Value = "Range value"我们还可以使用for循环来填充范围,使其成为动态的。 代码如下
// Specify range in which we want to write the values
for (int i = 1; i <= 5; i++)
{
// Write the Dynamic value in column C
workSheet["C" + i].Value = "Value: " + i;
// Write the Dynamic value in column D
workSheet["D" + i].Value = "Value: " + i;
}// Specify range in which we want to write the values
for (int i = 1; i <= 5; i++)
{
// Write the Dynamic value in column C
workSheet["C" + i].Value = "Value: " + i;
// Write the Dynamic value in column D
workSheet["D" + i].Value = "Value: " + i;
}' Specify range in which we want to write the values
For i As Integer = 1 To 5
' Write the Dynamic value in column C
workSheet("C" & i).Value = "Value: " & i
' Write the Dynamic value in column D
workSheet("D" & i).Value = "Value: " & i
Next i将数据写入 Excel 文件的另一种方法是使用Replace方法。
// Replace the value in cell D5
workSheet["D5"].Replace("Value: 5", "Replaced Value");// Replace the value in cell D5
workSheet["D5"].Replace("Value: 5", "Replaced Value");' Replace the value in cell D5
workSheet("D5").Replace("Value: 5", "Replaced Value")用 C# 保存 Excel 文件
本节介绍如何保存包含新写入内容的 Excel 文件。
// Save the updated Excel workbook to a file
workBook.SaveAs("sample.xlsx");// Save the updated Excel workbook to a file
workBook.SaveAs("sample.xlsx");' Save the updated Excel workbook to a file
workBook.SaveAs("sample.xlsx")完整代码如下:
using System;
using IronXL;
class Program
{
static void Main(string[] args)
{
// Supported spreadsheet formats for reading XLSX, XLS, XLSM, XLTX, CSV and TSV
WorkBook workBook = WorkBook.Load("sample.xlsx");
// Select worksheet at index 0
WorkSheet workSheet = workBook.WorkSheets[0];
// Access A1 cell and write the value
workSheet["A1"].Value = "Value using cell reference";
// Write the same value to cells from B1 to B5
workSheet["B1:B5"].Value = "Range value";
// Specify range in which we want to write the values
for (int i = 1; i <= 5; i++)
{
// Write the Dynamic value in column C
workSheet["C" + i].Value = "Value: " + i;
// Write the Dynamic value in column D
workSheet["D" + i].Value = "Value: " + i;
}
// Replace the value in cell D5
workSheet["D5"].Replace("Value: 5", "Replaced Value");
// Save the updated Excel workbook to a file
workBook.SaveAs("sample.xlsx");
Console.WriteLine("Successfully written to Excel File");
}
}using System;
using IronXL;
class Program
{
static void Main(string[] args)
{
// Supported spreadsheet formats for reading XLSX, XLS, XLSM, XLTX, CSV and TSV
WorkBook workBook = WorkBook.Load("sample.xlsx");
// Select worksheet at index 0
WorkSheet workSheet = workBook.WorkSheets[0];
// Access A1 cell and write the value
workSheet["A1"].Value = "Value using cell reference";
// Write the same value to cells from B1 to B5
workSheet["B1:B5"].Value = "Range value";
// Specify range in which we want to write the values
for (int i = 1; i <= 5; i++)
{
// Write the Dynamic value in column C
workSheet["C" + i].Value = "Value: " + i;
// Write the Dynamic value in column D
workSheet["D" + i].Value = "Value: " + i;
}
// Replace the value in cell D5
workSheet["D5"].Replace("Value: 5", "Replaced Value");
// Save the updated Excel workbook to a file
workBook.SaveAs("sample.xlsx");
Console.WriteLine("Successfully written to Excel File");
}
}Imports System
Imports IronXL
Friend Class Program
Shared Sub Main(ByVal args() As String)
' Supported spreadsheet formats for reading XLSX, XLS, XLSM, XLTX, CSV and TSV
Dim workBook As WorkBook = WorkBook.Load("sample.xlsx")
' Select worksheet at index 0
Dim workSheet As WorkSheet = workBook.WorkSheets(0)
' Access A1 cell and write the value
workSheet("A1").Value = "Value using cell reference"
' Write the same value to cells from B1 to B5
workSheet("B1:B5").Value = "Range value"
' Specify range in which we want to write the values
For i As Integer = 1 To 5
' Write the Dynamic value in column C
workSheet("C" & i).Value = "Value: " & i
' Write the Dynamic value in column D
workSheet("D" & i).Value = "Value: " & i
Next i
' Replace the value in cell D5
workSheet("D5").Replace("Value: 5", "Replaced Value")
' Save the updated Excel workbook to a file
workBook.SaveAs("sample.xlsx")
Console.WriteLine("Successfully written to Excel File")
End Sub
End Class有关如何在 C# 中读取 Excel 文件数据的更多详细信息,请参阅此示例。
输出
文件的输出结果为:
如何在 C# 中打开 Excel 文件并写入数据,图 7:输出的 Excel 文件 输出 Excel 文件
摘要
本文演示了如何使用 IronXL 在 C# 中将数据写入 Excel 文件。 IronXL 提供了处理现有 Excel 文件的便利。 它还允许您创建新的 Excel 文件,并使用简单的语法向其中写入数据。 即使没有安装 Microsoft Excel 应用程序,IronXL 也可以读取 Excel 文件。 要从 Excel 文件中读取数据,您可以查看此代码示例页面。
IronXL 免费供开发使用,并可获得商业用途许可证。 您也可以尝试 IronXL 的商业用途免费试用版。
常见问题解答
如何在C#中打开Excel文件而不使用Interop?
您可以通过使用IronXL库在C#中打开Excel文件而不使用Interop。IronXL允许您高效地处理Excel文件,而无需Microsoft Excel安装,提升性能和兼容性。
如何在C#中使用Excel文件的特定单元格写入数据?
使用IronXL,您可以通过访问工作簿,选择工作表,然后使用单元格引用为所需单元格分配值,在Excel文件中写入数据。
使用IronXL在Excel文件操作中有什么优于Office Interop的优势?
IronXL相较Office Interop有多个优势,包括不依赖于Microsoft Excel安装,改进的性能,跨平台兼容性,以及支持多种Excel文件格式如XLS、XLSX和CSV。
我可以使用这个库编辑Excel文件中的公式吗?
是的,IronXL允许您编辑Excel文件中的公式。您可以使用简单的语法操作现有公式或插入新公式,库会高效地处理这些操作。
IronXL 如何支持不同的 Excel 文件格式?
IronXL支持多种Excel文件格式,如XLS、XLSX、XLSM、CSV和TSV,使您能够在C#应用程序中无缝创建、读取和编辑这些文件。
可以使用IronXL保护Excel文件吗?
是的,IronXL提供了通过设置用户密码和权限来保护Excel文件的功能,以确保数据安全和受控访问。
如何将IronXL库集成到我的C#项目中?
要将IronXL集成到您的C#项目中,您可以在Visual Studio中使用NuGet包管理器。搜索IronXL,并将其添加到您的项目中以开始使用其功能。
IronXL 支持哪些平台?
IronXL支持多种平台,包括.NET 5、6 和 7 版,以及.NET Core 和框架,使之成为各种开发环境的多功能选择。
我可以免费使用IronXL吗?
IronXL可在开发中免费使用。然而,商业用途需要许可证。提供免费试用以在商业环境中测试库的功能。
如何使用IronXL将Excel数据导出为JSON?
IronXL允许您通过将工作表或特定数据范围转换为JSON字符串将Excel数据导出为JSON格式,然后可以在需要JSON数据的应用程序中使用。








