使用IRONXL

在C#中生成Excel文件

更新 2023年十二月20日
分享:

我们开发的应用程序经常要与 Excel 电子表格通信,以获取数据进行评估和得出结果。能用 C# 编程生成 Excel 文件确实很有帮助,可以节省我们的开发时间和精力。在本教程中,我们将学习如何使用高效的 C# 函数编程生成不同格式的 Excel 文件、设置单元格样式和插入数据。


步骤 1

1.使用 IronXL 生成 Excel 文件

使用 IronXL Excel for C# 库生成 Excel 文件,该库提供了一系列用于在项目中生成和处理数据的函数。该库的开发是免费的,当你准备好正式发布时,还可以获得许可证。要学习本教程,您可以 下载 IronXL 生成 或通过 Visual Studio 和 NuGet 图库.


Install-Package IronXL.Excel

教程

2.C# Excel 文件生成器概述

在商业应用程序开发中,我们经常需要以编程方式生成不同类型的 Excel 文件。为此,我们需要最简单快捷的方法来生成不同类型的文件并自动保存到所需位置。

安装 IronXL 后,我们可以使用以下功能 生成不同类型的 Excel 文件:

  • 扩展名为 .xlsx 的 Excel 文件。
  • 扩展名为.xls的 Excel 文件。
  • 逗号分隔值 (.csv) 文件
  • 制表符分隔值 (.tsv) 文件
  • JavaScript 对象符号 (.json) 文件
  • 可扩展标记语言 (.xml) 文件

要生成任何类型的文件,我们首先需要创建一个 Excel 工作簿.

//generate New WorkBook
WorkBook wb = WorkBook.Create();
//generate New WorkBook
WorkBook wb = WorkBook.Create();
'generate New WorkBook
Dim wb As WorkBook = WorkBook.Create()
VB   C#

上面的代码行将创建一个新的 WorkBook``wb ,现在我们将创建一个 工作表 反对

//Generate New WorkSheet
WorkSheet ws = WorkBook.CreateWorkSheet("SheetName");
//Generate New WorkSheet
WorkSheet ws = WorkBook.CreateWorkSheet("SheetName");
'Generate New WorkSheet
Dim ws As WorkSheet = WorkBook.CreateWorkSheet("SheetName")
VB   C#

这将创建一个 WorkSheet``ws ,我们可以用它在 Excel 文件中插入数据。


3.生成 XLSX 文件 C#

首先,我们按照上述步骤生成 WorkBookWorkSheet

然后,我们在其中插入数据,创建我们的.xlsx扩展文件。为此,IronXL 提供了单元格地址系统,允许我们以编程方式在特定单元格地址中插入数据。

//Insert data by cell addressing
WorkSheet ["CellAddress"].Value = "MyValue";
//Insert data by cell addressing
WorkSheet ["CellAddress"].Value = "MyValue";
'Insert data by cell addressing
WorkSheet ("CellAddress").Value = "MyValue"
VB   C#

它将在特定的单元格地址中插入一个名为 "MyValue "的新值。同样,我们可以在任意多个单元格中插入数据。之后,我们将把 Excel 文件保存到指定路径,如下所示:

//specify file path and name
WorkBook.SaveAs("Path + FileName.xlsx");
//specify file path and name
WorkBook.SaveAs("Path + FileName.xlsx");
'specify file path and name
WorkBook.SaveAs("Path + FileName.xlsx")
VB   C#

这将在指定路径下创建扩展名为.xlsx的新 Excel 文件。保存文件时,不要忘记在文件名中写入扩展名 .xlsx

进一步了解如何 在 C# 项目中创建 Excel 工作簿请点击这里查看代码示例。

/**
Generate XLSX File
anchor-generate-xlsx-file-c-num
**/
using IronXL;
static void Main(string [] args)
{
    //create new WorkBook of .xlsx Extension
    WorkBook wb = WorkBook.Create(ExcelFileFormat.XLSX);
    //create workSheet
    WorkSheet ws = wb.CreateWorkSheet("Sheet1");
    //insert data in the cells of WorkSheet
    ws ["A1"].Value = "Hello";
    ws ["A2"].Value = "World";
    ws ["C4"].Value = "IronXL";
    //save the file as .xlsx
    wb.SaveAs("sample.xlsx");
    Console.WriteLine("successfully created.");
    Console.ReadKey();
}
/**
Generate XLSX File
anchor-generate-xlsx-file-c-num
**/
using IronXL;
static void Main(string [] args)
{
    //create new WorkBook of .xlsx Extension
    WorkBook wb = WorkBook.Create(ExcelFileFormat.XLSX);
    //create workSheet
    WorkSheet ws = wb.CreateWorkSheet("Sheet1");
    //insert data in the cells of WorkSheet
    ws ["A1"].Value = "Hello";
    ws ["A2"].Value = "World";
    ws ["C4"].Value = "IronXL";
    //save the file as .xlsx
    wb.SaveAs("sample.xlsx");
    Console.WriteLine("successfully created.");
    Console.ReadKey();
}
'''
'''Generate XLSX File
'''anchor-generate-xlsx-file-c-num
'''*
Imports IronXL
Shared Sub Main(ByVal args() As String)
	'create new WorkBook of .xlsx Extension
	Dim wb As WorkBook = WorkBook.Create(ExcelFileFormat.XLSX)
	'create workSheet
	Dim ws As WorkSheet = wb.CreateWorkSheet("Sheet1")
	'insert data in the cells of WorkSheet
	ws ("A1").Value = "Hello"
	ws ("A2").Value = "World"
	ws ("C4").Value = "IronXL"
	'save the file as .xlsx
	wb.SaveAs("sample.xlsx")
	Console.WriteLine("successfully created.")
	Console.ReadKey()
