如何使用C#导出Excel数据

C# Export to Excel: Complete Developer Guide

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

Working with Excel data often requires exporting spreadsheets to different formats for integration with various systems and applications. This guide demonstrates how to export Excel files to multiple formats including .xlsx, .xls, .csv, .xml, and .json using C# and IronXL—without requiring Microsoft Office Interop dependencies.

Quickstart: Export Workbook to JSON in One Line

Load your existing Excel file and export it to JSON in just one line with IronXL’s intuitive API. Developers can start exporting Excel files without writing boilerplate code, making data interchange effortless.

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.

    WorkBook workbook = WorkBook.Load("data.xlsx"); workbook.SaveAsJson("output.json");
  3. Deploy to test on your live environment

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

Step 1

How to Install IronXL for Excel Export?

IronXL provides a streamlined approach to working with Excel files in .NET applications. Download the IronXL DLL directly or install via NuGet for development and testing.

Install-Package IronXL.Excel

After installation, add the IronXL reference to access all Excel manipulation features through the IronXL namespace. The library supports .NET Framework, .NET Core, and .NET 5+ applications, making it compatible with modern development environments.


How to Tutorial

What Excel Export Formats Does C# Support?

IronXL enables seamless data export from Excel workbooks to multiple file formats. The library supports exporting to:

  • Excel formats: .xlsx (modern) and .xls (legacy)
  • Data interchange formats: .csv, .json, and .xml
  • Tab-separated values: .tsv

Each format serves specific use cases—CSV for database imports, JSON for web APIs, and XML for enterprise system integration. We can also export the file formats metioned above to their respective output streams to intergrate with other applications. Let's explore how to implement each export type.


How to Export Excel to XLSX in C#?

Converting between Excel formats is straightforward with IronXL. The following example demonstrates loading an older .xls file and saving it as a modern .xlsx file:

:path=/static-assets/excel/content-code-examples/how-to/c-sharp-export-to-excel-xlsx.cs
using IronXL;

// Import .xls, .csv, or .tsv file
WorkBook workbook = WorkBook.Load("XlsFile.xls");

// Export as .xlsx file
workbook.SaveAs("newFile.xlsx");
Imports IronXL

' Import .xls, .csv, or .tsv file
Private workbook As WorkBook = WorkBook.Load("XlsFile.xls")

' Export as .xlsx file
workbook.SaveAs("newFile.xlsx")
$vbLabelText   $csharpLabel

The SaveAs method automatically detects the desired format based on the file extension. When converting from .xls to .xlsx, the process preserves all worksheets, formulas, formatting, and data types. This conversion is particularly useful when modernizing legacy Excel files for compatibility with newer Office versions.

Export to XLSX Stream

Alternatively, you can also use ToXlsxStream to export the document to an XLSX to integrate with other applications. Here's a brief example on how to do it.

:path=/static-assets/excel/content-code-examples/how-to/c-sharp-export-to-excel-xlsx-stream.cs
using IronXL;

// Load the workbook
WorkBook workbook = WorkBook.Load("xlsFile.xlsx");

// Exports the workbook to a XLSX stream.
var stream = workbook.ToXlsxStream();
Imports IronXL

' Load the workbook
Private workbook As WorkBook = WorkBook.Load("xlsFile.xlsx")

' Exports the workbook to a XLSX stream.
Private stream = workbook.ToXlsxStream()
$vbLabelText   $csharpLabel

How to Export Excel to XLS Format?

Legacy systems sometimes require the older .xls format. IronXL handles this conversion seamlessly:

:path=/static-assets/excel/content-code-examples/how-to/c-sharp-export-to-excel-xls.cs
using IronXL;

// Import .xlsx, .csv or .tsv file
WorkBook workbook = WorkBook.Load("XlsxFile.xlsx");

// Export as .xls file
workbook.SaveAs("newFile.xls");
Imports IronXL

' Import .xlsx, .csv or .tsv file
Private workbook As WorkBook = WorkBook.Load("XlsxFile.xlsx")

' Export as .xls file
workbook.SaveAs("newFile.xls")
$vbLabelText   $csharpLabel

