Test in a live environment
Test in production without watermarks.
Works wherever you need it to.
This article will explore how to use C# to export data from a dataset to an Excel spreadsheet using the market-leading C# Excel library, IronXL.
IronXL is a popular C# library that provides developers with the ability to read, write, and manipulate Excel files programmatically. With its simple and intuitive interface, IronXL makes it easy for developers to integrate Excel functionality into their C# applications. This powerful library supports various Excel file formats, including XLSX, XLS, and CSV, and offers a wide range of features such as cell styling, formula calculation, data validation, and charting.
With IronXL, developers can automate tasks related to Excel, such as importing data from external sources, generating reports, and exporting data to different file formats. Additionally, IronXL is highly efficient and capable of handling large Excel files with ease.
To export data from a DataTable
to Excel using the IronXL library, you need to meet a few prerequisites:
DataTable
to an Excel file. You can install it by downloading the IronXL NuGet package from the NuGet Package Manager within Visual Studio.To utilize the IronXL library with Excel, you need to create a .NET project in Visual Studio. You can use any version of Visual Studio, but it is recommended to use the latest version available. Depending on your requirements, you can choose from various project templates, such as Windows Forms or ASP.NET. For this tutorial, the Console Application project template is recommended to demonstrate the usage of IronXL.
Create a New Project Window
After selecting the project type, provide a name for the project and designate its location. Choose the desired framework, such as .NET Core 6, for the project.
Project configuration
Once the solution is created, the program.cs file will be opened, enabling you to enter code and construct/run the application.
Project with code open
Finally, you can incorporate the library to test the code.
The IronXL Library can be downloaded and installed in different ways.
These are:
To install the IronXL library, the simplest approach is to use the NuGet Package Manager in Visual Studio. Simply open the NuGet Package Manager and search for IronXL in the Browse tab. Once you have located IronXL in the search results, select it and proceed with the installation. Once the installation is complete, you can start using the IronXL library in your project.
The below screenshot shows how to open the NuGet Package Manager in Visual Studio.
NuGet Package Manager
IronXL in search results:
IronXL search result
Many developers prefer to install packages using the command line interface. To install IronXL using the command line, follow these steps:
Install-Package IronXL.Excel
Now the package will download/install to the current project and be ready to use.
Installing via command line
Creating an Excel file from a dataset using IronXL is straightforward, and anyone familiar with setting up the environment and running a program can easily convert a dataset to an Excel file with ease. The following section will explore how to convert data from CSV and XML file formats to an Excel sheet within the Microsoft Excel application using IronXL.
The following code example shows how to convert CSV files to XLSX file format using IronXL in C# with just a few lines of code.
using IronXL;
WorkBook workbook = WorkBook.LoadCSV("demo.csv", fileFormat: ExcelFileFormat.XLSX, ListDelimiter: ",");
workbook.SaveAs("Csv_To_Excel.xlsx");
using IronXL;
WorkBook workbook = WorkBook.LoadCSV("demo.csv", fileFormat: ExcelFileFormat.XLSX, ListDelimiter: ",");
workbook.SaveAs("Csv_To_Excel.xlsx");
Imports IronXL
Private workbook As WorkBook = WorkBook.LoadCSV("demo.csv", fileFormat:= ExcelFileFormat.XLSX, ListDelimiter:= ",")
workbook.SaveAs("Csv_To_Excel.xlsx")
CSV file
Output spreadsheet file
Also with just a little tweak, you can convert a CSV file to an XLS file. The below code will show how.
You can also convert a CSV file to an XLS file by modifying a single line of code in the above example. Replace the last line of code with this line, and the output file that gets generated will be in XLS format.
workbook.SaveAs("Csv_To_Excel.xls");
workbook.SaveAs("Csv_To_Excel.xls");
workbook.SaveAs("Csv_To_Excel.xls")
Spreadsheet file
Exporting a dataset to an Excel spreadsheet using C# is a crucial functionality that can save developers significant time and effort. Fortunately, IronXL, a powerful C# library, makes this task easy by providing capabilities to read, write, and manipulate Excel files programmatically. With IronXL, developers can automate various Excel-related tasks, including importing data from external sources, generating reports, and exporting data to different file formats.
To use IronXL with Excel, developers need to create a .NET project in Visual Studio, install the IronXL library, and then convert the dataset to an Excel file. This process is straightforward, and with just a few lines of code, developers can convert a CSV file or any other data format to an Excel file in either XLS or XLSX format.
Please visit another tutorial for a detailed tutorial on how to convert CSV to Excel using IronXL,
To gain more insights about IronXL and how to export a DataTable
to various Excel formats, please visit the following blog for DataTable
instructions. Additionally, you can refer to the pricing plans to obtain detailed information about prices and licensing.
Users can also benefit from Iron Suite, a suite of five professional libraries including IronXL, IronPDF, and others.
9 .NET API products for your office documents