How to Convert Spreadsheet File Types

by Chaknith Bin

Introduction

IronXL supports converting spreadsheet file from file formats: XLS, XLSX, XLSM, XLTX, CSV, TSV to file formats: XLS, XLSX, XLSM, CSV, TSV, JSON, XML or HTML including inline code data types: HTML string, Binary, Byte array, Data set, and Memory stream. The Load method is used to open spreadsheet file and the SaveAs method is used to export spreadsheet to 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

How to Convert Spreadsheet Type

The idea of converting spreadsheet type is to load the file from one of the supported type and export it to another format. Hence, the converting process happens within IronXL ingenious restructuring of data.

Although SaveAs can be used to export CSV, JSON, XML and HTML. Exclusive methods for each of these file format is recommended:

  • SaveAsCsv
  • SaveAsJson
  • SaveAsXml
  • ExportToHtml

Please note

  • For CSV, TSV, JSON, and XML file format, each file will be created corresponding to each worksheet. The naming convention would be fileName.sheetName.format. In the example below the output for 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 last section we only explore the most common file formats. However, IronXL can convert spreadsheet to many more formats. Explore all possible options for loading and exporting spreadsheet.

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 is can be sent as a restful API respond or be used with IronPDF to convert into 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 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.