Create Excel (XLSX) or PDF in Docker container

GemBox.Spreadsheet can be used inside docker containers that are running .NET Docker images.

To use Docker, you must first install Docker Desktop. After that, you can containerize your .NET application.

Visual Studio container tool for adding Docker to existing .NET Core project
Screenshot of Visual Studio container tool

The following is a project file for the .NET Core application with added Docker support.

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

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net8.0</TargetFramework>
    <DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
    <DockerfileContext>.</DockerfileContext>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="GemBox.Spreadsheet" Version="*" />
    <PackageReference Include="SkiaSharp.NativeAssets.Linux" Version="*" />
    <PackageReference Include="HarfBuzzSharp.NativeAssets.Linux" Version="*" />
  </ItemGroup>

</Project>

To configure or customize a Docker image, you'll need to edit the Dockerfile.

When creating PDF or image files, the font files need to be present in the container. The official Linux images for .NET won't have any fonts installed. So, you'll need to copy the necessary font files to the Linux container, or install a font package like ttf-mscorefonts-installer, or add them as custom fonts.

The following example shows how you can create XLSX and PDF files from Docker containers and configure Docker images with Dockerfile.

Generated Excel spreadsheet (XLSX format) and PDF file from Docker container
Screenshot of Excel and PDF files created with Docker container
FROM mcr.microsoft.com/dotnet/runtime:8.0 AS base
WORKDIR /app

FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
WORKDIR /src
COPY ["SpreadsheetDocker.csproj", ""]
RUN dotnet restore "./SpreadsheetDocker.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "SpreadsheetDocker.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "SpreadsheetDocker.csproj" -c Release -o /app/publish /p:UseAppHost=false

FROM base AS final

# Update package sources to include supplemental packages (contrib archive area).
RUN sed -i 's/main/main contrib/g' /etc/apt/sources.list.d/debian.sources

# Downloads the package lists from the repositories.
RUN apt-get update

# Install font configuration.
RUN apt-get install -y fontconfig

# Install Microsoft TrueType core fonts.
RUN apt-get install -y ttf-mscorefonts-installer

# Or install Liberation TrueType fonts.
# RUN apt-get install -y fonts-liberation

# Or some other font package...

WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "SpreadsheetDocker.dll"]
using GemBox.Spreadsheet;
using GemBox.Spreadsheet.Charts;

class Program
{
    static void Main()
    {
        SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY");

        // Create new workbook.
        var workbook = new ExcelFile();

        // Create new worksheet.
        var worksheet = workbook.Worksheets.Add("Sheet1");
        worksheet.PrintOptions.PrintHeadings = true;
        worksheet.PrintOptions.PrintGridlines = true;
        worksheet.PrintOptions.FitWorksheetWidthToPages = 1;

        // Add sample formatting.
        worksheet.Rows[0].Style = workbook.Styles[BuiltInCellStyleName.Heading1];
        worksheet.Columns[0].SetWidth(80, LengthUnit.Pixel);
        worksheet.Columns[1].SetWidth(80, LengthUnit.Pixel);

        // Add sample data.
        worksheet.Cells["A1"].Value = "Name";
        worksheet.Cells["A2"].Value = "John Doe";
        worksheet.Cells["A3"].Value = "Fred Nurk";
        worksheet.Cells["A4"].Value = "Hans Meier";
        worksheet.Cells["A5"].Value = "Ivan Horvat";
        worksheet.Cells["B1"].Value = "Salary";
        worksheet.Cells["B2"].Value = 3600;
        worksheet.Cells["B3"].Value = 2580;
        worksheet.Cells["B4"].Value = 3200;
        worksheet.Cells["B5"].Value = 4100;

        // Add sample chart.
        var chart = worksheet.Charts.Add(ChartType.Bar, "B7", "J20");
        chart.SelectData(worksheet.Cells.GetSubrange("A1:B5"), true);

        // Save spreadsheet in XLSX and PDF format.
        workbook.Save("output.xlsx");
        workbook.Save("output.pdf");
    }
}
Imports GemBox.Spreadsheet
Imports GemBox.Spreadsheet.Charts

Module Program

    Sub Main()

        SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY")

        ' Create new workbook.
        Dim workbook As New ExcelFile()

        ' Create new worksheet.
        Dim worksheet = workbook.Worksheets.Add("Sheet1")
        worksheet.PrintOptions.PrintHeadings = True
        worksheet.PrintOptions.PrintGridlines = True
        worksheet.PrintOptions.FitWorksheetWidthToPages = 1

        ' Add sample formatting.
        worksheet.Rows(0).Style = workbook.Styles(BuiltInCellStyleName.Heading1)
        worksheet.Columns(0).SetWidth(80, LengthUnit.Pixel)
        worksheet.Columns(1).SetWidth(80, LengthUnit.Pixel)

        ' Add sample data.
        worksheet.Cells("A1").Value = "Name"
        worksheet.Cells("A2").Value = "John Doe"
        worksheet.Cells("A3").Value = "Fred Nurk"
        worksheet.Cells("A4").Value = "Hans Meier"
        worksheet.Cells("A5").Value = "Ivan Horvat"
        worksheet.Cells("B1").Value = "Salary"
        worksheet.Cells("B2").Value = 3600
        worksheet.Cells("B3").Value = 2580
        worksheet.Cells("B4").Value = 3200
        worksheet.Cells("B5").Value = 4100

        ' Add sample chart.
        Dim chart = worksheet.Charts.Add(ChartType.Bar, "B7", "J20")
        chart.SelectData(worksheet.Cells.GetSubrange("A1:B5"), True)

        ' Save spreadsheet in XLSX and PDF format.
        workbook.Save("output.xlsx")
        workbook.Save("output.pdf")
    End Sub
End Module

Using full functionality of GemBox.Spreadsheet on a Linux host 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