This conversion maintains compatibility with Excel 97-2003 versions. The SaveAs method handles the format conversion internally, ensuring data integrity while adapting to the .xls format limitations such as the 65,536 row limit per worksheet.

Export to XLS Stream

In addition to using SaveAs to export to the legacy .XLS format, IronXL also supports exporting as an XLS stream.

:path=/static-assets/excel/content-code-examples/how-to/c-sharp-export-to-excel-xls-stream.cs
using IronXL;

// Load the workbook
WorkBook workbook = WorkBook.Load("xlsxFile.xlsx");

// Exports the workbook to a XLS stream.
var stream = workbook.ToXlsStream();
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

How to Convert Excel to CSV in C#?

CSV export is essential for data exchange between different systems. IronXL provides a dedicated method for CSV conversion:

:path=/static-assets/excel/content-code-examples/how-to/c-sharp-export-to-excel-csv.cs
using IronXL;

//  Import .xlsx or xls file
WorkBook workbook = WorkBook.Load("sample.xlsx");

// Export as .xls file
workbook.SaveAsCsv("newFile.csv");
Imports IronXL

'  Import .xlsx or xls file
Private workbook As WorkBook = WorkBook.Load("sample.xlsx")

' Export as .xls file
workbook.SaveAsCsv("newFile.csv")
$vbLabelText   $csharpLabel

When exporting multi-sheet workbooks, IronXL creates separate CSV files for each worksheet. For a workbook with three sheets named "Sheet1", "Sheet2", and "Sheet3", the export generates:

Three CSV files created from Excel export showing exportedData.Sheet1.csv, exportedData.Sheet2.csv, and exportedData.Sheet3.csv in Windows Explorer

Multiple CSV files generated from a three-sheet Excel workbook, with each worksheet exported as a separate CSV file

The original Excel file structure with three worksheets:

Excel workbook showing three worksheet tabs labeled Sheet1, Sheet2, and Sheet3 at the bottom of the spreadsheet interface

Original Excel workbook displaying three worksheets that will be exported as individual CSV files

The SaveAsCsv method preserves data values while removing formatting, formulas, and other Excel-specific features. This makes CSV ideal for importing data into databases or processing with other applications.

Export Excel Worksheet to CSV Stream

To convert only a specified worksheet within the workbook to a CSV stream, we can use the ToCsvStream method and provide an integer indicating the worksheet's index to return as a CSV stream. Below is an example of this method.

:path=/static-assets/excel/content-code-examples/how-to/c-sharp-export-to-excel-csv-stream.cs
using IronXL;

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

// Exports the first sheet of the workbook to a CSV stream
var stream = workbook.ToCsvStream(0);
Imports IronXL

' Load the workbook
Private workbook As WorkBook = WorkBook.Load("sample.xlsx")

' Exports the first sheet of the workbook to a CSV stream
Private stream = workbook.ToCsvStream(0)
$vbLabelText   $csharpLabel

Export All to CSV Stream

IronXL also supports converting the entire workbook into a CSV stream; we use the method ToCsvStreams instead to convert all worksheets in the workbook.

:path=/static-assets/excel/content-code-examples/how-to/c-sharp-export-to-excel-csv-streams.cs
using IronXL;

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

// Exports the entire workbook to a CSV stream
var workBookStream = workbook.ToCsvStreams();
Imports IronXL

' Load the workbook
Private workbook As WorkBook = WorkBook.Load("sample.xlsx")

' Exports the entire workbook to a CSV stream
Private workBookStream = workbook.ToCsvStreams()
$vbLabelText   $csharpLabel

How to Export Excel Data to XML?

XML export enables structured data exchange with enterprise systems. IronXL simplifies this process:

:path=/static-assets/excel/content-code-examples/how-to/c-sharp-export-to-excel-xml.cs
using IronXL;

// Import .xlsx, .xls or .csv file
WorkBook workbook = WorkBook.Load("sample.xlsx");

// Export as .xml file
workbook.SaveAsXml("newFile.xml");
Imports IronXL

