How to Export to Excel in ASP.NET Core

In today's fast-paced digital world, sharing and exchanging data across various platforms has become a fundamental requirement for individuals and businesses alike. One of the most widely used formats for sharing and presenting data is Microsoft Excel. Excel offers a multitude of functionalities that allow users to create and manipulate complex spreadsheets with ease. However, sometimes the need arises to share data in other formats, such as PDF or CSV. In such cases, users can export their Excel files to different formats, which not only enables seamless data exchange but also allows the data to be viewed and analyzed in different ways. In this article, we will explore how you can export data in an Excel file programmatically using ASP.NET Core.

1. IronXL for .NET Core

IronXL is a powerful ASP.NET entity framework core library that provides developers with a new user convenient and efficient way to work with Excel files in ASP.NET Core applications. With IronXL, developers can read, write, and manipulate Excel files, perform data analysis and processing, and generate reports with ease. ASP.NET Core is a widely used web application framework that allows developers to create dynamic web applications using a variety of programming languages, including C#. One of the key components of many web applications is the ability to read and write data to and from Excel files.

2. Prerequisites

  1. Visual Studio: To create an ASP.NET project, you must have Visual Studio installed on your system. If you don't have Visual Studio, you can download it from the Microsoft website.
  2. ASP.NET: Before creating an ASP.NET project, you must ensure that ASP.NET is installed on your system. You can check this by going to the Control Panel and looking for the "Turn Windows features on or off" option. Make sure that the "ASP.NET" option is checked.
  3. IronXL: To export Excel to different formats, you must install the IronXL library. You can do this by downloading the IronXL NuGet package from the NuGet package manager within Visual Studio.

Once you have installed Visual Studio, ASP.NET, and IronXL, you can create a new ASP.NET project in Visual Studio and begin writing code to export Excel to different formats using IronXL.

3. Creating New ASP.NET Core Project

To make use of the IronXL library, you must create a .NET project in Visual Studio. While any version of Visual Studio can be used, it is recommended to use the latest version. You can choose from various project templates, such as Windows Forms, depending on your requirements. To simplify the process for this tutorial, we will be using the Console Application.

New project window

New project window

After selecting the project type, provide a name for the project and designate its location. Then, choose the desired framework for the project, such as .NET Core 6.

Project configuration

Project configuration

Once the solution is created, the "Program.cs" file will be opened, enabling you to enter code and construct/run the application.

New project

New project

Finally, you can incorporate the library to test the code.

4. Install IronXL

The IronXL Library can be downloaded and installed in many different ways.

These are:

  • Using Visual Studio NuGet packages
  • Using the Visual Studio Command-Line.

4.1 Using Visual Studio

To install the IronXL library, we can use the NuGet Package Manager. Open the manager and search for IronXL in the browse tab. Choose IronXL from the search results and install it. Once the installation is complete, our project will be set up to use the IronXL library.

The screenshot below shows how to open the NuGet Package Manager in Visual Studio:

NuGet Package Manager

NuGet Package Manager

IronXL in search results:

Search result

Search result

4.2 Using the Visual Studio Command-Line

Using a console to perform operations is a popular choice for many. If you prefer to install IronXL through the command line, you can follow these steps:

  • In Visual Studio, go to Tools-> NuGet Package manager -> Package manager console.
  • Enter the following line in the package manager console tab: Install-Package IronXL.Excel

Now the package will download and install to the current project and be ready to use.

Installing project dependencies

Installing project dependencies

5. Export Data from Excel

Using IronXL, you can export a database table of an Excel file to CSV files, XML files, and even JSON files. You can also read data from real Excel files and export to other formats, such as Excel. Using IronXL controller, you can convert and stream worksheets, model classes, models, card footers, and even embed colors from Excel exports to different formats.

5.1. Excel to CSV

Converting Excel files to CSV format is a common requirement for data processing and exchange. Converting CSV files manually can be quite bothersome. Here, we will use IronXL to convert an Excel file to a CSV file. The source code below shows how you can easily convert an XLSX file to a CSV file in just a few lines of code.

using IronXL;
using System;
using System.Linq;

WorkBook workBook = WorkBook.Load("multiple.xlsx");
workBook.SaveAsCsv("sample.csv");
using IronXL;
using System;
using System.Linq;

WorkBook workBook = WorkBook.Load("multiple.xlsx");
workBook.SaveAsCsv("sample.csv");
Imports IronXL
Imports System
Imports System.Linq

Private workBook As WorkBook = WorkBook.Load("multiple.xlsx")
workBook.SaveAsCsv("sample.csv")
VB   C#
CSV file

CSV file

5.2. Excel to XML

Converting Excel files to XML format can be useful for exchanging data between different systems, services, or applications.

Similarly to converting to CSV, you can easily convert an Excel file to XML using IronXL in just a few lines of code.

using IronXL;
using System;
using System.Linq;

WorkBook workBook = WorkBook.Load("multiple.xlsx");
workBook.SaveAsXml("sample.xml");
using IronXL;
using System;
using System.Linq;

WorkBook workBook = WorkBook.Load("multiple.xlsx");
workBook.SaveAsXml("sample.xml");
Imports IronXL
Imports System
Imports System.Linq

Private workBook As WorkBook = WorkBook.Load("multiple.xlsx")
workBook.SaveAsXml("sample.xml")
VB   C#
File output

File output

5.3. Excel to JSON

Converting an Excel file to JSON format can be useful when you want to display the data on a web page or when you need to publish the data on a public interface on the web. Using IronXL, you can easily convert an Excel file to a JSON file.

using IronXL;
using System;
using System.Linq;

WorkBook workBook = WorkBook.Load("multiple.xlsx");
workBook.SaveAsJson("sample.json");
using IronXL;
using System;
using System.Linq;

WorkBook workBook = WorkBook.Load("multiple.xlsx");
workBook.SaveAsJson("sample.json");
Imports IronXL
Imports System
Imports System.Linq

Private workBook As WorkBook = WorkBook.Load("multiple.xlsx")
workBook.SaveAsJson("sample.json")
VB   C#
JSON file

JSON file

6. Conclusion

Exporting Excel data to different formats is essential for exchanging and sharing data in today's fast-paced digital world. With the help of IronXL library and ASP.NET Core, developers can easily read, write, manipulate, and export Excel files to various formats such as CSV, XML, and JSON. The process of exporting data is straightforward and can be achieved in just a few lines of code. By leveraging IronXL and ASP.NET Core, developers can create robust and dynamic applications that can handle Excel data with ease and efficiency. Therefore, exporting Excel data to different formats using IronXL and ASP.NET Core can significantly enhance the functionality and usability of an application.

To learn more about IronXL and exporting Excel to different formats visit the following link. For exporting DataTtable please visit this link.

Users can also benefit from Iron Suite, a Suite of five professional libraries including IronXL, IronPDF, and more.