C# 导出到 Excel:逐步教程

This article was translated from English: Does it need improvement?
Translated
View the article in English

有必要处理不同格式的 Excel 电子表格并使用 C Sharp Export to Excel 功能。项目可能需要使用特定格式的电子表格数据,包括 .xml.csv.xls.xlsx.json。在本教程中,我们将学习如何使用 C# 将 Excel 电子表格数据导出为不同格式。它可以很简单,甚至无需依赖传统的 Microsoft.Office.Interop.Excel 库。


步骤 1

1.获取 IronXL 库

要在 .NET Core 中轻松处理 Excel 文件,请试试 IronXL。 下载 IronXL DLL使用 NuGet 安装 供开发项目免费使用。

Install-Package IronXL.Excel

下载并在项目中添加其引用。可以使用 IronXL 命名空间访问 IronXL 类。


教程

2.以 C&num 格式导出到 Excel;

IronXL 提供了将数据导出到 Excel 的最便捷方法,其中包括 ( .xls"、".xlsx "和".csv) 文件。还可以将数据导出为 .json.xml 文件。让我们逐一看看将 Excel 文件数据导出为这些格式有多简单。


3.C# 导出到 .XLSX 文件

导出扩展名为".xlsx "的 Excel 文件非常简单。让我们看看示例。在下面的代码中,我们的 XlsFile.xls 文件存在于项目的 bin>Debug 文件夹中。

请记住在导入或导出文件时,不要忘记在文件名中写入扩展名。

默认情况下,新 Excel 文件将在项目的 bin>Debug 文件夹中创建。如果我们想在自定义路径下创建新文件,可以使用 wb.SaveAs(@"E:\IronXL\NewXlsxFile.xlsx");.阅读此处的教程,了解如何 在 .NET 中导出 Excel 文件

/**
Export to XLSX
anchor-c-export-to-xlsx-file
**/
using IronXL;
static void Main(string [] args)
{
    WorkBook wb = WorkBook.Load("XlsFile.xls");//Import .xls, .csv, or .tsv file
    wb.SaveAs("NewXlsxFile.xlsx");//Export as .xlsx file
}
/**
Export to XLSX
anchor-c-export-to-xlsx-file
**/
using IronXL;
static void Main(string [] args)
{
    WorkBook wb = WorkBook.Load("XlsFile.xls");//Import .xls, .csv, or .tsv file
    wb.SaveAs("NewXlsxFile.xlsx");//Export as .xlsx file
}
'''
'''Export to XLSX
'''anchor-c-export-to-xlsx-file
'''*
Imports IronXL
Shared Sub Main(ByVal args() As String)
	Dim wb As WorkBook = WorkBook.Load("XlsFile.xls") 'Import .xls, .csv, or .tsv file
	wb.SaveAs("NewXlsxFile.xlsx") 'Export as .xlsx file
End Sub
VB   C#

4.C# 导出到 .XLS 文件

使用 IronXL 还可以导出扩展名为 .xls 的文件。为此,请看下面的示例。

/**
Export to XLS
anchor-c-export-to-xls-file
**/
using IronXL;
static void Main(string [] args)
{
    WorkBook wb = WorkBook.Load("XlsxFile.xlsx");//Import .xlsx, .csv or .tsv file
    wb.SaveAs("NewXlsFile.xls");//Export as .xls file
}
/**
Export to XLS
anchor-c-export-to-xls-file
**/
using IronXL;
static void Main(string [] args)
{
    WorkBook wb = WorkBook.Load("XlsxFile.xlsx");//Import .xlsx, .csv or .tsv file
    wb.SaveAs("NewXlsFile.xls");//Export as .xls file
}
'''
'''Export to XLS
'''anchor-c-export-to-xls-file
'''*
Imports IronXL
Shared Sub Main(ByVal args() As String)
	Dim wb As WorkBook = WorkBook.Load("XlsxFile.xlsx") 'Import .xlsx, .csv or .tsv file
	wb.SaveAs("NewXlsFile.xls") 'Export as .xls file
End Sub
VB   C#

5.C# 导出到 .CSV 文件

使用 IronXL,我们可以轻松地将.xlsx.xls文件导出为.csv。让我们来看一个如何将 Excel 文件导出为 CSV 的案例 (.csv) 锉刀

