How to Save or Export Spreadsheets

Chaknith related to How to Save or Export Spreadsheets
Chaknith Bin
October 15, 2023
Updated December 10, 2024
Share:

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
using IronXL;

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

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

// Save the excel file as XLS, XLSX, XLSM, CSV, TSV, JSON, XML, HTML
workBook.SaveAs("sample.xls");
Imports IronXL

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

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

' Save the excel file as XLS, XLSX, XLSM, CSV, TSV, JSON, XML, HTML
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;

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

// Create three WorkSheets
WorkSheet workSheet1 = workBook.CreateWorkSheet("sheet1");
WorkSheet workSheet2 = workBook.CreateWorkSheet("sheet2");

// Input information
workSheet1["A1"].StringValue = "A1";
workSheet2["A1"].StringValue = "A1";

// Save as CSV
workBook.SaveAsCsv("sample.csv");

// Save as JSON
workBook.SaveAsJson("sample.json");

// Save as XML
workBook.SaveAsXml("sample.xml");

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

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

' Create three WorkSheets
Private workSheet1 As WorkSheet = workBook.CreateWorkSheet("sheet1")
Private workSheet2 As WorkSheet = workBook.CreateWorkSheet("sheet2")

' Input information
Private workSheet1("A1").StringValue = "A1"
Private workSheet2("A1").StringValue = "A1"

' Save as CSV
workBook.SaveAsCsv("sample.csv")

' Save as JSON
workBook.SaveAsJson("sample.json")

' Save as XML
workBook.SaveAsXml("sample.xml")

' Export the excel file as HTML
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 where two files have been created for CSV, JSON, and XML format.

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 new Excel WorkBook document
WorkBook workBook = WorkBook.Create();

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

// Export to HTML string
string htmlString = workBook.ExportToHtmlString();

// Export as Byte array
byte[] binary = workBook.ToBinary();
byte[] byteArray = workBook.ToByteArray();

// Export as Stream
Stream stream = workBook.ToStream();

// Export as DataSet
System.Data.DataSet dataSet = workBook.ToDataSet(); // Allow easy integration with DataGrids, SQL and EF
Imports IronXL
Imports System.IO

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

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

' Export to HTML string
Private htmlString As String = workBook.ExportToHtmlString()

' Export as Byte array
Private binary() As Byte = workBook.ToBinary()
Private byteArray() As Byte = workBook.ToByteArray()

' Export as Stream
Private stream As Stream = workBook.ToStream()

' Export as DataSet
Private dataSet As System.Data.DataSet = workBook.ToDataSet() ' Allow easy integration with DataGrids, SQL and EF
$vbLabelText   $csharpLabel
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.