Create 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.0 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.0</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.

Sending form data from Razor view in ASP.NET Core demo application
Screenshot of submitted web form Razor view in ASP.NET Core
Generated XLSX with the exported data from an ASP.NET Core demo application
Screenshot of exported Excel file from an ASP.NET Core
@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 full functionality of .NET Framework version, but with few rendering limitations:

However, GemBox.Spreadsheet provides a version for .NET Core 3.0 that has no rendering limitations. This version includes support for printing and saving to XPS or image formats, when used in .NET Core 3.0 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 Linux / macOS example.

To create an XPS or image file from ASP.NET Core 3.0 web application using GemBox.Spreadsheet, you'll need to add Microsoft.WindowsDesktop.App framework reference in the project file.

<Project Sdk="Microsoft.NET.Sdk.Web">

 <PropertyGroup>
    <TargetFramework>netcoreapp3.0</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.0 desktop application using GemBox.Spreadsheet, you'll need to use Microsoft.NET.Sdk.WindowsDesktop SDK and add UseWPF property in the project file.

<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp3.0</TargetFramework>
  </PropertyGroup>

  <PropertyGroup>
    <UseWPF>true</UseWPF>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="GemBox.Spreadsheet" Version="*" />
  </ItemGroup>

</Project>

Want more?

Next example GitHub

Check the next example or select an example from the menu. You can also download our examples from the GitHub.


Like it?

Download Buy

If you want to try the GemBox.Spreadsheet yourself, you can download the free version. It delivers the same performance and set of features as the professional version, but with some operations limited. To remove the limitation, you need to purchase a license.