How to Export Excel Data Using C#

In this tutorial, viewers learn how to export Excel spreadsheet data into various formats using C# and IronXL. The process begins with setting up a console application in Visual Studio 2022, followed by installing IronXL via the NuGet Package Manager. After importing the IronXL library in code, the Excel file is loaded using the Workbook.Load function. The tutorial guides users through exporting the Excel file to different formats, including CSV, JSON, and XML, using the SaveAs methods. For each file type, the path for the saved file is specified, and the generated files are reviewed to ensure data integrity and formatting consistency. The tutorial highlights IronXL’s effectiveness in maintaining original data formatting across exported files, making it a reliable tool for developers needing versatile data conversion capabilities. The tutorial concludes by offering support for further assistance, emphasizing IronXL's utility and user-friendliness in Excel data manipulation.

Here's a sample code snippet demonstrating how to achieve this:

// Ensure you have installed IronXL via the NuGet Package Manager in your Visual Studio project.

using System;
using IronXL;

class ExcelExportExample
{
    static void Main()
    {
        // Load the Excel file using IronXL's Workbook.Load method.
        // Replace "example.xlsx" with the path to your Excel file.
        WorkBook workbook = WorkBook.Load("example.xlsx");

        // Get the first worksheet in the Excel file.
        WorkSheet sheet = workbook.WorkSheets.First();

        // Export the Excel worksheet to CSV format.
        // Provide the desired output path for the CSV file.
        sheet.SaveAs("output.csv");

        // Export the Excel worksheet to JSON format.
        // Provide the desired output path for the JSON file.
        sheet.SaveAsJson("output.json");

        // Export the Excel worksheet to XML format.
        // Provide the desired output path for the XML file.
        sheet.SaveAsXml("output.xml");

        Console.WriteLine("Excel data exported successfully to CSV, JSON, and XML formats.");
    }
}
// Ensure you have installed IronXL via the NuGet Package Manager in your Visual Studio project.

using System;
using IronXL;

class ExcelExportExample
{
    static void Main()
    {
        // Load the Excel file using IronXL's Workbook.Load method.
        // Replace "example.xlsx" with the path to your Excel file.
        WorkBook workbook = WorkBook.Load("example.xlsx");

        // Get the first worksheet in the Excel file.
        WorkSheet sheet = workbook.WorkSheets.First();

        // Export the Excel worksheet to CSV format.
        // Provide the desired output path for the CSV file.
        sheet.SaveAs("output.csv");

        // Export the Excel worksheet to JSON format.
        // Provide the desired output path for the JSON file.
        sheet.SaveAsJson("output.json");

        // Export the Excel worksheet to XML format.
        // Provide the desired output path for the XML file.
        sheet.SaveAsXml("output.xml");

        Console.WriteLine("Excel data exported successfully to CSV, JSON, and XML formats.");
    }
}
' Ensure you have installed IronXL via the NuGet Package Manager in your Visual Studio project.

Imports System
Imports IronXL

Friend Class ExcelExportExample
	Shared Sub Main()
		' Load the Excel file using IronXL's Workbook.Load method.
		' Replace "example.xlsx" with the path to your Excel file.
		Dim workbook As WorkBook = WorkBook.Load("example.xlsx")

		' Get the first worksheet in the Excel file.
		Dim sheet As WorkSheet = workbook.WorkSheets.First()

		' Export the Excel worksheet to CSV format.
		' Provide the desired output path for the CSV file.
		sheet.SaveAs("output.csv")

		' Export the Excel worksheet to JSON format.
		' Provide the desired output path for the JSON file.
		sheet.SaveAsJson("output.json")

		' Export the Excel worksheet to XML format.
		' Provide the desired output path for the XML file.
		sheet.SaveAsXml("output.xml")

		Console.WriteLine("Excel data exported successfully to CSV, JSON, and XML formats.")
	End Sub
End Class
$vbLabelText   $csharpLabel

Explanation:

  • The IronXL library is used in the code to handle Excel file operations.
  • WorkBook.Load is used to load an existing Excel file.
  • The first worksheet is accessed using workbook.WorkSheets.First().
  • The SaveAs, SaveAsJson, and SaveAsXml methods are employed to export data to CSV, JSON, and XML formats, respectively.
  • User-defined paths are provided where the files will be saved.
  • A message is printed to the console to indicate that the export process is complete.

For further assistance and advanced tutorials, please refer to the C# Export to Excel: Step by Step Tutorial.

Regan Pun
Software Engineer
Regan graduated from the University of Reading, with a BA in Electronic Engineering. Before joining Iron Software, his previous job roles had him laser-focused on single tasks; and what he most enjoys at Iron Software is the spectrum of work he gets to undertake, whether it’s adding value to sales, technical support, product development or marketing. He enjoys understanding the way developers are using the Iron Software library, and using that knowledge to continually improve documentation and develop the products.
< PREVIOUS
How to Convert Spreadsheet Files to JSON, CSV, XML & More with IronXL in C#
NEXT >
How to Use Excel Interop in C# Alternatively