C# Export to Excel

By FoxLearn 1/8/2025 4:05:08 AM   61
Working with various Excel spreadsheet formats and utilizing C# export functionalities is essential for many projects.

IronXL offers an easy way to export data to Excel (.xls, .xlsx, .csv) as well as .json and .xml formats in .NET applications.

What is IronXL?

IronXL is an Excel library for C# and .NET that enables developers to read and edit Excel data from XLS and XLSX files without relying on Microsoft.Office.Interop.Excel. It allows developers to read, create, and modify Excel and other spreadsheet files in .NET applications.

In this tutorial, we will explore how to export Excel data into these different formats using C#, without relying on the legacy Microsoft.Office.Interop.Excel library.

How to Export to Excel in C#?

To work with Excel files in .NET Core, you can use the IronXL library.

You can install it via NuGet using the command Install-Package IronXL.Excel.

After installation, add the reference to your project, and access IronXL classes through the IronXL namespace.

C# Export to .XLSX File

Exporting an Excel file with a .xlsx extension is simple.

To save a new file in a custom location, use wb.SaveAs(@"C:\IronXL\XlsxFile.xlsx");

using IronXL;
static void Main(string[] args)
{
    WorkBook wb = WorkBook.Load("XlsFile.xls"); // Import .xls, .csv, or .tsv file
    wb.SaveAs("XlsxFile.xlsx"); // Export as .xlsx file
}

C# Export to .XLS File

You can also export a file with the .xls extension using IronXL.

using IronXL;
static void Main(string[] args)
{
    WorkBook wb = WorkBook.Load("XlsxFile.xlsx"); // Import .xlsx, .csv, or .tsv file
    wb.SaveAs("XlsFile.xls"); // Export as .xls file
}

C# Export to .CSV File

Exporting a .xlsx or .xls file to a .csv format is simple with IronXL.

using IronXL;
static void Main(string[] args)
{
    WorkBook wb = WorkBook.Load("sample.xlsx");  // Import .xlsx or .xls file
    wb.SaveAsCsv("CsvFile.csv"); // Export as .csv file
}

C# Export to .XML File

You can export Excel file data into .xml format using IronXL.

The following example demonstrates how to export the data from sample.xlsx to an .xml file. Similar to the previous example, it will create three XML files because sample.xlsx contains three worksheets.

using IronXL;
static void Main(string[] args)
{
    WorkBook wb = WorkBook.Load("sample.xlsx");  // Import .xlsx, .xls, or .csv file
    wb.SaveAsXml("XmlFile.xml"); // Export as .xml file
}

C# Export to .JSON File

IronXL simplifies the process of exporting Excel file data into JSON format. The following code example demonstrates how to export data from sample.xlsx to a .json file.

using IronXL;
static void Main(string[] args)
{
    WorkBook wb = WorkBook.Load("sample.xlsx"); // Import Excel file
    wb.SaveAsJson("JsonFile.json"); // Export as JSON file
}