How to Save or Export Spreadsheets

The DataSet class, integrated into Microsoft's .NET, is a fundamental component of the ADO.NET (ActiveX Data Objects for .NET) technology. It plays a vital role in applications related to databases and offers the capability to interact with data from diverse sources, including databases, XML, and more.

IronXL can convert Excel workbooks into many different file formats as well as inline code objects. The file formats include XLS, XLSX, XLSM, CSV, TSV, JSON, XML, and HTML. The inline code objects encompass exporting the Excel file as HTML string, binary, byte array, dataset, and stream.


Get started with IronXL

Start using IronXL in your project today with a free trial.

First Step:
green arrow pointer


Export Spreadsheet Example

After editing or viewing the workbook is finished, use the SaveAs method to export the Excel spreadsheet to the desired file format. This method accounts for a variety of file formats such as XLS, XLSX, XLSM, CSV, TSV, JSON, XML, and HTML.

Don't forget to include the file extension when importing or exporting. By default, new Excel files will be created in the 'bin > Debug > net6.0' folder of the project.

:path=/static-assets/excel/content-code-examples/how-to/export-spreadsheet-export-spreadsheet.cs
// Import the necessary namespace for IronXL
using IronXL;

// Create a new Excel WorkBook document
WorkBook workBook = WorkBook.Create();

// Create a blank WorkSheet named "new_sheet"
WorkSheet workSheet = workBook.CreateWorkSheet("new_sheet");

// Save the Excel file as .xls
// The Excel file can also be saved in other formats like XLSX, XLSM, CSV, TSV, JSON, XML, HTML 
// by changing the file extension in the SaveAs method
workBook.SaveAs("sample.xls");
' Import the necessary namespace for IronXL
Imports IronXL

' Create a new Excel WorkBook document
Private workBook As WorkBook = WorkBook.Create()

' Create a blank WorkSheet named "new_sheet"
Private workSheet As WorkSheet = workBook.CreateWorkSheet("new_sheet")

' Save the Excel file as .xls
' The Excel file can also be saved in other formats like XLSX, XLSM, CSV, TSV, JSON, XML, HTML 
' by changing the file extension in the SaveAs method
workBook.SaveAs("sample.xls")
$vbLabelText   $csharpLabel

Export CSV, JSON, XML, and HTML Files

Although the SaveAs method can be used to export CSV, JSON, XML, and HTML files, it is recommended to use exclusive methods for each of these file formats. Use the SaveAsCsv, SaveAsJson, SaveAsXml, and ExportToHtml methods.

:path=/static-assets/excel/content-code-examples/how-to/export-spreadsheet-export-csv-json-xml-html.cs
using IronXL;

// This code demonstrates creating an Excel workbook with multiple worksheets,
// populating cells with data, and saving the workbook in various formats.

// Create a new Excel WorkBook document
WorkBook workBook = WorkBook.Create(ExcelFileFormat.XLSX);

// Create two WorkSheets and name them "sheet1" and "sheet2"
WorkSheet workSheet1 = workBook.CreateWorkSheet("sheet1");
WorkSheet workSheet2 = workBook.CreateWorkSheet("sheet2");

// Input data in the first cell of each worksheet
workSheet1["A1"].StringValue = "Data in A1 of sheet1";
workSheet2["A1"].StringValue = "Data in A1 of sheet2";

// Save the workbook as a CSV file
workBook.SaveAs("sample.csv", fileFormat: ExcelFileFormat.CSV);

// Save the workbook as a JSON file
workBook.SaveAs("sample.json", fileFormat: ExcelFileFormat.JSON);

// Save the workbook as an XML file
workBook.SaveAs("sample.xml", fileFormat: ExcelFileFormat.XML);

// Export the workbook as an HTML file
workBook.ExportToHtml("sample.html");
Imports IronXL

' This code demonstrates creating an Excel workbook with multiple worksheets,
' populating cells with data, and saving the workbook in various formats.

' Create a new Excel WorkBook document
Private workBook As WorkBook = WorkBook.Create(ExcelFileFormat.XLSX)

' Create two WorkSheets and name them "sheet1" and "sheet2"
Private workSheet1 As WorkSheet = workBook.CreateWorkSheet("sheet1")
Private workSheet2 As WorkSheet = workBook.CreateWorkSheet("sheet2")

' Input data in the first cell of each worksheet
Private workSheet1("A1").StringValue = "Data in A1 of sheet1"
Private workSheet2("A1").StringValue = "Data in A1 of sheet2"

