Create an Excel (XLSX) or PDF file in ASP.NET Core
GemBox.Spreadsheet provides a support for .NET Standard which is why it can be used on various .NET platforms such as .NET Framework, .NET Core, Xamarin, and Universal Windows Platform (UWP).
Save to Excel or PDF in .NET Core
To create a Excel spreadsheet or PDF file with an application that targets .NET Core 2.1 or above on Linux, macOS, or Windows using GemBox.Spreadsheet, simply add the following package references in the project file.
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="GemBox.Spreadsheet" Version="*" />
<PackageReference Include="System.Drawing.Common" Version="*" />
</ItemGroup>
</Project>
GemBox.Spreadsheet is ideal for web applications (like ASP.NET Core, ASP.NET MVC, and ASP.NET Web Forms) because of its fast performance and thread safety when working with multiple ExcelFile
objects.
The following example shows how you can create a simple ASP.NET Core application that exports data to an Excel file in the specified format and downloads it to the client's browser.


@model SpreadsheetCore.Controllers.WorkbookModel
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta name="viewport" content="width=device-width">
<meta charset="utf-8" />
<title>Create an Excel Workbook</title>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<style>
body { padding: 20px; font-size: 0.9rem; }
thead tr { text-align: center; }
.table th { color: white; background-color: rgb(242, 101, 34); padding: 0.5rem; }
.table td { padding: 0.5rem; }
.table input { font-size: 0.9rem; padding: 0.3rem 0.35rem; }
.first-column { text-align: center; }
</style>
</head>
<body>
<form asp-action="Create">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<table class="table table-bordered table-condensed">
<colgroup>
<col style="width: 10%" />
<col style="width: 45%" />
<col style="width: 45%" />
</colgroup>
<thead>
<tr>
<th>ID</th>
<th>First Name</th>
<th>Last Name</th>
</tr>
</thead>
<tbody>
@for (int i = 0; i < Model.Items.Count; i++)
{
<tr>
<td><input asp-for="Items[i].Id" type="text" class="form-control-plaintext first-column" readonly /></td>
<td><input asp-for="Items[i].FirstName" type="text" class="form-control" /></td>
<td><input asp-for="Items[i].LastName" type="text" class="form-control" /></td>
</tr>
}
</tbody>
</table>
<hr />
<div>
<h4>Output format:</h4>
<div class="form-check">
<input id="PDF" name="SelectedFormat" type="radio" class="form-check-input" value="PDF" />
<label for="PDF" class="form-check-label">PDF</label>
</div>
<div class="form-check">
<input id="XLSX" name="SelectedFormat" type="radio" class="form-check-input" checked value="XLSX" />
<label for="XLSX" class="form-check-label">XLSX</label>
</div>
<div class="form-check">
<input id="XLS" name="SelectedFormat" type="radio" class="form-check-input" value="XLS" />
<label for="XLS" class="form-check-label">XLS</label>
</div>
<div class="form-check">
<input id="ODS" name="SelectedFormat" type="radio" class="form-check-input" value="ODS" />
<label for="ODS" class="form-check-label">ODS</label>
</div>
<div class="form-check">
<input id="CSV" name="SelectedFormat" type="radio" class="form-check-input" value="CSV" />
<label for="CSV" class="form-check-label">CSV</label>
</div>
<div class="form-check">
<input id="HTML" name="SelectedFormat" type="radio" class="form-check-input" value="HTML" />
<label for="HTML" class="form-check-label">HTML</label>
</div>
<fieldset disabled>
<div class="form-check">
<input id="XPS" name="SelectedFormat" type="radio" class="form-check-input" value="XPS" />
<label for="XPS" class="form-check-label">XPS</label>
</div>
<div class="form-check">
<input id="PNG" name="SelectedFormat" type="radio" class="form-check-input" value="PNG" />
<label for="PNG" class="form-check-label">PNG</label>
</div>
<div class="form-check">
<input id="JPG" name="SelectedFormat" type="radio" class="form-check-input" value="JPG" />
<label for="JPG" class="form-check-label">JPG</label>
</div>
<div class="form-check">
<input id="GIF" name="SelectedFormat" type="radio" class="form-check-input" value="GIF" />
<label for="GIF" class="form-check-label">GIF</label>
</div>
<div class="form-check">
<input id="TIF" name="SelectedFormat" type="radio" class="form-check-input" value="TIF" />
<label for="TIF" class="form-check-label">TIF</label>
</div>
<div class="form-check">
<input id="BMP" name="SelectedFormat" type="radio" class="form-check-input" value="BMP" />
<label for="BMP" class="form-check-label">BMP</label>
</div>
<div class="form-check">
<input id="WMP" name="SelectedFormat" type="radio" class="form-check-input" value="WMP" />
<label for="WMP" class="form-check-label">WMP</label>
</div>
</fieldset>
</div>
<hr />
<button type="submit" class="btn btn-default">Export</button>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.AspNetCore.Mvc;
using GemBox.Spreadsheet;
namespace SpreadsheetCore.Controllers
{
public class WorkbookController : Controller
{
private static readonly IList<WorkbookItemModel> data = new List<WorkbookItemModel>()
{
new WorkbookItemModel() { Id = 100, FirstName = "John", LastName = "Doe"},
new WorkbookItemModel() { Id = 101, FirstName = "Fred", LastName = "Nurk"},
new WorkbookItemModel() { Id = 102, FirstName = "Hans", LastName = "Meier"},
new WorkbookItemModel() { Id = 103, FirstName = "Ivan", LastName = "Horvat"},
new WorkbookItemModel() { Id = 104, FirstName = "Jean", LastName = "Dupont"},
new WorkbookItemModel() { Id = 105, FirstName = "Mario", LastName = "Rossi"},
};
static WorkbookController()
{
SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY");
}
[HttpGet]
public IActionResult Create()
{
return View(new WorkbookModel()
{
Items = data,
SelectedFormat = "XLSX"
});
}
[HttpPost, ValidateAntiForgeryToken]
public IActionResult Create(WorkbookModel model)
{
if (!ModelState.IsValid)
return View(model);
var book = new ExcelFile();
var sheet = book.Worksheets.Add("Sheet1");
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(50, LengthUnit.Pixel);
sheet.Columns[1].SetWidth(150, LengthUnit.Pixel);
sheet.Columns[2].SetWidth(150, LengthUnit.Pixel);
sheet.Cells["A1"].Value = "ID";
sheet.Cells["B1"].Value = "First Name";
sheet.Cells["C1"].Value = "Last Name";
for (int r = 1; r <= model.Items.Count; r++)
{
WorkbookItemModel item = model.Items[r - 1];
sheet.Cells[r, 0].Value = item.Id;
sheet.Cells[r, 1].Value = item.FirstName;
sheet.Cells[r, 2].Value = item.LastName;
}
SaveOptions options = GetSaveOptions(model.SelectedFormat);
using (var stream = new MemoryStream())
{
book.Save(stream, options);
return File(stream.ToArray(), options.ContentType, "Create." + model.SelectedFormat.ToLower());
}
}
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();
}
}
}
public class WorkbookModel
{
public string SelectedFormat { get; set; }
public IList<WorkbookItemModel> Items { get; set; }
}
public class WorkbookItemModel
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
}
GemBox.Spreadsheet is only licensed per developer and the licenses include a royalty-free deployment. There are no server or OEM licenses, there are no additional costs for anything (like building, testing and deploying).
So, you're free to build an unlimited number of applications and deploy or distribute them to an unlimited number of servers or end user machines with no extra cost.
Print and Save to XPS or image in .NET Core
The .NET Standard version of GemBox.Spreadsheet has the full functionality of the .NET Framework version, but with a few rendering limitations:
- Printing workbooks.
- Saving workbooks to XPS and image formats.
- Calling
ConvertToImageSource
andConvertToXpsDocument
methods.
However, GemBox.Spreadsheet provides a .NET Core 3.1 library that has no rendering limitations. This version includes support for printing and saving to XPS or image formats, when used in a .NET Core 3.1 or above application running on Windows.
But when used on a non-Windows platform then the same .NET Standard limitations are applied. For more information see the Linux / macOS example.
To create an XPS or image file from ASP.NET Core 3.1 web application using GemBox.Spreadsheet, you'll need to add Microsoft.WindowsDesktop.App
framework reference in the project file as shown below.
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<FrameworkReference Include="Microsoft.WindowsDesktop.App" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="GemBox.Spreadsheet" Version="*" />
</ItemGroup>
</Project>
To create an XPS or image file from .NET Core 3.1 desktop application using GemBox.Spreadsheet, you'll need to use Microsoft.NET.Sdk.WindowsDesktop
SDK and add UseWPF
property in the project file as shown below.
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<PropertyGroup>
<UseWPF>true</UseWPF>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="GemBox.Spreadsheet" Version="*" />
</ItemGroup>
</Project>