End Sub
VB   C#

你可以在这里看到新创建的 Excel 文件 sample.xlsx 的截图:

在 C# 中生成 Excel 文件,图 1:单元格 C4 中修改值的结果

单元格 C4 中修改值的结果


4.生成 XLS 文件 C#

使用 IonXL 也可以生成 .xls 文件。为此,我们将使用 创建工作簿()`工作簿 功能如下

WorkBook.Create(ExcelFileFormat.XLS)
WorkBook.Create(ExcelFileFormat.XLS)
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'WorkBook.Create(ExcelFileFormat.XLS)
VB   C#

这将创建一个扩展名为".xls "的新 Excel 文件。请记住,在为 Excel 文件命名时,必须在文件名后加上扩展名 .xls,就像这样:

WorkBook.SaveAs("Path + FileName.xls");
WorkBook.SaveAs("Path + FileName.xls");
WorkBook.SaveAs("Path + FileName.xls")
VB   C#

现在,让我们看看如何生成扩展名为 .xls 的 Excel 文件的示例:

/**
Generate XLS File
anchor-generate-xls-file-c-num
**/
using IronXL;
static void Main(string [] args)
{
    //create new WorkBook of .xls Extension 
    WorkBook wb = WorkBook.Create(ExcelFileFormat.XLS);
    //create WorkSheet
    WorkSheet ws = wb.CreateWorkSheet("Sheet1");
    //insert data in the cells of WorkSheet
    ws ["A1"].Value = "Hello";
    ws ["A2"].Value = "World";
    //save the file as .xls
    wb.SaveAs("sample.xls");
    Console.WriteLine("successfully created.");
    Console.ReadKey();
}
/**
Generate XLS File
anchor-generate-xls-file-c-num
**/
using IronXL;
static void Main(string [] args)
{
    //create new WorkBook of .xls Extension 
    WorkBook wb = WorkBook.Create(ExcelFileFormat.XLS);
    //create WorkSheet
    WorkSheet ws = wb.CreateWorkSheet("Sheet1");
    //insert data in the cells of WorkSheet
    ws ["A1"].Value = "Hello";
    ws ["A2"].Value = "World";
    //save the file as .xls
    wb.SaveAs("sample.xls");
    Console.WriteLine("successfully created.");
    Console.ReadKey();
}
'''
'''Generate XLS File
'''anchor-generate-xls-file-c-num
'''*
Imports IronXL
Shared Sub Main(ByVal args() As String)
	'create new WorkBook of .xls Extension 
	Dim wb As WorkBook = WorkBook.Create(ExcelFileFormat.XLS)
	'create WorkSheet
	Dim ws As WorkSheet = wb.CreateWorkSheet("Sheet1")
	'insert data in the cells of WorkSheet
	ws ("A1").Value = "Hello"
	ws ("A2").Value = "World"
	'save the file as .xls
	wb.SaveAs("sample.xls")
	Console.WriteLine("successfully created.")
	Console.ReadKey()
End Sub
VB   C#

5.生成 CSV 文件 C#

逗号分隔值 (.csv) 在不同类型的组织中,CSV 文件在保存数据方面也发挥着非常重要的作用。因此,我们还需要学习如何生成.csv文件并以编程方式在其中插入数据。

我们可以使用与上述相同的过程,但需要在保存文件时指定.csv扩展名。让我们看看如何在 C# 项目中创建 .csv 文件的示例:

/**
Generate CSV File
anchor-generate-csv-file-c-num
**/
using IronXL;
static void Main(string [] args)
{
    //create new WorkBook  
    WorkBook wb = WorkBook.Create();
    //create WorkSheet
    WorkSheet ws = wb.CreateWorkSheet("Sheet1");
    //insert data in the cells of WorkSheet
    ws ["A1"].Value = "Hello";
    ws ["A2"].Value = "World";
    //save the file as .csv
    wb.SaveAsCsv("sample.csv");
    Console.WriteLine("successfully created.");
    Console.ReadKey();
}
/**
Generate CSV File
anchor-generate-csv-file-c-num
**/
using IronXL;
static void Main(string [] args)
{
    //create new WorkBook  
    WorkBook wb = WorkBook.Create();
    //create WorkSheet
    WorkSheet ws = wb.CreateWorkSheet("Sheet1");
    //insert data in the cells of WorkSheet
    ws ["A1"].Value = "Hello";
    ws ["A2"].Value = "World";
    //save the file as .csv
    wb.SaveAsCsv("sample.csv");
    Console.WriteLine("successfully created.");
    Console.ReadKey();
}
'''
'''Generate CSV File
'''anchor-generate-csv-file-c-num
'''*
Imports IronXL
Shared Sub Main(ByVal args() As String)
	'create new WorkBook  
	Dim wb As WorkBook = WorkBook.Create()
	'create WorkSheet
	Dim ws As WorkSheet = wb.CreateWorkSheet("Sheet1")
	'insert data in the cells of WorkSheet
	ws ("A1").Value = "Hello"
	ws ("A2").Value = "World"
	'save the file as .csv
	wb.SaveAsCsv("sample.csv")
	Console.WriteLine("successfully created.")
	Console.ReadKey()
End Sub
VB   C#

要与 CSV 文件进行更多交互,您可以按照本教程进行以下操作 读取.csv文件.


6.生成 TSV 文件 C#

有时我们需要生成制表符分隔值 (.tsv) 文件,并以编程方式插入数据。

使用 IronXL,我们还可以生成.tsv扩展文件,在其中插入数据,然后将其保存到所需位置。

让我们看看如何生成.tsv扩展文件的示例:

/**
Generate TSV File
anchor-generate-tsv-file-c-num
**/
using IronXL;
static void Main(string [] args)
{ 
    //create new WorkBook  
    WorkBook wb = WorkBook.Create();
    //create WorkSheet
    WorkSheet ws = wb.CreateWorkSheet("Sheet1");
    //insert data in the cells of WorkSheet
    ws ["A1"].Value = "Hello";
    ws ["A2"].Value = "World";
    //save the file as .tsv
    wb.SaveAs("sample.tsv");
    Console.WriteLine("successfully created.");
    Console.ReadKey();
}
/**
Generate TSV File
anchor-generate-tsv-file-c-num
**/
using IronXL;
static void Main(string [] args)
{ 
    //create new WorkBook  
    WorkBook wb = WorkBook.Create();
    //create WorkSheet
    WorkSheet ws = wb.CreateWorkSheet("Sheet1");
    //insert data in the cells of WorkSheet
    ws ["A1"].Value = "Hello";
    ws ["A2"].Value = "World";
    //save the file as .tsv
    wb.SaveAs("sample.tsv");
    Console.WriteLine("successfully created.");
    Console.ReadKey();
}
'''
'''Generate TSV File
'''anchor-generate-tsv-file-c-num
'''*
Imports IronXL
Shared Sub Main(ByVal args() As String)
	'create new WorkBook  
	Dim wb As WorkBook = WorkBook.Create()
	'create WorkSheet
	Dim ws As WorkSheet = wb.CreateWorkSheet("Sheet1")
	'insert data in the cells of WorkSheet
	ws ("A1").Value = "Hello"
	ws ("A2").Value = "World"
	'save the file as .tsv
	wb.SaveAs("sample.tsv")
	Console.WriteLine("successfully created.")
	Console.ReadKey()
End Sub
VB   C#

7.生成 JSON 文件 C#

我们可以轻松地说,JavaScript Object Notation(JavaScript 对象标记法 (.json) 文件是最常见的数据文件,几乎所有软件开发公司都会使用。因此,我们经常需要以 JSON 格式保存数据。为此,我们需要一种最简单的方法来生成 JSON 格式文件并将数据插入其中。

在这种情况下,IronXL 是最好的选择,我们可以用它为 C# 轻松生成这些文件。让我们看看示例。

/**
Generate JSON File
anchor-generate-json-file-c-num
**/
using IronXL;
static void Main(string [] args)
{
    //create new WorkBook 
    WorkBook wb = WorkBook.Create();
    //create WorkSheet
    WorkSheet ws = wb.CreateWorkSheet("Sheet1");
    //insert data in the cells of WorkSheet
    ws ["A1"].Value = "1";
    ws ["A2"].Value = "john";
    ws ["B1"].Value = "2";
    ws ["B2"].Value = "alex";
    ws ["C1"].Value = "3";
    ws ["C2"].Value = "stokes";
    //save the file as .json
    wb.SaveAsJson("sample.json");
    Console.WriteLine("successfully created.");
    Console.ReadKey();
}
/**
Generate JSON File
anchor-generate-json-file-c-num
**/
using IronXL;
static void Main(string [] args)
{
    //create new WorkBook 
    WorkBook wb = WorkBook.Create();
    //create WorkSheet
    WorkSheet ws = wb.CreateWorkSheet("Sheet1");
    //insert data in the cells of WorkSheet
    ws ["A1"].Value = "1";
    ws ["A2"].Value = "john";
    ws ["B1"].Value = "2";
    ws ["B2"].Value = "alex";
    ws ["C1"].Value = "3";
    ws ["C2"].Value = "stokes";
    //save the file as .json
    wb.SaveAsJson("sample.json");
    Console.WriteLine("successfully created.");
    Console.ReadKey();
}
'''
'''Generate JSON File
'''anchor-generate-json-file-c-num
'''*
Imports IronXL
Shared Sub Main(ByVal args() As String)
	'create new WorkBook 
	Dim wb As WorkBook = WorkBook.Create()
	'create WorkSheet
	Dim ws As WorkSheet = wb.CreateWorkSheet("Sheet1")
	'insert data in the cells of WorkSheet
	ws ("A1").Value = "1"
	ws ("A2").Value = "john"
	ws ("B1").Value = "2"
	ws ("B2").Value = "alex"
	ws ("C1").Value = "3"
	ws ("C2").Value = "stokes"
	'save the file as .json
	wb.SaveAsJson("sample.json")
	Console.WriteLine("successfully created.")
	Console.ReadKey()
End Sub
VB   C#

并查看新创建的 JSON 文件 sample.json 的截图:

在 C# 中生成 Excel 文件,图 2:在 Visual Studio 中导航到 NuGet 包管理器

在 Visual Studio 中导航到 NuGet 包管理器


8.生成 XML 文件 C#

在商业应用程序开发中,我们经常需要将数据保存在可扩展标记语言(Extensible Markup Language)中。 (.xml) 文件格式。这一点很重要,因为.xml文件数据可被人类和机器读取。

通过以下示例,我们将学习如何为 C# 生成 .xml 文件并以编程方式插入数据。

/**
Generate XML File
anchor-generate-xml-file-c-num
**/
using IronXL;
static void Main(string [] args)
{
    //create new WorkBook 
    WorkBook wb = WorkBook.Create();
    //create WorkSheet
    WorkSheet ws = wb.CreateWorkSheet("Sheet1");
    //insert data in the cells of WorkSheet
    ws ["A1"].Value = "Hello";
    ws ["A2"].Value = "World";
    //save the file as .json
    wb.SaveAsXml("sample.xml");
    Console.WriteLine("successfully created.");
    Console.ReadKey();
}
/**
Generate XML File
anchor-generate-xml-file-c-num
**/
using IronXL;
static void Main(string [] args)
{
    //create new WorkBook 
    WorkBook wb = WorkBook.Create();
    //create WorkSheet
    WorkSheet ws = wb.CreateWorkSheet("Sheet1");
    //insert data in the cells of WorkSheet
    ws ["A1"].Value = "Hello";
    ws ["A2"].Value = "World";
    //save the file as .json
    wb.SaveAsXml("sample.xml");
    Console.WriteLine("successfully created.");
    Console.ReadKey();
}
'''
'''Generate XML File
'''anchor-generate-xml-file-c-num
'''*
Imports IronXL
Shared Sub Main(ByVal args() As String)
	'create new WorkBook 
	Dim wb As WorkBook = WorkBook.Create()
	'create WorkSheet
	Dim ws As WorkSheet = wb.CreateWorkSheet("Sheet1")
	'insert data in the cells of WorkSheet
	ws ("A1").Value = "Hello"
	ws ("A2").Value = "World"
	'save the file as .json
	wb.SaveAsXml("sample.xml")
	Console.WriteLine("successfully created.")
	Console.ReadKey()
End Sub
VB   C#

您可以阅读更多关于 转换 Excel 电子表格 和文件,以便在 C# 项目中使用。

IronXL 库还提供多种与 Excel 文件交互的功能,如 单元格数据格式化, 合并单元格, 插入数学函数甚至管理图表。


教程快速访问

IronXL 生成器文档

阅读有关 IronXL 如何生成 C# 项目所需的各种 Excel 格式文件的完整文档。

IronXL 生成器文档
< 前一页
使用 C# 创建 CSV 文件
下一步 >
在 .NET Core 中处理 Excel

准备开始了吗? 版本: 2024.9 刚刚发布

免费NuGet下载 总下载量: 1,010,881 查看许可证 >