在实际环境中测试
在生产中测试无水印。
随时随地为您服务。
我们开发的应用程序经常要与 Excel 电子表格进行通信,以获取评估和结果所需的数据。 能用 C# 编程生成 Excel 文件确实很有帮助,为我们的开发工作节省了时间和精力。 在本教程中,我们将学习使用高效函数 C# 编程生成不同格式的 Excel 文件、设置单元格样式和插入数据。
使用 IronXL Excel for C# 库生成 Excel 文件,该库提供了一系列用于在项目中生成和处理数据的函数。 该库的开发是免费的,当您准备好上线时,还可以获得许可证。要学习本教程,您可以下载 IronXL 生成或通过 Visual Studio 和NuGet 图库.
Install-Package IronXL.Excel
在商业应用程序开发中,我们经常需要以编程方式生成不同类型的 Excel 文件。 为此,我们需要最简单快捷的方法来生成不同类型的文件,并自动保存到所需位置。
安装 IronXL 后,我们可以使用其功能来生成不同类型的 Excel 文件:
.xlsx
的 Excel 文件。.xls
的 Excel 文件。.tsv
)文件.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()
上面的代码行将创建一个新的 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")
这将创建一个 WorkSheet``ws
,我们可以用它在 Excel 文件中插入数据。
首先,我们按照上述步骤生成 "WorkBook "和 "WorkSheet"。
然后,我们在其中插入数据,创建我们的".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"
它将在特定的单元格地址中插入一个我们称为 "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")
这将在指定路径下创建一个扩展名为.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
您可以在此处查看新创建的 Excel 文件 sample.xlsx
的截图:
单元格 C4 中修改值的结果
也可以使用 IonXL 生成 .xls
文件。 为此,我们将使用WorkBook.Create()`工作簿功能如下
WorkBook.Create(ExcelFileFormat.XLS)
WorkBook.Create(ExcelFileFormat.XLS)
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'WorkBook.Create(ExcelFileFormat.XLS)
这将创建一个新的 Excel 文件,扩展名为 .xls
。 请记住,在为 Excel 文件命名时,您必须在文件名后写上扩展名 .xls
,就像这样:
WorkBook.SaveAs("Path + FileName.xls");
WorkBook.SaveAs("Path + FileName.xls");
WorkBook.SaveAs("Path + FileName.xls")
现在,让我们看看如何生成扩展名为 .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
逗号分隔值(.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
要与 CSV 文件进行更多交互,您可以按照本教程进行以下操作读取.csv
文件.
有时我们需要生成以制表符分隔的值(.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
我们可以轻松地说,JavaScript Object Notation(.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
并查看新创建的 JSON 文件 sample.json
的截图:
在 Visual Studio 中导航到 NuGet 包管理器
在商业应用程序开发中,我们经常需要将数据保存在可扩展标记语言中。(.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
您可以阅读更多关于转换 Excel 电子表格在 C# 项目中以编程方式使用 .NET 和文件。
IronXL.Excel 库还提供了与 Excel 文件交互的多种功能,如单元格数据格式化, 合并单元格, 插入数学函数甚至管理图表。