How to Convert Spreadsheet File Types

by Chaknith Bin

Introduction

IronXL supports the conversion of spreadsheet files between various formats, including XLS, XLSX, XLSM, XLTX, CSV, TSV, JSON, XML, and HTML. It also provides support for inline code data types such as HTML string, Binary, Byte array, Data set, and Memory stream. The Load method is used to open a spreadsheet file, and the SaveAs method is used to export the spreadsheet to the desired file type.


C# NuGet Library for Excel

Install with NuGet

Install-Package IronXL.Excel
or
C# Excel DLL

Download DLL

Download DLL

Manually install into your project

Convert Spreadsheet Type Example

The process of converting spreadsheet types involves loading a file in one supported format and exporting it to another format using IronXL's intelligent data restructuring capabilities.

While the SaveAs method can be used to export to CSV, JSON, XML, and HTML, it is recommended to use the dedicated methods for each file format:

  • SaveAsCsv
  • SaveAsJson
  • SaveAsXml
  • ExportToHtml

Please note
For CSV, TSV, JSON, and XML file formats, a separate file will be created for each worksheet. The naming convention follows the format fileName.sheetName.format. In the example below, the output for the CSV format would be sample.new_sheet.csv.

:path=/static-assets/excel/content-code-examples/how-to/convert-spreadsheet-file-types-convert.cs
using IronXL;

// Import any XLSX, XLS, XLSM, XLTX, CSV and TSV
WorkBook workBook = WorkBook.Load("sample.xlsx");

// Export the excel file as XLS, XLSX, XLSM, CSV, TSV, JSON, XML
workBook.SaveAs("sample.xls");
workBook.SaveAs("sample.tsv");
workBook.SaveAsCsv("sample.csv");
workBook.SaveAsJson("sample.json");
workBook.SaveAsXml("sample.xml");

// Export the excel file as Html
workBook.ExportToHtml("sample.html");
Imports IronXL

' Import any XLSX, XLS, XLSM, XLTX, CSV and TSV
Private workBook As WorkBook = WorkBook.Load("sample.xlsx")

' Export the excel file as XLS, XLSX, XLSM, CSV, TSV, JSON, XML
workBook.SaveAs("sample.xls")
workBook.SaveAs("sample.tsv")
workBook.SaveAsCsv("sample.csv")
workBook.SaveAsJson("sample.json")
workBook.SaveAsXml("sample.xml")

' Export the excel file as Html
workBook.ExportToHtml("sample.html")
VB   C#

Advanced

In the previous section, we explored the most common file formats for conversion. However, IronXL has the capability to convert spreadsheets to many more formats. Explore all the available options for loading and exporting spreadsheets.

Load

  • XLS, XLSX, XLSM, and XLTX
  • CSV
  • TSV

Export

  • XLS, XLSX, and XLSM
  • CSV and TSV
  • JSON
  • XML
  • HTML
  • Inline code data types:
    • HTML string
    • Binary and Byte array
    • Data set: Exporting Excel into System.Data.DataSet and System.Data.DataTable objects allow easy interoperability or integration with DataGrids, SQL and EF.
    • Memory stream

The inline code data types can be sent as a RESTful API response or used with IronPDF to convert them into a PDF document.

:path=/static-assets/excel/content-code-examples/how-to/convert-spreadsheet-file-types-convert-advance.cs
using IronXL;
using System.IO;

// Import any XLSX, XLS, XLSM, XLTX, CSV and TSV
WorkBook workBook = WorkBook.Load("sample.xlsx");

// Export the excel file as XLS, XLSX, XLSM, CSV, TSV, JSON, XML
workBook.SaveAs("sample.xls");
workBook.SaveAs("sample.xlsx");
workBook.SaveAs("sample.tsv");
workBook.SaveAsCsv("sample.csv");
workBook.SaveAsJson("sample.json");
workBook.SaveAsXml("sample.xml");

// Export the excel file as Html, Html string
workBook.ExportToHtml("sample.html");
string htmlString = workBook.ExportToHtmlString();

// Export the excel file as Binary, Byte array, Data set, Stream
byte[] binary = workBook.ToBinary();
byte[] byteArray = workBook.ToByteArray();
System.Data.DataSet dataSet = workBook.ToDataSet(); // Allow easy integration with DataGrids, SQL and EF
Stream stream = workBook.ToStream();
Imports IronXL
Imports System.IO

' Import any XLSX, XLS, XLSM, XLTX, CSV and TSV
Private workBook As WorkBook = WorkBook.Load("sample.xlsx")

' Export the excel file as XLS, XLSX, XLSM, CSV, TSV, JSON, XML
workBook.SaveAs("sample.xls")
workBook.SaveAs("sample.xlsx")
workBook.SaveAs("sample.tsv")
workBook.SaveAsCsv("sample.csv")
workBook.SaveAsJson("sample.json")
workBook.SaveAsXml("sample.xml")

' Export the excel file as Html, Html string
workBook.ExportToHtml("sample.html")
Dim htmlString As String = workBook.ExportToHtmlString()

' Export the excel file as Binary, Byte array, Data set, Stream
Dim binary() As Byte = workBook.ToBinary()
Dim byteArray() As Byte = workBook.ToByteArray()
Dim dataSet As System.Data.DataSet = workBook.ToDataSet() ' Allow easy integration with DataGrids, SQL and EF
Dim stream As Stream = workBook.ToStream()
VB   C#

The code above loads an ordinary XLSX file, then converts and exports it to several formats.

The Spreadsheet We Will Convert

XLSX file
XSLX file

The various files exported are shown below.

sample.Data.tsv
TSV File Export
sample.Data.csv
CSV File Export
sample.Data.json
Json File Export
sample.Data.xml
XML File Export
sample.html
HTML File Export

Chaknith Bin

Software Engineer

Chaknith is the Sherlock Holmes of developers. It first occurred to him he might have a future in software engineering, when he was doing code challenges for fun. His focus is on IronXL and IronBarcode, but he takes pride in helping customers with every product. Chaknith leverages his knowledge from talking directly with customers, to help further improve the products themselves. His anecdotal feedback goes beyond Jira tickets and supports product development, documentation and marketing, to improve customer’s overall experience.When he isn’t in the office, he can be found learning about machine learning, coding and hiking.