' Import .xlsx, .xls or .csv file
Private workbook As WorkBook = WorkBook.Load("sample.xlsx")

' Export as .xml file
workbook.SaveAsXml("newFile.xml")
$vbLabelText   $csharpLabel

The SaveAsXml method converts each worksheet into a separate XML file with a structured representation of the spreadsheet data. This includes:

  • Cell values and data types
  • Row and column positioning
  • Basic structural information

XML export is particularly valuable for integrating Excel data with web services or systems that require structured data formats.

Export Data as XML Stream

To export a specified worksheet to an XML stream, we use the ToXmlStream method and provide an integer parameter, which represents the index of the worksheet.

:path=/static-assets/excel/content-code-examples/how-to/c-sharp-export-to-excel-xml-stream.cs
using IronXL;

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

// Converts the first worksheet to a XML Stream
var stream = workbook.ToXmlStream(0);
Imports IronXL

' Load the workbook
Private workbook As WorkBook = WorkBook.Load("sample.xlsx")

' Converts the first worksheet to a XML Stream
Private stream = workbook.ToXmlStream(0)
$vbLabelText   $csharpLabel

Export All Data to XML Streams

To export the entire workbook as XML streams, we use the ToXmlStreams method.

:path=/static-assets/excel/content-code-examples/how-to/c-sharp-export-to-excel-xml-streams.cs
using IronXL;

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

// Exports the entire workbook to a XML stream
var workBookStream = workbook.ToXmlStreams();
Imports IronXL

' Load the workbook
Private workbook As WorkBook = WorkBook.Load("sample.xlsx")

' Exports the entire workbook to a XML stream
Private workBookStream = workbook.ToXmlStreams()
$vbLabelText   $csharpLabel

How to Generate JSON from Excel Files?

Modern web applications often require JSON format for data exchange. IronXL provides direct JSON export functionality:

:path=/static-assets/excel/content-code-examples/how-to/c-sharp-export-to-excel-json.cs
using IronXL;

// Import Excel file
WorkBook workbook = WorkBook.Load("sample.xlsx");

// Export as JSON file
workbook.SaveAsJson("newFile.json");
Imports IronXL

' Import Excel file
Private workbook As WorkBook = WorkBook.Load("sample.xlsx")

' Export as JSON file
workbook.SaveAsJson("newFile.json")
$vbLabelText   $csharpLabel

The SaveAsJson method creates JSON files with array-based structures representing spreadsheet data. Each worksheet exports as a separate JSON file, making it easy to:

  • Integrate with RESTful APIs
  • Process data in JavaScript applications
  • Store spreadsheet data in NoSQL databases

For advanced scenarios, developers can combine JSON export with IronXL's data manipulation features to transform data before exporting.

Export Excel Worksheet as JSON Stream

To convert a specified worksheet to a JSON stream, we use the method ToJsonStream and provide an integer as an index of the worksheet you wish to convert.

:path=/static-assets/excel/content-code-examples/how-to/c-sharp-export-to-excel-json-stream.cs
using IronXL;

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

// Exports the first sheet of the workbook to a JSON stream
var stream = workbook.ToJsonStream(0);
Imports IronXL

' Load the workbook
Private workbook As WorkBook = WorkBook.Load("sample.xlsx")

' Exports the first sheet of the workbook to a JSON stream
Private stream = workbook.ToJsonStream(0)
$vbLabelText   $csharpLabel

Export All as JSON Streams

To convert the entire workbook to a JSON stream, we use the method ToJsonStreams instead.

:path=/static-assets/excel/content-code-examples/how-to/c-sharp-export-to-excel-json-streams.cs
using IronXL;

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

// Exports the entire workbook to a JSON stream
var workBookStream = workbook.ToJsonStreams();
Imports IronXL

' Load the workbook
Private workbook As WorkBook = WorkBook.Load("sample.xlsx")

' Exports the entire workbook to a JSON stream
Private workBookStream = workbook.ToJsonStreams()
$vbLabelText   $csharpLabel

Tutorial Quick Access

Documentation related to Tutorial Quick Access

