Overview
In this tutorial I will show you how you can use the ClosedXML nuget package to export data in Excel format using an ASP.NET 6.0 Razor Pages web app. The data we will is is hard-coded student data. However, the approach works for data from any data source.
Source code: https://github.com/medhatelmasry/ExcelStar.git
Companion Video: https://youtu.be/8olaWPDKXyU
The environment I am using is:
- https://github.com/medhatelmasry/OrdersChartRazorGoogleWrapper
- .NET version 6.0.100
- Visual Studio Code
Project setup
Run the following command to create an ASP.NET Core Razor Pages application using .NET 6.0 in a folder named ExcelStar:
dotnet new razor -f net6.0 -o ExcelStar
Change directory into the new folder:
cd ExcelStar
Install the ClosedXML nuget package with:
dotnet add package ClosedXML
Open the project inside VS Code with the following commands:
code .
Sample student data
Create a folder named Data. Inside the Data folder, create a Student.cs class file and add to it the following Student code:
public int? Id { get; set; }
public string? FirstName { get; set; }
public string? LastName { get; set; }
public string? School { get; set; }
public static IEnumerable<Student> GetStudents() {
int ndx = 0;
List<Student> students = new List<Student>() {
new Student() { Id = ++ndx, FirstName="Max", LastName="Pao", School="Science" },
new Student() { Id = ++ndx, FirstName="Tom", LastName="Fay", School="Mining" },
new Student() { Id = ++ndx, FirstName="Ann", LastName="Sun", School="Nursing" },
new Student() { Id = ++ndx, FirstName="Joe", LastName="Fox", School="Computing" },
new Student() { Id = ++ndx, FirstName="Sue", LastName="Mai", School="Mining" },
new Student() { Id = ++ndx, FirstName="Ben", LastName="Lau", School="Business" },
new Student() { Id = ++ndx, FirstName="Zoe", LastName="Ray", School="Mining" },
new Student() { Id = ++ndx, FirstName="Sam", LastName="Ash", School="Medicine" },
new Student() { Id = ++ndx, FirstName="Dan", LastName="Lee", School="Computing" },
new Student() { Id = ++ndx, FirstName="Pat", LastName="Day", School="Science" },
new Student() { Id = ++ndx, FirstName="Kim", LastName="Rex", School="Computing" },
new Student() { Id = ++ndx, FirstName="Tim", LastName="Ram", School="Business" },
new Student() { Id = ++ndx, FirstName="Rob", LastName="Wei", School="Mining" },
new Student() { Id = ++ndx, FirstName="Jan", LastName="Tex", School="Science" },
new Student() { Id = ++ndx, FirstName="Jim", LastName="Kid", School="Business" },
new Student() { Id = ++ndx, FirstName="Ben", LastName="Chu", School="Medicine" },
new Student() { Id = ++ndx, FirstName="Mia", LastName="Tao", School="Computing" },
new Student() { Id = ++ndx, FirstName="Ted", LastName="Day", School="Business" },
new Student() { Id = ++ndx, FirstName="Amy", LastName="Roy", School="Science" },
new Student() { Id = ++ndx, FirstName="Ian", LastName="Kit", School="Nursing" },
new Student() { Id = ++ndx, FirstName="Liz", LastName="Tan", School="Medicine" },
new Student() { Id = ++ndx, FirstName="Mat", LastName="Roy", School="Tourism" },
new Student() { Id = ++ndx, FirstName="Deb", LastName="Luo", School="Medicine" },
new Student() { Id = ++ndx, FirstName="Ana", LastName="Poe", School="Computing" },
new Student() { Id = ++ndx, FirstName="Lyn", LastName="Raj", School="Science" },
new Student() { Id = ++ndx, FirstName="Amy", LastName="Ash", School="Tourism" },
new Student() { Id = ++ndx, FirstName="Kim", LastName="Kid", School="Mining" },
new Student() { Id = ++ndx, FirstName="Bec", LastName="Fry", School="Nursing" },
new Student() { Id = ++ndx, FirstName="Eva", LastName="Lap", School="Computing" },
new Student() { Id = ++ndx, FirstName="Eli", LastName="Yim", School="Business" },
new Student() { Id = ++ndx, FirstName="Sam", LastName="Hui", School="Science" },
new Student() { Id = ++ndx, FirstName="Joe", LastName="Jin", School="Mining" },
new Student() { Id = ++ndx, FirstName="Liz", LastName="Kuo", School="Agriculture" },
new Student() { Id = ++ndx, FirstName="Ric", LastName="Mak", School="Tourism" },
new Student() { Id = ++ndx, FirstName="Pam", LastName="Day", School="Computing" },
new Student() { Id = ++ndx, FirstName="Stu", LastName="Gad", School="Business" },
new Student() { Id = ++ndx, FirstName="Tom", LastName="Bee", School="Tourism" },
new Student() { Id = ++ndx, FirstName="Bob", LastName="Lam", School="Agriculture" },
new Student() { Id = ++ndx, FirstName="Jim", LastName="Ots", School="Medicine" },
new Student() { Id = ++ndx, FirstName="Tom", LastName="Mag", School="Mining" },
new Student() { Id = ++ndx, FirstName="Hal", LastName="Doe", School="Agriculture" },
new Student() { Id = ++ndx, FirstName="Roy", LastName="Kim", School="Nursing" },
new Student() { Id = ++ndx, FirstName="Vis", LastName="Cox", School="Science" },
new Student() { Id = ++ndx, FirstName="Kay", LastName="Aga", School="Tourism" },
new Student() { Id = ++ndx, FirstName="Reo", LastName="Hui", School="Business" },
new Student() { Id = ++ndx, FirstName="Bob", LastName="Roe", School="Medicine" },
};
return students;
}
}
Export data to Excel format
@page@model ExcelModel
What does the above code do?
- Students are read into a variable named data.
- A worksheet is created named Students with column titles Id, First, Last & School
- The Students worksheet is added to the workbook.
- The background color of the column titles is made to be the Almond color.
- Thereafter, each row is filled with student data whereby Id goes into column 1, FirstName goes into column 2, LastName goes into column 3 and School goes into column 4.
- The workbook is saved as a stream.
- The stream is returned to the user as a file with a fixed name and an appropriate HTTP content type.
No comments:
Post a Comment