How to Convert XLSX to CSV, JSON, XML and more in C#
IronXL can convert any Excel file into various formats.
These formats include: JSON, CSV, XML, and even older Excel formats such as XLS.
This brief article will show you how to use IronXL to convert to XML, convert to CSV, convert to JSON, and as a bonus, show you how to output an Excel worksheet as a dataset.
How to Convert XLSX (Excel) to CSV in C#
- Download C# library to convert XLSX to CSV
- Load existing XLSX Excel spreadsheet
- Access or modify the workbook
- Export to CSV file or several other formats including JSON, TSV, and XML
- Check the output files and apply further processing
Step 1
1. Install the IronXL Library Free
First, you must have IronXL installed before you can use it in your applications. The following two methods allow you to install IronXL.
Download: https://ironsoftware.com/csharp/excel/docs/
Or use the NuGet Package Manager
- Right-click the Solution name in Solution Explorer
- Click Manage NuGet Packages
- Browse for IronXL.Excel
- Install
Install-Package IronXL.Excel
How to Tutorial
2. Convert to XML, JSON, CSV, XLS
Now you are ready.
Add the following code:
:path=/static-assets/excel/content-code-examples/how-to/csharp-convert-xlsx-csv-convert.cs
using IronXL;
// Load an existing Excel workbook
WorkBook workbook = WorkBook.Load("Normal_Excel_File.xlsx");
// Set metadata title for the workbook
workbook.Metadata.Title = "Normal_Excel_File.xlsx";
// Save the workbook in different formats
workbook.SaveAs("XLS_Export.xls");
workbook.SaveAs("XLSX_Export.xlsx");
workbook.SaveAsCsv("CSV_Export.csv");
workbook.SaveAsJson("JSON_Export.json");
workbook.SaveAsXml("XML_Export.xml");
// Convert the workbook to a DataSet, allowing integration with other data tools like DataGridView
System.Data.DataSet dataSet = workbook.ToDataSet();
// Bind the dataset to a data grid view control for display
dataGridView1.DataSource = dataSet;
dataGridView1.DataMember = "Sheet1";
The code above loads an ordinary XLSX file, adds a Title, then converts it to several formats. Lastly, it exports the Worksheet as a DataSet that gets used by a DataGridView object.
The various files exported are shown below.
Library Quick Access
IronXL API Reference Documentation
Learn more and share how to merge, unmerge, and work with cells in Excel spreadsheets using the handy IronXL API Reference Documentation.
IronXL API Reference DocumentationFrequently Asked Questions
How can I convert an XLSX file to CSV in C#?
To convert an XLSX file to CSV using IronXL, first install the library via the NuGet Package Manager with Install-Package IronXL.Excel
. Next, load the Excel file using IronXL's methods, modify the workbook if needed, and export it to a CSV file.
What file formats can be exported using IronXL?
IronXL allows exporting Excel files to several formats, including CSV, JSON, XML, XLS, and XLSX, enabling flexible data handling and integration.
Is it necessary to have Microsoft Excel installed to use IronXL?
No, IronXL operates independently of Microsoft Excel, allowing you to perform Excel file conversions without needing Excel installed on your system.
How do I convert an Excel workbook to a DataSet in C#?
You can convert an Excel workbook to a DataSet using IronXL by loading the workbook and utilizing the library's method to transform it into a DataSet, facilitating integration with tools like DataGridView.
Where can I find the API Reference Documentation for IronXL?
The API Reference Documentation for IronXL is available on the IronXL website, providing comprehensive guidance on handling Excel files programmatically.
How can I install the IronXL library in a C# project?
You can install the IronXL library by using the NuGet Package Manager in Visual Studio. Use the command Install-Package IronXL.Excel
in the NuGet Package Manager Console.
Can IronXL convert Excel files to JSON and XML formats?
Yes, IronXL can convert Excel files into JSON and XML formats, among others, by using the library's export functions.
What are common troubleshooting steps if my Excel to CSV conversion fails?
Ensure that IronXL is correctly installed and that your code accurately references the library's methods. Verify that the Excel file is accessible and not corrupted. Consult the IronXL API documentation for additional troubleshooting tips.