跳至页脚内容
使用 IRONXL

如何在 .NET 6 中导出文件到 Excel

In this article, one of the most popular libraries, IronXL, will be used to compare and contrast how to interact with Microsoft Excel documents programmatically in .NET technologies, whether in CSV format or the standard Excel format.

2.0 IronXL Library Features

Microsoft Excel documents can be read and converted to CSV files using the C# IronXL .NET Excel library. IronXL is a stand-alone .NET software library that can read multiple spreadsheet formats and does not require Microsoft Office or Microsoft.Office.Interop.Excel to be installed.

With the simple C# API of IronXL, you can quickly read, modify, and create Excel spreadsheets in the .NET environment. IronXL fully supports .NET Core, .NET Framework, Xamarin, Mobile, Linux, macOS, and Azure.

  • IronXL is one of the best Excel spreadsheet libraries for C#, supporting both the .NET Framework and .NET Core.
  • IronXL supports nearly all .NET Frameworks, including Console Applications, Windows Forms, and Web Applications.
  • IronXL is compatible with the Windows, Linux, and macOS operating systems.
  • IronXL can quickly and effortlessly read Excel files.
  • IronXL can read a variety of Excel file types, including XLSX, CSV, XLS, XLST, TSV, XLSM, and others. IronXL also offers a lot of options, including the ability to import, update, and export data tables and datasets.
  • IronXL is capable of generating calculations for Excel.
  • IronXL supports several Excel column data formats, including text, numbers, formulas, dates, currencies, and percentages.
  • IronXL supports the following Excel column data types: text, integers, formulas, dates, currencies, and percentages.

Visit the official IronXL website to learn more.

3.0 Creating a .NET Core 6 Project

The following sections of this newsletter provide an example to illustrate how easily the IronXL library generates QR codes.

Open Visual Studio and select "New Project" from the "File" menu option.

In the resulting dialog box, select the "Console App" template and click "Next."

How to Export File to Excel in .NET 6, Figure 1: Create a new project in Visual Studio Create a new project in Visual Studio

Enter any project name you choose for the Project name, and then enter the location of the new project in the Location field. Click the Next button to proceed.

How to Export File to Excel in .NET 6, Figure 2: Configure your new project Configure your new project

Select a .NET Framework from the Framework drop-down option. Here, we're using .NET 6.0, which has long-term support. Then click Create.

How to Export File to Excel in .NET 6, Figure 3: .NET Framework selection .NET Framework selection

Next, download the required IronXL library for the solution. To do so, use the following command in the Package Manager Console:

Install-Package IronXL.Excel

How to Export File to Excel in .NET 6, Figure 4: Package Manager Console tab Package Manager Console tab

Alternatively, the package 'IronXL' can be searched and installed using the NuGet Package Manager. This will list all the NuGet packages related to IronXL, and then you can select the required package to download.

How to Export File to Excel in .NET 6, Figure 5: Search and install the IronXL package in NuGet Package Manager UI Search and install the IronXL package in NuGet Package Manager UI

The created Form has added a 'Save As' dialog box which allows saving the generated IronXL images to the selected location.

4.0 Export Data using IronXL

Exporting data to Excel format can be easily done using IronXL as well as exporting data into various file types, such as XLS, XLSX, CSV, JSON, and XML, using IronXL.

4.1 Export Data to an Excel File

It is very simple to export data to .xlsx or .xls formats with just a few lines of code. Below is a sample source code to export data from an Excel file into a simple tabular format:

// Load an existing Excel file into a WorkBook object.
var workbook = IronXL.WorkBook.LoadExcel("Demo file.xlsx");

// Save the entire workbook as export.xlsx.
workbook.SaveAs("export.xlsx");

// Alternatively, save as export.xls.
workbook.SaveAs("export.xls");

// Save a specific worksheet, identified by index, as export.xls.
workbook.WorkSheets[0].SaveAs("export.xls");
// Load an existing Excel file into a WorkBook object.
var workbook = IronXL.WorkBook.LoadExcel("Demo file.xlsx");

// Save the entire workbook as export.xlsx.
workbook.SaveAs("export.xlsx");

// Alternatively, save as export.xls.
workbook.SaveAs("export.xls");

// Save a specific worksheet, identified by index, as export.xls.
workbook.WorkSheets[0].SaveAs("export.xls");
' Load an existing Excel file into a WorkBook object.
Dim workbook = IronXL.WorkBook.LoadExcel("Demo file.xlsx")

' Save the entire workbook as export.xlsx.
workbook.SaveAs("export.xlsx")

' Alternatively, save as export.xls.
workbook.SaveAs("export.xls")

' Save a specific worksheet, identified by index, as export.xls.
workbook.WorkSheets(0).SaveAs("export.xls")
$vbLabelText   $csharpLabel

In the above example, an existing Excel file is loaded using the LoadExcel method, which allows passing the file name with the file path as a parameter. This loads the file into the 'Workbook' object. The WorkBook object provides a method called SaveAs, which allows saving the Excel document as an XLSX or XLS file. This method saves the entire file into the chosen format.

It is also possible to select a specific Excel worksheet by using the index value of the worksheet or by specifying the name of the sheet. Then, use the SaveAs function to export the data of the Excel worksheet into a separate file.

4.2 Export Data to a CSV File

We can also export data into CSV using IronXL. Below is an example code to export the data source into CSV format:

// Load an existing Excel file into an WorkBook object.
var excelDoc = IronXL.WorkBook.LoadExcel("Demo file.xlsx");