API Reference Documentation

Explore the complete IronXL API including all namespaces, classes, methods, and properties for advanced Excel manipulation.

View API Reference

Ready to Export Excel Files?

IronXL simplifies Excel file export operations in C# applications. Whether converting between Excel formats or exporting to data interchange formats like CSV, JSON, or XML, the library provides intuitive methods that handle complex conversions automatically.

Start implementing Excel export functionality today with a free trial of IronXL. For production deployments, explore our flexible licensing options designed for teams of all sizes.

立即开始使用 IronXL。
green arrow pointer

常见问题解答

如何使用 C# 将 Excel 数据导出到多种格式?

您可以使用 IronXL 通过使用 SaveAsCsv()、SaveAsXml() 和 SaveAsJson() 等相应的方法,将 Excel 数据导出为 .xls,.xlsx,.csv,.xml 和 .json 等多种格式。

我需要 Microsoft Office 来在 C# 中导出 Excel 文件吗?

不,您不需要安装 Microsoft Office。IronXL 提供了独立的解决方案,以便无需依赖 Microsoft.Office.Interop.Excel 即可导出 Excel 文件。

如何在 C# 中将 Excel 文件导出为 JSON 格式?

使用 IronXL 的 SaveAsJson() 方法将 Excel 数据转换为 JSON 格式,创建基于数组的 JSON 结构,实现与 Web API 的轻松集成。

使用 IronXL 导出 Excel 文件的前提条件是什么?

确保通过 NuGet 安装 IronXL 或手动将其添加到项目中,在导出的文件名中包含正确的文件扩展名,并使用与所需格式对应的正确导出方法。

我可以选择特定的 Excel 工作表进行导出吗?

是的,IronXL 允许您使用 WorkBook.WorkSheets[index] 或 WorkBook.GetWorkSheet(name) 选择特定的工作表进行导出,然后在所选工作表上应用 SaveAsCsv() 等导出方法。

如何将导出的 Excel 文件保存到指定目录?

在 SaveAs() 方法参数中指定完整的文件路径,以将导出的文件保存到自定义目录而不是默认的 bin\Debug 文件夹中。

我应该使用什么方法将 Excel 数据导出到 XML?

IronXL 提供 SaveAsXml() 方法,该方法将每个工作表导出为单独的 XML 文件,保留数据结构,使其适合与需要 XML 输入的应用程序进行集成。

如何在 C# 中将 Excel 文件导出为 CSV 格式?

使用 IronXL 的 SaveAsCsv() 方法将 Excel 文件导出为 CSV 格式。它会自动处理具有多个工作表的工作簿,为每个工作表创建单独的 CSV 文件。

使用 IronXL 进行 Excel 导出操作的好处是什么?

IronXL 通过其直观的 API 简化了复杂的导出操作,使开发人员可以将 Excel 文件转换为多种格式,同时确保数据完整性而无需依赖 Microsoft Office。

Jacob Mellor,Team Iron 的首席技术官
首席技术官

Jacob Mellor 是 Iron Software 的首席技术官,是 C# PDF 技术的先锋工程师。作为 Iron Software 核心代码库的原始开发者,自公司成立以来,他就塑造了公司的产品架构,并与首席执行官 Cameron Rimington 一起将其转变成一家公司,拥有50多人,服务于 NASA、特斯拉和全球政府机构。

Jacob 拥有曼彻斯特大学 (1998-2001) 的一级荣誉土木工程学士学位。1999 年在伦敦创办了自己的第一家软件公司,并于 2005 年创建了他的第一个 .NET 组件后,他专注于解决微软生态系统中的复杂问题。

他的旗舰 IronPDF 和 IronSuite .NET 库在全球已获得超过 3000 万次的 NuGet 安装,其基础代码继续为全球使用的开发者工具提供支持。拥有 25 年商业经验和 41 年编程经验的 Jacob 仍专注于推动企业级 C#、Java 和 Python PDF 技术的创新,同时指导下一代技术领导者。

准备开始了吗?
Nuget 下载 1,686,155 | 版本: 2025.11 刚刚发布