Test in production without watermarks.
Works wherever you need it to.
Get 30 days of fully functional product.
Have it up and running in minutes.
Full access to our support engineering team during your product trial
Looking for a way to export data to an Excel file using Razor Pages? This article instructs on how to do so using IronXL for .NET.
CreateWorkSheet
method to create WorkSheetIronXL is a .NET library used for working with Excel files. It is built on top of OpenXML SDK and provides a simple API for reading, writing, and manipulating Excel spreadsheets. IronXL can be used in a variety of .NET applications, including Web Applications, Desktop Applications, and Console Applications. IronXL provides a range of features for working with Excel documents, including the ability to read and write data to individual cells, create charts and graphs, and perform complex calculations. It also supports advanced Excel features such as pivot tables, conditional formatting, and data validation.
Exporting data to Excel and CSV files is a common task in web development, and Razor and IronXL make it easy to accomplish. With IronXL, developers can easily create Excel spreadsheets and save them to a file or stream. Razor can then be used to generate dynamic content that includes links to the Excel document, allowing users to download them directly from the web page. The CSV file format can also be generated using IronXL and downloaded similarly using Razor. This makes it simple to create reports and other data-driven content that can be easily shared with others. Overall, the combination of Razor and IronXL .NET Core Razor Pages provides a powerful toolset for web developers looking to create dynamic, data-driven web pages.
Before using Razor in Visual Studio, there are a few prerequisites that users should have in place. Here are some of the important ones:
To create a new Visual Studio project, follow these steps:
Click on "Create a New Project" in the start window, or click on "File" > "New" > "Project" in the menu bar.
Visual Studio's start window
Select the type of project you want to create, to use Razor Pages, select ASP.NET Core Web App and click on Next.
Create a new .NET Core Web App in Visual Studio
A new window will appear, write your new project name and location. Then click on Next.
Configure the new project
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. Open the manager and search for IronXL in the browse tab. Choose IronXL from the search results and install it.
The below screenshot shows how to open the NuGet Package Manager in Visual Studio.
Navigate to NuGet Package Manager
IronXL in search results:
Search for IronXL in NuGet Package Manager UI
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:
Install-Package IronXL.Excel
Now the package will download/install to the current project and be ready to use.
Install IronXL in the Package Manager Console
Once the project is created, a simple interface of the Razor application is created. Now change the interface to export to Excel export.
The Web interface of the newly created project
Change the above interface by editing the index.cshtml
file in the Pages
directory.
@page
@model IndexModel
@{
ViewData["Title"] = "Home page";
}
<div class="text-center">
<h1 class="display-4">IronXL Generate Excel File</h1>
<p class="m-5">This will be the combination of Razor and IronXL, together they will Export data to Excel file.</p>
<form method="post" asp-page-handler="Export">
<button class="btn btn-success p-3">Export Excel File!</button>
</form>
</div>
@page
@model IndexModel
@{
ViewData["Title"] = "Home page";
}
<div class="text-center">
<h1 class="display-4">IronXL Generate Excel File</h1>
<p class="m-5">This will be the combination of Razor and IronXL, together they will Export data to Excel file.</p>
<form method="post" asp-page-handler="Export">
<button class="btn btn-success p-3">Export Excel File!</button>
</form>
</div>
page model ReadOnly Property () As IndexModel
ViewData("Title") = "Home page"
End Property
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'<div class="text-center"> <h1 class="display-4"> IronXL Generate Excel File</h1> <p class="m-5"> This will be the combination @of Razor @and IronXL, together they will Export data @to Excel file.</p> <form method="post" asp-page-handler="Export"> <button class="btn btn-success p-3"> Export Excel File!</button> </form> </div>
This code will change the interface of the home page and add the export button.
The new interface with an export button
Now create the OnPostExport
method in the index.cshtml.cs
file and write the following code. This code generates a worksheet, writes column names in bold, and allows setting other styling options like font size and background color. You can think of it as a simple example of exporting data from a database to an Excel file.
public FileResult OnPostExport()
{
// Create a new workbook with the Excel file format
WorkBook workBook = new WorkBook(IronXL.ExcelFileFormat.XLSX);
// Create a new worksheet in the workbook
WorkSheet workSheet = workBook.CreateWorkSheet("data");
// Add data and styles to the new worksheet
workSheet["A1"].Value = "Name";
workSheet["B1"].Value = "Email";
workSheet["C1"].Value = "Marks";
workSheet["D1"].Value = "Percentage";
// Set styling for column headers
workSheet["A1:D1"].Style.Font.Bold = true;
// Filling the worksheet with data
workSheet["A2"].Value = "Nick";
workSheet["A3"].Value = "Roy";
workSheet["A4"].Value = "Chris";
workSheet["B2"].Value = "Nick@gmail.com";
workSheet["B3"].Value = "Roy123@gmail.com";
workSheet["B4"].Value = "Chris1999@gmail.com";
workSheet["C2"].Value = "850";
workSheet["C3"].Value = "710";
workSheet["C4"].Value = "990";
workSheet["D2"].Value = "85%";
workSheet["D3"].Value = "71%";
workSheet["D4"].Value = "99%";
// Return the file as an Excel sheet
return File(workBook.ToStream().ToArray(), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "Exported.xlsx");
}
public FileResult OnPostExport()
{
// Create a new workbook with the Excel file format
WorkBook workBook = new WorkBook(IronXL.ExcelFileFormat.XLSX);
// Create a new worksheet in the workbook
WorkSheet workSheet = workBook.CreateWorkSheet("data");
// Add data and styles to the new worksheet
workSheet["A1"].Value = "Name";
workSheet["B1"].Value = "Email";
workSheet["C1"].Value = "Marks";
workSheet["D1"].Value = "Percentage";
// Set styling for column headers
workSheet["A1:D1"].Style.Font.Bold = true;
// Filling the worksheet with data
workSheet["A2"].Value = "Nick";
workSheet["A3"].Value = "Roy";
workSheet["A4"].Value = "Chris";
workSheet["B2"].Value = "Nick@gmail.com";
workSheet["B3"].Value = "Roy123@gmail.com";
workSheet["B4"].Value = "Chris1999@gmail.com";
workSheet["C2"].Value = "850";
workSheet["C3"].Value = "710";
workSheet["C4"].Value = "990";
workSheet["D2"].Value = "85%";
workSheet["D3"].Value = "71%";
workSheet["D4"].Value = "99%";
// Return the file as an Excel sheet
return File(workBook.ToStream().ToArray(), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "Exported.xlsx");
}
Public Function OnPostExport() As FileResult
' Create a new workbook with the Excel file format
Dim workBook As New WorkBook(IronXL.ExcelFileFormat.XLSX)
' Create a new worksheet in the workbook
Dim workSheet As WorkSheet = workBook.CreateWorkSheet("data")
' Add data and styles to the new worksheet
workSheet("A1").Value = "Name"
workSheet("B1").Value = "Email"
workSheet("C1").Value = "Marks"
workSheet("D1").Value = "Percentage"
' Set styling for column headers
workSheet("A1:D1").Style.Font.Bold = True
' Filling the worksheet with data
workSheet("A2").Value = "Nick"
workSheet("A3").Value = "Roy"
workSheet("A4").Value = "Chris"
workSheet("B2").Value = "Nick@gmail.com"
workSheet("B3").Value = "Roy123@gmail.com"
workSheet("B4").Value = "Chris1999@gmail.com"
workSheet("C2").Value = "850"
workSheet("C3").Value = "710"
workSheet("C4").Value = "990"
workSheet("D2").Value = "85%"
workSheet("D3").Value = "71%"
workSheet("D4").Value = "99%"
' Return the file as an Excel sheet
Return File(workBook.ToStream().ToArray(), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "Exported.xlsx")
End Function
This above code will create a workbook and return the file as an Excel sheet. This method is linked to the button, and once the button is clicked, it will automatically create and download the XLSX exported file.
Export an XLSX file in the browser
The data in the exported XLSX file
You can easily export CSV text files with one change in the above code. Just replace the return file code with the following, and your Razor page will return a CSV file.
return File(workBook.ToStream().ToArray(), "text/csv", "Student.csv");
return File(workBook.ToStream().ToArray(), "text/csv", "Student.csv");
Return File(workBook.ToStream().ToArray(), "text/csv", "Student.csv")
Export a CSV file in the browser
The data in the exported CSV file
Exporting data to Excel sheets and CSV files is a common task in web development, and Razor and IronXL make it easy to accomplish. With IronXL, developers can easily create Excel spreadsheets and save them to a file or stream. Razor can then be used to generate dynamic content that includes links to the Excel document, allowing users to download them directly from the web page. This makes it simple to create reports and other data-driven content that can be easily shared with others.
The combination of Razor and IronXL .NET Core Razor Pages provides a powerful toolset for web developers looking to create dynamic, data-driven web pages. To start using Razor and IronXL, one needs to have Visual Studio and .NET Core SDK installed. After that, creating a new Visual Studio project and installing the IronXL library can be done easily. Finally, by changing the interface of the home page and creating an OnPostExport
method, developers can export data to Excel documents in a few simple steps.
To learn more about IronXL, Razor and how to export data to Excel and CSV files, please visit the following page.
IronXL is free to use for non-commercial development purposes. A free trial is available for testing in production. Look at the pricing plans for more details about prices and licensing.
Users can also benefit from Iron Suite, a Suite of 5 professional ASP.NET Core libraries including IronXL, IronPDF, and more.
IronXL is a .NET library used for working with Excel files. It provides a simple API for reading, writing, and manipulating Excel spreadsheets and can be used in various .NET applications.
You can install IronXL using the NuGet Package Manager in Visual Studio by searching for IronXL and installing it, or by using the Visual Studio Command-Line with the command Install-Package IronXL.Excel.
Yes, you can export data to both Excel and CSV formats using Razor and IronXL. The library allows developers to create spreadsheets and save them in different formats, including XLSX and CSV.
To use Razor with IronXL, you need to have Visual Studio and the .NET Core SDK installed on your computer. You also need to set up your web development environment before starting to work with Razor View.
Open Visual Studio, click on 'Create a New Project', select ASP.NET Core Web App, and follow the prompts to configure your project. Ensure your target framework is set correctly and configure for HTTPS.
Create a new workbook using IronXL, add data to a worksheet, and then return the file as an Excel sheet. This can be done in a Razor Page by creating an OnPostExport method linked to a button.
To export data as a CSV file, replace the return file code in the Razor page to specify the format as 'text/csv'. This allows the page to return a CSV file instead of an Excel file.
IronXL is free to use for non-commercial development purposes. A free trial is available for testing in production, and pricing plans are available for commercial use.
Razor and IronXL allow web developers to easily export data to Excel and CSV files, creating dynamic, data-driven web pages. They simplify the creation of reports and other data-driven content that can be easily shared.