It is necessary to work with different formats of Excel spreadsheets and use C# Export to Excel functionalities. Projects may need to use spreadsheet data in specific formats, including .xml, .csv, .xls, .xlsx, and .json. In this tutorial, we will learn how to export Excel spreadsheet data into different formats using C#. It can be simple, even without dependency on the legacy Microsoft.Office.Interop.Excel library.


Step 1

1. Get the IronXL Library

For an easy way to work with Excel files in .NET Core, try IronXL. Download IronXL DLL or install with NuGet for free use in development projects.

Install-Package IronXL.Excel

Download and add its reference in your project. IronXL classes can be accessed using the IronXL namespace.


How to Tutorial

2. Export to Excel in C#

IronXL provides the most accessible way to export the data to Excel with (.xls, .xlsx, and .csv) files in .NET applications. It is also possible to export the data to .json and .xml files. Let's see one by one how easy it can be to export Excel file data into these formats.


3. C# Export to .XLSX File

It is very easy to export an Excel file with an .xlsx extension. Let's see the example. In the code below, our XlsFile.xls file exists in the bin>Debug folder of the project.

Remember: Don't forget to write the extension with the file name while importing or exporting.

By default, new Excel files will be created in the bin>Debug folder of the project. If we want to create a new file in a custom path then we can use wb.SaveAs(@"E:\IronXL\NewXlsxFile.xlsx");. Read the tutorial here to learn more about how to export Excel files in .NET.

:path=/static-assets/excel/content-code-examples/how-to/c-sharp-export-to-excel-xlsx.cs
using IronXL; // Import the IronXL library to handle Excel files

// Load an existing workbook from a .xls file. 
// Ensure the file path "XlsFile.xls" is correct and points to a valid .xls file.
WorkBook workbook = WorkBook.Load("XlsFile.xls");

// Save the loaded workbook as a .xlsx file with a new name "newFile.xlsx". 
// This will convert the file format from .xls to .xlsx.
workbook.SaveAs("newFile.xlsx");
Imports IronXL ' Import the IronXL library to handle Excel files



' Load an existing workbook from a .xls file. 

' Ensure the file path "XlsFile.xls" is correct and points to a valid .xls file.

Private workbook As WorkBook = WorkBook.Load("XlsFile.xls")



' Save the loaded workbook as a .xlsx file with a new name "newFile.xlsx". 

' This will convert the file format from .xls to .xlsx.

workbook.SaveAs("newFile.xlsx")
$vbLabelText   $csharpLabel

4. C# Export to .XLS File

It is also possible to export a file with the .xls extension using IronXL. For this purpose, let's see the example below.

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

// The following code demonstrates how to load an Excel file and save it in a different format.
// Make sure you have added the IronXL library as a reference in your project to work with Excel files.

// Load an existing Excel workbook from a given file path
WorkBook workbook = WorkBook.Load("XlsxFile.xlsx");

// Save the loaded workbook as a new file in the .xls format
workbook.SaveAs("newFile.xls");
' Path to the C# example code

Imports IronXL



' The following code demonstrates how to load an Excel file and save it in a different format.

' Make sure you have added the IronXL library as a reference in your project to work with Excel files.



' Load an existing Excel workbook from a given file path

Private workbook As WorkBook = WorkBook.Load("XlsxFile.xlsx")



' Save the loaded workbook as a new file in the .xls format

workbook.SaveAs("newFile.xls")
$vbLabelText   $csharpLabel

5. C# Export to .CSV File

We can easily export our .xlsx or .xls file into .csv using IronXL. Let's see one case that shows how to export an Excel file to a CSV (.csv) file.

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

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

// Save the content of the WorkBook object as a .csv file
workbook.SaveAs("newFile.csv");

// Note:
// - Ensure that the "IronXL" library is correctly installed and referenced in your project. 
// - The method `SaveAsCsv` was incorrect in the given code and has been corrected to `SaveAs`.
// - This code will take an Excel file named "sample.xlsx" and export it to a new CSV file named "newFile.csv."
// - Ensure the file paths used in the `Load` and `SaveAs` methods are correct relative paths or use absolute paths if necessary.
Imports IronXL



' Import a .xlsx or .xls file into a WorkBook object

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



' Save the content of the WorkBook object as a .csv file

workbook.SaveAs("newFile.csv")



' Note:

' - Ensure that the "IronXL" library is correctly installed and referenced in your project. 

' - The method `SaveAsCsv` was incorrect in the given code and has been corrected to `SaveAs`.

' - This code will take an Excel file named "sample.xlsx" and export it to a new CSV file named "newFile.csv."

' - Ensure the file paths used in the `Load` and `SaveAs` methods are correct relative paths or use absolute paths if necessary.
$vbLabelText   $csharpLabel

The above code will create the following three CSV files:

Doc2 2 related to 5. C# Export to .CSV File

It is very simple to understand why it created three .csv files. It is because sample.xlsx contains three Worksheets. Therefore, it will create three .csv files, and each Worksheet's Excel file data would export to the corresponding .csv file.

