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:
using IronXL;
private void button7_Click(object sender, EventArgs e)
{
// 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";
}
using IronXL;
private void button7_Click(object sender, EventArgs e)
{
// 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";
}
Imports IronXL
Private Sub button7_Click(ByVal sender As Object, ByVal e As EventArgs)
' Load an existing Excel workbook
Dim workbook As 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
Dim dataSet As System.Data.DataSet = workbook.ToDataSet()
' Bind the dataset to a data grid view control for display
dataGridView1.DataSource = dataSet
dataGridView1.DataMember = "Sheet1"
End Sub
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
What is IronXL used for?
IronXL is a C# library used to convert Excel files into various formats such as JSON, CSV, XML, and older Excel formats like XLS.
How can I convert an XLSX file to CSV using IronXL?
To convert an XLSX file to CSV using IronXL, load the Excel file, access or modify the workbook, and then export it to a CSV file using the library's methods.
What are the steps to install the IronXL library?
You can install IronXL by downloading it from the IronXL website or 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?
Yes, IronXL can convert Excel files to JSON and XML formats, among others. You can use the library's methods to save the workbook as JSON or XML files.
Is it possible to convert an Excel workbook to a DataSet using IronXL?
Yes, IronXL allows you to convert an Excel workbook to a DataSet, enabling integration with other data tools like DataGridView.
Where can I find the IronXL API Reference Documentation?
The IronXL API Reference Documentation is available on the IronXL website, providing detailed information on how to work with Excel files using the library.
What file formats can IronXL export to?
IronXL can export Excel files to formats including CSV, JSON, XML, XLS, and XLSX.
Do I need Excel installed on my system to use IronXL?
No, IronXL does not require Microsoft Excel or any other software to be installed on your system to perform conversions.