// Save the loaded Excel document as a CSV file.
excelDoc.SaveAsCsv("export.csv");
// Load an existing Excel file into an WorkBook object.
var excelDoc = IronXL.WorkBook.LoadExcel("Demo file.xlsx");

// Save the loaded Excel document as a CSV file.
excelDoc.SaveAsCsv("export.csv");
' Load an existing Excel file into an WorkBook object.
Dim excelDoc = IronXL.WorkBook.LoadExcel("Demo file.xlsx")

' Save the loaded Excel document as a CSV file.
excelDoc.SaveAsCsv("export.csv")
$vbLabelText   $csharpLabel

The above code is similar to exporting to XLS. IronXL provides a separate method called SaveAsCsv, which helps to export XLSX files into CSV files. It is easy and simple to use. In the above code, an XLSX file is loaded, converted, and saved into a CSV file with just a few lines of code. We can also pass user input such as a password as a parameter on the LoadExcel method.

4.3 Export Data to a JSON File

Saving XLSX files as JSON has never been easier by using the following code:

// Load the Excel document into an WorkBook object.
var excelDoc = IronXL.WorkBook.LoadExcel("Demo file.xlsx");

// Save the loaded document as a JSON file.
excelDoc.SaveAsJson("export.json");

// Alternatively, save as an XML file.
excelDoc.SaveAsXml("export.xml");
// Load the Excel document into an WorkBook object.
var excelDoc = IronXL.WorkBook.LoadExcel("Demo file.xlsx");

// Save the loaded document as a JSON file.
excelDoc.SaveAsJson("export.json");

// Alternatively, save as an XML file.
excelDoc.SaveAsXml("export.xml");
' Load the Excel document into an WorkBook object.
Dim excelDoc = IronXL.WorkBook.LoadExcel("Demo file.xlsx")

' Save the loaded document as a JSON file.
excelDoc.SaveAsJson("export.json")

' Alternatively, save as an XML file.
excelDoc.SaveAsXml("export.xml")
$vbLabelText   $csharpLabel

The above sample code demonstrates that IronXL provides separate methods to save files as XML and JSON, which can be easily implemented.

IronXL offers features such as header rows, font sizes, formulas, colors, and more. Visit IronXL tutorials for more information.

Conclusion

IronXL is one of the most widely used Excel utilities that can operate independently of any other external libraries. It doesn't require Microsoft Excel to be installed and uses a variety of channels to function.

For all tasks related to Microsoft Excel documents that need to be implemented programmatically, IronXL offers an all-in-one solution. You can perform formula calculations, sort strings or numbers, cut and append data, find and replace, merge and unmerge cells, and save files. Additionally, it allows you to set cell data formats and perform spreadsheet data validation. You can also read and write CSV files, enabling interaction with Excel data.

IronXL is available for $799 at launch and provides the option for customers to pay a one-year membership charge for updates and product support. For an additional charge, IronXL provides security for unrestricted redistribution. Visit the licensing page to learn more details about the pricing information.

常见问题解答

如何在 .NET 6 中导出数据到 Excel 而不使用 Interop?

您可以使用 IronXL 库在 .NET 6 中将数据导出到 Excel,而无需依赖 Microsoft.Office.Interop.Excel。IronXL 允许您以多种格式创建、修改和保存 Excel 文件,如 XLS、XLSX、CSV、JSON 和 XML。

在 .NET 中使用 IronXL 进行 Excel 操作有什么好处?

IronXL 提供了一个简洁的 C# API 用于 Excel 操作,支持多种格式,包括 XLS、XLSX 和 CSV。它独立于 Microsoft Excel 运行,与各种 .NET 框架兼容,并可在 Windows、Linux 和 macOS 上运行。

在 .NET 6 项目中安装 IronXL 需要哪些步骤?

要在 .NET 6 项目中安装 IronXL,您可以使用包管理器控制台命令 Install-Package IronXL.Excel,或使用 Visual Studio 中的 NuGet 包管理器来查找和安装 'IronXL'。

如何使用 IronXL 将 Excel 文件导出为 JSON 格式?

您可以使用 IronXL 将 Excel 文件导出为 JSON 格式,方法是将 Excel 文档加载到 WorkBook 对象中,然后使用 SaveAsJson 方法另存为 JSON 文件。

IronXL 能够处理 Excel 公式和数据格式化吗?

是的,IronXL 支持 Excel 公式,并允许您为单元格设置数据格式,从而便于计算并确保数据的一致性展示。

IronXL 需要安装 Microsoft Office 才能进行 Excel 操作吗?

不,IronXL 不需要安装 Microsoft Office 或任何外部 Excel 应用程序。它是一个独立的库,可以独立处理 Excel 操作。

IronXL 可以将数据导出到哪些文件格式?

IronXL 可以将数据导出到多种文件格式,包括 XLS、XLSX、CSV、JSON 和 XML,为数据存储和共享提供了灵活性。

IronXL 可以与 .NET Core 应用程序一起使用吗?

是的,IronXL 兼容 .NET Core,也兼容 .NET Framework、Xamarin、移动设备、Azure 等,使其适用于不同类型的应用程序。

IronXL 提供什么样的支持和更新?

IronXL 提供为期一年的更新和产品支持会员资格。还有可供选择的无限制再发布选项,为开发者提供安全和灵活性。

Curtis Chau
技术作家

Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。

除了开发之外,Curtis 对物联网 (IoT) 有浓厚的兴趣,探索将硬件和软件集成的新方法。在空闲时间,他喜欢玩游戏和构建 Discord 机器人,将他对技术的热爱与创造力相结合。