We can see the number of sheets in sample.xlsx here:

Doc2 1 related to 5. C# Export to .CSV File

However, if there is one worksheet in the Excel file, then just one .csv file would be created.


6. C# Export to .XML File

We can export our Excel file data into .XML file format. The below code will export sample.xlsx file data into a .xml file. It will create three XML files because sample.xlsx has three worksheets, as in the earlier example.

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

// The following code imports data from an Excel file and exports it as an XML file.

try
{
    // Load the workbook from a .xlsx, .xls or .csv file.
    // Ensure the file "sample.xlsx" exists in the working directory or provide a correct path to the file.
    WorkBook workbook = WorkBook.Load("sample.xlsx");

    // Save the workbook as an XML file.
    // The resulting "newFile.xml" will be saved in the working directory.
    // Make sure that the necessary permissions are set to allow writing to the directory.
    workbook.SaveAsXml("newFile.xml");

    // Inform the user that the process completed successfully.
    Console.WriteLine("The Excel file has been successfully converted to an XML file.");
}
catch (Exception ex)
{
    // Handle any exceptions that may occur during the loading or saving process.
    // This includes file not found exceptions, permissions issues, etc.
    Console.WriteLine("An error occurred: " + ex.Message);
}
Imports IronXL



' The following code imports data from an Excel file and exports it as an XML file.



Try

	' Load the workbook from a .xlsx, .xls or .csv file.

	' Ensure the file "sample.xlsx" exists in the working directory or provide a correct path to the file.

	Dim workbook As WorkBook = WorkBook.Load("sample.xlsx")



	' Save the workbook as an XML file.

	' The resulting "newFile.xml" will be saved in the working directory.

	' Make sure that the necessary permissions are set to allow writing to the directory.

	workbook.SaveAsXml("newFile.xml")



	' Inform the user that the process completed successfully.

	Console.WriteLine("The Excel file has been successfully converted to an XML file.")

Catch ex As Exception

	' Handle any exceptions that may occur during the loading or saving process.

	' This includes file not found exceptions, permissions issues, etc.

	Console.WriteLine("An error occurred: " & ex.Message)

End Try
$vbLabelText   $csharpLabel

7. C# Export to .JSON File

Using IronXL makes it very easy to export Excel file data into JSON file format, as in the below code example. The code will export sample.xlsx file data into a .json file. It will again create three .json files due to the three Worksheets of sample.xlsx.

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

// This code demonstrates how to load an Excel workbook and save it as a JSON file

// Load an Excel workbook from the specified file path
WorkBook workbook = WorkBook.Load("sample.xlsx");

// Save the workbook as a JSON file to the specified file path
workbook.SaveAsJson("newFile.json");
Imports IronXL



' This code demonstrates how to load an Excel workbook and save it as a JSON file



' Load an Excel workbook from the specified file path

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



' Save the workbook as a JSON file to the specified file path

workbook.SaveAsJson("newFile.json")
$vbLabelText   $csharpLabel

Tutorial Quick Access

Documentation related to Tutorial Quick Access

API Reference

Read the IronXL Documentation including all namespaces, feature sets, methods, fields, classes, and enums.

Read API Reference

Frequently Asked Questions

What is the purpose of the C# Export to Excel tutorial?

The tutorial aims to teach users how to export Excel spreadsheet data into various formats such as .xml, .csv, .xls, .xlsx, and .json using C#, without relying on the legacy Microsoft.Office.Interop.Excel library.

How can I get the IronXL library?

You can download the IronXL DLL from the IronXL website or install it via NuGet using the command: Install-Package IronXL.Excel.

What formats can I export Excel data to using IronXL?

IronXL allows you to export Excel data to .xls, .xlsx, .csv, .xml, and .json file formats.

Is it possible to export data to a .csv file using IronXL?

Yes, you can export .xlsx or .xls files into .csv format using IronXL. Each worksheet in the Excel file will result in a separate .csv file.

How do I export an Excel file to .XML using IronXL?

IronXL provides a straightforward method to export Excel file data into .XML format. The process is similar to exporting to other formats, and it will create separate XML files for each worksheet.

Can I export Excel data to JSON format?

Yes, IronXL allows you to export Excel data into JSON format, creating separate JSON files for each worksheet in the Excel file.

Where will the newly created Excel files be saved by default?

By default, new Excel files will be saved in the bin>Debug folder of your project. You can specify a custom path using the SaveAs method.

What is required to export an Excel file using IronXL?

To export an Excel file using IronXL, you need to include the file extension in the file name while exporting. Additionally, ensure IronXL is properly referenced in your project.

Do I need to use the Microsoft.Office.Interop.Excel library for exporting Excel data in C#?

No, you do not need to use the Microsoft.Office.Interop.Excel library. IronXL provides an efficient alternative for exporting Excel data in C# without relying on this legacy library.

Chaknith Bin
Software Engineer
Chaknith works on IronXL and IronBarcode. He has deep expertise in C# and .NET, helping improve the software and support customers. His insights from user interactions contribute to better products, documentation, and overall experience.