简体   繁体   中英

Is it possible to use EPPlus in dotnet core app running in a windows docker container?

I have a dotnet core 2.1 web api that has an action that generates a .xlsx spreadsheet. It creates a FileStreamResult that the browser can then handle. The code to generate the spreadsheet is like so:

using (var excelFile = new ExcelPackage())
using (var worksheet = excelFile.Workbook.Worksheets.Add("Sheet 1"))
{
    ...
    //insert data into worksheet

    return new FileStreamResult(new MemoryStream(excelFile.GetAsByteArray()), "application/octet-stream") { FileDownloadName = "Report.xlsx" };
}

Hosting this in IIS works fine and will generate the spreadsheet. When I host the app in a windows docker container specifically the image: microsoft/dotnet:2.1-aspnetcore-runtime-nanoserver-sac2016 I get the following exception when trying to generate the report:

System.TypeInitializationException: The type initializer for 'Gdip' threw an exception. ---> System.DllNotFoundException: Unable to load DLL 'gdiplus.dll'

After a bit of research I realise gdiplus is not present in the nanoserver image.

Is it possible to use EPPlus to create a spreadsheet on a dotnet core app hosted in the nanoserver image? Or will I have to use another library to generate the xlsx? I'd like to use EPPlus if possible. I cannot use a linux container (for now anyway unfortunately)

I don't know if it will work, but try commenting out any of the methods from the library that uses System.Drawing.Common , such as AutoFitColumns() . That is what was causing it to break on my linux container.

You can build you own ubtuntu/dotnetcore image! I just did it myself for this very problem.

From a command line:

docker pull ubuntu

docker run -t -t ubuntu:latest /bin/bash -> to open a container shell ->

    apt-get update

    apt-get install wget    

    wget -q https://packages.microsoft.com/config/ubuntu/18.04/packages-microsoft-prod.deb -O packages-microsoft-prod.deb

    dpkg -i packages-microsoft-prod.deb

    apt-get install apt-transport-https

    apt-get update

    apt-get install dotnet-sdk-2.2

Install the special libraries we need to resolve the System.Drawing/EPPlus problem here:

    apt-get install libgdiplus

    cd /usr/lib

    ln -s libgdiplus.so gdiplus.dll

    apt-get install libc6-dev libx11-dev

    rm -rf /var/lib/apt/lists/*

Find the container process id in a second command line window and commit the image:

docker ps 


    CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
    4975113bb79        ubuntu:latest       "/bin/bash"         9 minutes ago       Up 9 minutes                            infallible_poincare

docker commit d497 custom/imagename:dotnetcore-2.2-custom

docker ps kill d497

docker image rm ubuntu

Now you have a custom ubuntu image(the same image that dotnetcore uses) with the sdk loaded and the libraries you need to stop the errors.

The image could probably be trimmed down quite a bit, be warned, this was my first successful attempt at solving this problem where other methods I found online did not succeed due to corporate firewall restrictions and such.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM