Introduction
- We have created Blazor 3.2 WASM with Add/Edit/Delete capabilities and .NET Core in previous articles.
- In this article, we will be adding export capabilities on the home screen with the ability to download Employee data in excel format on the Home screen on the click of a button.
Prerequisites
Refer to my previous article on creating fully functional Blazor WASM with Add/Edit/Delete capabilities.
Output

On a click of Export Button Employee, details are exported in .xlsx Format.
Implementation
Step 1 - Download and set up Gembox NuGet package
Navigate to Tools-->Nuget Package manager and download the latest version of the Gembox spreadsheet and install it in our .NET Core server project.

Import Gembox library in startup.cs with the below statement
- using GemBox.Spreadsheet;
Initialize the Spreadsheet info class and set License Key information.
This needs to be done in startup.cs of Server project in Configure service method
- public void ConfigureServices(IServiceCollection services) {
- services.AddDbContext < AppDbContext > (options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
- services.AddControllersWithViews();
- services.AddRazorPages();
- services.AddScoped < IEmployeeRepository, EmployeeRepository > ();
- SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY");
- }
This completes the set up for Gembox
Step 2 - Creating .NET Core API for excel Export
Import Gembox library using below syntax
- using GemBox.Spreadsheet;
Create a new Route in Employee Controller.
- [Route("Export")]
- [HttpGet]
- public IActionResult Export() {
- var book = new ExcelFile();
- var sheet = book.Worksheets.Add("Employee");
- IList < Employee > data = _employeeRepository.GetAllEmployees().ToList();
- CellStyle style = sheet.Rows[0].Style;
- style.Font.Weight = ExcelFont.BoldWeight;
- style.HorizontalAlignment = HorizontalAlignmentStyle.Center;
- sheet.Columns[0].Style.HorizontalAlignment = HorizontalAlignmentStyle.Center;
- sheet.Columns[0].SetWidth(150, LengthUnit.Pixel);
- sheet.Columns[1].SetWidth(150, LengthUnit.Pixel);
- sheet.Columns[2].SetWidth(150, LengthUnit.Pixel);
- sheet.Columns[3].SetWidth(150, LengthUnit.Pixel);
- sheet.Columns[5].SetWidth(150, LengthUnit.Pixel);
- sheet.Columns[6].SetWidth(300, LengthUnit.Pixel);
- sheet.Cells["A1"].Value = "EmployeeId";
- sheet.Cells["B1"].Value = "First Name";
- sheet.Cells["C1"].Value = "Last Name";
- sheet.Cells["D1"].Value = "EmailId";
- sheet.Cells["E1"].Value = "City";
- sheet.Cells["F1"].Value = "Comment";
- for (int r = 1; r <= data.Count; r++) {
- Employee item = data[r - 1];
- sheet.Cells[r, 0].Value = item.EmployeeId;
- sheet.Cells[r, 1].Value = item.FirstName;
- sheet.Cells[r, 2].Value = item.LastName;
- sheet.Cells[r, 3].Value = item.Email;
- sheet.Cells[r, 4].Value = item.City;
- sheet.Cells[r, 5].Value = item.Comment;
- }
- SaveOptions options = GetSaveOptions("XLSX");
- using(var stream = new MemoryStream()) {
- book.Save(stream, options);
- return File(stream.ToArray(), options.ContentType, "EmployeeData." + "XLSX");
- }
- }
- private static SaveOptions GetSaveOptions(string format) {
- switch (format.ToUpper()) {
- case "XLSX":
- return SaveOptions.XlsxDefault;
- case "XLS":
- return SaveOptions.XlsDefault;
- case "ODS":
- return SaveOptions.OdsDefault;
- case "CSV":
- return SaveOptions.CsvDefault;
- case "HTML":
- return SaveOptions.HtmlDefault;
- case "PDF":
- return SaveOptions.PdfDefault;
- case "XPS":
- case "PNG":
- case "JPG":
- case "GIF":
- case "TIF":
- case "BMP":
- case "WMP":
- throw new InvalidOperationException("To enable saving to XPS or image format, add 'Microsoft.WindowsDesktop.App' framework reference.");
- default:
- throw new NotSupportedException();
- }
- }


Join the conversation! Your thoughts help the community grow.