如何使用 IronXL 在 C# 中将电子表格文件转换为 JSON、CSV、XML 等格式

How to Convert Spreadsheet File Types

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

IronXL supports the conversion of spreadsheet files between various formats, including XLS, XLSX, XLSM, XLTX, CSV, TSV, JSON, XML, and HTML. It also provides support for inline code data types such as HTML string, Binary, Byte array, Data set, and Memory stream. The Load method is used to open a spreadsheet file, and the SaveAs method is used to export the spreadsheet to the desired file type.

Quickstart: Convert an Excel Spreadsheet to JSON

With one simple line of code, IronXL lets you load an Excel file and export it as JSON—no complex configuration, just fast results. It’s a perfect get-started example for developers needing swift file-type conversion.

Nuget IconGet started making PDFs with NuGet now:

  1. Install IronXL with NuGet Package Manager

    PM > Install-Package IronXL.Excel

  2. Copy and run this code snippet.

    IronXL.WorkBook.Load("input.xlsx").SaveAsJson("output.json");
  3. Deploy to test on your live environment

    Start using IronXL in your project today with a free trial
    arrow pointer


Convert Spreadsheet Type Example

The process of converting spreadsheet types involves loading a file in one supported format and exporting it to another format using IronXL's intelligent data restructuring capabilities.

While the SaveAs method can be used to export to CSV, JSON, XML, and HTML, it is recommended to use the dedicated methods for each file format:

  • SaveAsCsv
  • SaveAsJson
  • SaveAsXml
  • ExportToHtml

请注意For CSV, TSV, JSON, and XML file formats, a separate file will be created for each worksheet. The naming convention follows the format fileName.sheetName.format. In the example below, the output for the CSV format would be sample.new_sheet.csv.

:path=/static-assets/excel/content-code-examples/how-to/convert-spreadsheet-file-types-different-format.cs
using IronXL;

// Load the Excel spreadsheet
WorkBook workbook = WorkBook.Load("sample.xlsx");

// Convert and save as CSV
workbook.SaveAsCsv("sample.csv");

// Convert and save as JSON
workbook.SaveAsJson("sample.json");

// Convert and save as XML
workbook.SaveAsXml("sample.xml");

// Convert and export to HTML
workbook.ExportToHtml("sample.html");

// The files will be saved in the project's directory with the specified formats
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

Advanced

In the previous section, we explored the most common file formats for conversion. However, IronXL has the capability to convert spreadsheets to many more formats. Explore all the available options for loading and exporting spreadsheets.

Load

  • XLS, XLSX, XLSM, and XLTX
  • CSV
  • TSV

Export

  • XLS, XLSX, and XLSM
  • CSV and TSV
  • JSON
  • XML
  • HTML
  • Inline code data types:

    • HTML string
    • Binary and Byte array
    • Data set: Exporting Excel into System.Data.DataSet and System.Data.DataTable objects allows easy interoperability or integration with DataGrids, SQL, and EF.
    • Memory stream

The inline code data types can be sent as a RESTful API response or used with IronPDF to convert them into a PDF document.

:path=/static-assets/excel/content-code-examples/how-to/convert-spreadsheet-file-types-different-format-advanced.cs
using IronXL;

// Load the Excel spreadsheet
WorkBook workbook = WorkBook.Load("advanced_sample.xlsx");

// Convert and save to various formats
workbook.SaveAs("advanced_sample.xlsm");
workbook.SaveAsCsv("advanced_sample.csv");
workbook.SaveAsJson("advanced_sample.json");
workbook.SaveAsXml("advanced_sample.xml");
workbook.ExportToHtml("advanced_sample.html");

// You can also convert to binary or byte array for advanced uses
byte[] excelAsByteArray = workbook.ToBinary();
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

The code above loads an ordinary XLSX file, then converts and exports it to several formats.

The Spreadsheet We Will Convert

XLSX file
XLSX file

The various files exported are shown below.

sample.Data.tsv
TSV File Export
sample.Data.csv
CSV File Export
sample.Data.json
JSON File Export
sample.Data.xml
XML File Export
sample.html
HTML File Export

常见问题解答

如何使用 C# 将电子表格转换为 CSV?

您可以通过 IronXL 库提供的 `SaveAsCsv` 方法将电子表格转换为 CSV。只需使用 `WorkBook.Load` 方法加载您的 Excel 文件,并调用 `SaveAsCsv` 即可导出。

使用 IronXL 可以导出哪些格式?

IronXL 允许通过相应的方法如 `SaveAsCsv`、`SaveAsTsv`、`SaveAsJson`、`SaveAsXml` 和 `ExportToHtml` 将电子表格导出为多个格式,包括 CSV、TSV、JSON、XML 和 HTML。

您如何在 C# 中加载 Excel 文件以进行转换?

要在 C# 中加载 Excel 文件,使用 IronXL 库中的 `WorkBook.Load` 方法。此方法支持多种 Excel 格式,例如 XLS、XLSX 等。

IronXL 能处理数据转换为 JSON 吗?

是的,IronXL 可以使用 `SaveAsJson` 方法将电子表格数据转换为 JSON 格式。这允许轻松的数据操作和与 Web 应用程序的集成。

可以将 Excel 文件导出为 HTML 吗?

可以,使用 IronXL,您可以通过 `ExportToHtml` 方法将 Excel 文件导出为 HTML 格式。这对于电子表格数据的 Web 演示很有用。

WorkBook 类在 IronXL 中的作用是什么?

IronXL 中的 `WorkBook` 类用于加载和操作 Excel 文件,允许您查看、修改数据并将其导出为各种格式。

如何确保导出的数据是正确的?

在使用 IronXL 方法如 `SaveAsCsv` 或 `SaveAsJson` 导出后,可以通过检查指定目录中保存的文件来验证输出以确保准确性。

IronXL 可以将电子表格数据导出为 XML 吗?

是的,IronXL 支持使用 `SaveAsXml` 方法将电子表格数据导出为 XML 格式,便于集成需要 XML 输入的系统。

如何在 C# 项目中安装 IronXL 以转换 Excel 文件?

可通过 NuGet 包管理器在您的 C# 项目中安装 IronXL,允许您利用其强大的 Excel 文件转换功能。

使用 IronXL 进行文件转换的优点是什么?

IronXL 通过支持多种文件格式并提供高级数据处理方法(如导出为字节数组或内存流),简化了转换过程。

Chaknith Bin
软件工程师
Chaknith 在 IronXL 和 IronBarcode 工作。他在 C# 和 .NET 方面有着深厚的专业知识,帮助改进软件并支持客户。他从用户互动中获得的见解有助于更好的产品、文档和整体体验。
准备开始了吗?
Nuget 下载 1,686,155 | 版本: 2025.11 刚刚发布