/**
Export to CSV
anchor-c-export-to-csv-file
**/
using IronXL;
static void Main(string [] args)
{
    WorkBook wb = WorkBook.Load("sample.xlsx");  //Import .xlsx or xls file          
    wb.SaveAsCsv("NewCsvFile.csv"); //Export as .xls file
}
/**
Export to CSV
anchor-c-export-to-csv-file
**/
using IronXL;
static void Main(string [] args)
{
    WorkBook wb = WorkBook.Load("sample.xlsx");  //Import .xlsx or xls file          
    wb.SaveAsCsv("NewCsvFile.csv"); //Export as .xls file
}
'''
'''Export to CSV
'''anchor-c-export-to-csv-file
'''*
Imports IronXL
Shared Sub Main(ByVal args() As String)
	Dim wb As WorkBook = WorkBook.Load("sample.xlsx") 'Import .xlsx or xls file
	wb.SaveAsCsv("NewCsvFile.csv") 'Export as .xls file
End Sub
VB   C#

上述代码将创建以下三个 CSV 文件:

很容易理解为什么会创建三个.csv文件。这是因为 sample.xlsx 包含三个工作表。因此,它会创建三个.csv文件,每个工作表的 Excel 文件数据都会导出到相应的.csv文件。

我们可以在这里看到 sample.xlsx 中的工作表数量:

但是,如果 Excel 文件中只有一个工作表,那么将只创建一个 .csv 文件。


6.C# 导出到 .XML 文件:

我们可以将 Excel 文件数据导出为 .XML 文件格式。下面的代码将把 sample.xlsx 文件数据导出为 .xml 文件。它将创建三个 XML 文件,因为 sample.xlsx 有三个工作表,与前面的示例相同。

/**
Export to XML
anchor-c-export-to-xml-file
**/
using IronXL;
static void Main(string [] args)
{
    WorkBook wb = WorkBook.Load("sample.xlsx");  //Import .xlsx, .xls or .csv file          
    wb.SaveAsCsv("NewXmlFile.xml"); //Export as .xml file
}
/**
Export to XML
anchor-c-export-to-xml-file
**/
using IronXL;
static void Main(string [] args)
{
    WorkBook wb = WorkBook.Load("sample.xlsx");  //Import .xlsx, .xls or .csv file          
    wb.SaveAsCsv("NewXmlFile.xml"); //Export as .xml file
}
'''
'''Export to XML
'''anchor-c-export-to-xml-file
'''*
Imports IronXL
Shared Sub Main(ByVal args() As String)
	Dim wb As WorkBook = WorkBook.Load("sample.xlsx") 'Import .xlsx, .xls or .csv file
	wb.SaveAsCsv("NewXmlFile.xml") 'Export as .xml file
End Sub
VB   C#

7.C# 导出到 .JSON 文件

使用 IronXL 可以非常轻松地将 Excel 文件数据导出为 JSON 文件格式,如以下代码示例所示。代码将把 sample.xlsx 文件数据导出为 .json 文件。它还将根据 sample.xlsx 的三个工作表创建三个 .json 文件。

/**
Export to JSON
anchor-c-export-to-json-file
**/
using IronXL;
static void Main(string [] args)
{
    WorkBook wb = WorkBook.Load("sample.xlsx"); //import Excel file
    wb.SaveAsJson("NewjsonFile.json"); //Export as JSON file
}
/**
Export to JSON
anchor-c-export-to-json-file
**/
using IronXL;
static void Main(string [] args)
{
    WorkBook wb = WorkBook.Load("sample.xlsx"); //import Excel file
    wb.SaveAsJson("NewjsonFile.json"); //Export as JSON file
}
'''
'''Export to JSON
'''anchor-c-export-to-json-file
'''*
Imports IronXL
Shared Sub Main(ByVal args() As String)
	Dim wb As WorkBook = WorkBook.Load("sample.xlsx") 'import Excel file
	wb.SaveAsJson("NewjsonFile.json") 'Export as JSON file
End Sub
VB   C#

教程快速访问

API文档

阅读 IronXL 文档,包括所有命名空间、特征集、方法字段、类和枚举。

Read API文档