Create Excel or PDF files on Azure

The following examples demonstrate how to use GemBox.Spreadsheet in Azure Functions and Azure App Services.

Azure Functions

To create an Azure function, search for "Azure Functions" in a new project dialog.

Screenshot of Visual Studio project type selection
Screenshot of Visual Studio project type selection

To avoid any potential issues with GemBox.Spreadsheet, we recommend using the newer versions of Azure Functions (v3 or above). The following is an Azure function project file:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net8.0</TargetFramework>
    <AzureFunctionsVersion>v4</AzureFunctionsVersion>
    <OutputType>Exe</OutputType>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="GemBox.Spreadsheet" Version="*" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.23.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.2.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.18.1" />
  </ItemGroup>
  <ItemGroup>
    <None Update="host.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Update="local.settings.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <CopyToPublishDirectory>Never</CopyToPublishDirectory>
    </None>
  </ItemGroup>
</Project>

The following example shows how you can create an Excel spreadsheet using GemBox.Spreadsheet in an Azure Function.

using GemBox.Spreadsheet;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Http;
using System.IO;
using System.Net;
using System.Threading.Tasks;

public class GemBoxFunction
{
    [Function("GemBoxFunction")]
    public async Task<HttpResponseData> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get")] HttpRequestData req)
    {
        // If using the Professional version, put your serial key below.
        SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY");

        var workbook = new ExcelFile();
        var worksheet = workbook.Worksheets.Add("Hello World");

        worksheet.Cells[0, 0].Value = "Hello";
        worksheet.Cells[0, 1].Value = "World";

        var fileName = "Output.xlsx";
        var options = SaveOptions.XlsxDefault;

        using var stream = new MemoryStream();
        workbook.Save(stream, options);
        var bytes = stream.ToArray();

        var response = req.CreateResponse(HttpStatusCode.OK);
        response.Headers.Add("Content-Type", options.ContentType);
        response.Headers.Add("Content-Disposition", "attachment; filename=" + fileName);
        await response.Body.WriteAsync(bytes, 0, bytes.Length);
        return response;
    }
}
Imports GemBox.Spreadsheet
Imports Microsoft.Azure.Functions.Worker
Imports Microsoft.Azure.Functions.Worker.Http
Imports System.IO
Imports System.Net
Imports System.Threading.Tasks

Public Class GemBoxFunction
    <[Function]("GemBoxFunction")>
    Public Async Function Run(<HttpTrigger(AuthorizationLevel.Anonymous, "get")> req As HttpRequestData) As Task(Of HttpResponseData)

        ' If using the Professional version, put your serial key below.
        SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY")

        Dim workbook As New ExcelFile()
        Dim worksheet = workbook.Worksheets.Add("Hello World")

        worksheet.Cells(0, 0).Value = "Hello"
        worksheet.Cells(0, 1).Value = "World"

        Dim fileName = "Output.xlsx"
        Dim options = SaveOptions.XlsxDefault

        Using stream As New MemoryStream()
            workbook.Save(stream, options)
            Dim bytes = stream.ToArray()

            Dim response = req.CreateResponse(HttpStatusCode.OK)
            response.Headers.Add("Content-Type", options.ContentType)
            response.Headers.Add("Content-Disposition", "attachment; filename=" & fileName)
            Await response.Body.WriteAsync(bytes, 0, bytes.Length)
            Return response
        End Using

    End Function
End Class
Generated Excel spreadsheet (XLSX format) from Azure Functions
Screenshot of Excel file created with Azure function

Note that saving to an XPS currently works only on Azure Functions that target .NET Framework 4.8.

Azure App Services

App Service is a fully managed platform for building, deploying, and scaling web apps. To publish an application to Azure App service, you need to:

1. Pick Azure as publish target.

Screenshot of Visual Studio target selection
Screenshot of Visual Studio target selection

2. Pick Azure App Service as a specific target.

Screenshot of Visual Studio specific target selection
Screenshot of Visual Studio specific target selection

3. Specify existing App Service or create a new one.

Screenshot of Visual Studio specific App Service selection
Screenshot of Visual Studio specific App Service selection

Using full functionality of GemBox.Spreadsheet in an App Service application requires adjustments explained in detail on Supported Platforms help page.

See also


Next steps

GemBox.Spreadsheet is a .NET component that enables you to read, write, edit, convert, and print spreadsheet files from your .NET applications using one simple API.

Download Buy