' Save the workbook as a CSV file
workBook.SaveAs("sample.csv", fileFormat:= ExcelFileFormat.CSV)

' Save the workbook as a JSON file
workBook.SaveAs("sample.json", fileFormat:= ExcelFileFormat.JSON)

' Save the workbook as an XML file
workBook.SaveAs("sample.xml", fileFormat:= ExcelFileFormat.XML)

' Export the workbook as an HTML file
workBook.ExportToHtml("sample.html")
$vbLabelText   $csharpLabel

Please note that for CSV, TSV, JSON, and XML file formats, a separate file will be created corresponding to each sheet. The naming convention will be fileName.sheetName.format. Take a look at the following image to illustrate how multiple files are named for CSV, JSON, and XML formats.

Naming format

Export to Inline Code Object

Export Excel workbook to various inline code objects, such as HTML strings, binary data, byte arrays, streams, and even a .NET DataSet. Use the corresponding methods to achieve it, and the returned object is ready to be used for further processing.

:path=/static-assets/excel/content-code-examples/how-to/export-spreadsheet-export-object.cs
using IronXL;
using System.IO;

// Create a new Excel WorkBook document
WorkBook workBook = WorkBook.Create();

// Create a blank WorkSheet with the specified name
WorkSheet workSheet = workBook.CreateWorkSheet("new_sheet");

// Export the workbook to an HTML formatted string
string htmlString = workBook.ExportToHtmlString();

// Export the workbook as a binary array
// This can be useful for situations where you need to work with raw binary data, 
// such as saving to a binary file format or transmitting over a network.
byte[] binary = workBook.ToByteArray(); // Note: ToBinary() method is not valid. It should be ToByteArray().

// Export the workbook as a Stream
// Streams are useful for writing the workbook to a file, sending over a network, etc.
Stream stream = workBook.ToStream();

// Export the workbook as a DataSet
// This allows easy integration with DataGrids, SQL, and Entity Framework (EF).
System.Data.DataSet dataSet = workBook.ToDataSet();
Imports IronXL
Imports System.IO

' Create a new Excel WorkBook document
Private workBook As WorkBook = WorkBook.Create()

' Create a blank WorkSheet with the specified name
Private workSheet As WorkSheet = workBook.CreateWorkSheet("new_sheet")

' Export the workbook to an HTML formatted string
Private htmlString As String = workBook.ExportToHtmlString()

' Export the workbook as a binary array
' This can be useful for situations where you need to work with raw binary data, 
' such as saving to a binary file format or transmitting over a network.
Private binary() As Byte = workBook.ToByteArray() ' Note: ToBinary() method is not valid. It should be ToByteArray().

' Export the workbook as a Stream
' Streams are useful for writing the workbook to a file, sending over a network, etc.
Private stream As Stream = workBook.ToStream()

' Export the workbook as a DataSet
' This allows easy integration with DataGrids, SQL, and Entity Framework (EF).
Private dataSet As System.Data.DataSet = workBook.ToDataSet()
$vbLabelText   $csharpLabel

Frequently Asked Questions

What file formats can IronXL export spreadsheets to?

IronXL can export spreadsheets to several file formats, including XLS, XLSX, XLSM, CSV, TSV, JSON, XML, and HTML.

How do I export a spreadsheet using IronXL in C#?

To export a spreadsheet using IronXL, first load the Excel file using the `WorkBook.Load` method, then use the `SaveAs` method to export it to the desired format.

Which methods are recommended for exporting specific file formats?

While the `SaveAs` method can export to multiple formats, it is recommended to use specific methods like `SaveAsCsv`, `SaveAsJson`, `SaveAsXml`, and `ExportToHtml` for better control.

Can IronXL export a workbook to inline code objects?

Yes, IronXL can export a workbook to various inline code objects such as HTML strings, binary data, byte arrays, streams, and a .NET DataSet.

What is the naming convention for exported files in CSV, JSON, and XML formats?

For CSV, JSON, and XML formats, each sheet will be exported as a separate file, named in the format of **fileName.sheetName.format**.

Where are new Excel files saved by default when using IronXL?

By default, new Excel files are saved in the 'bin > Debug > net6.0' folder of the project.

What is the `DataSet` class in .NET?

The `DataSet` class in .NET is part of ADO.NET technology and is used to interact with data from various sources such as databases and XML.

Chaknith related to Export to Inline Code Object
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.