NPOI in use in alpine

NPOI in use in alpine

Intro

.Net NPOI often used to do import and export of Excel, NPOI from the 2.4.0 version began to support .netstandard2.0, for .net core applications can also be used DotNetCore.NPOI.

For .NET Core applications, if no special needs, alpine is the most appropriate basis docker container of a mirror, because the image size is relatively small, whether it is packaged or for download very quickly.

There is a use NPOI export Excel functions in one of my asp.net core application, my application deployed on k8s by docker, which docker's image is based on the alpine, using an excel export to NPOI

NPOI cross-platform implementations rely on System.Drawing.Common, System.Drawing.Commonto achieve libgdiplus rely on Linux, you need to install libgdiplusto work properly, if you do not libgdiplusencounter such an exception similar to the following:

On Linux System.Drawing.Common

sudo apt-get install libgdiplus libc6-dev
  • Installed on alpine libgdiplus

.netcore package docker mirror when I generally opt for alpine as the basic mirror, because the mirror itself is relatively small, the download package will quickly and very convenient, so he is not looking for what you can install on alpine libgdiplus, if not, then I had to change mirroring the

Libgdiplus found in the packages on alpine sites, https://pkgs.alpinelinux.org/packages?name=libgdiplus&branch=edge

目前仍处于测试阶段,还未正式发布,不过已经可以使用,可以通过下面的命令来在 alpine 上安装

apk add libgdiplus --update-cache --repository http://dl-3.alpinelinux.org/alpine/edge/testing/ --allow-untrusted

More

安装了 libgdiplus 之后,重新部署再导出测试一下,发现还是不行,现在爆的异常如下:

根据异常提示找到异常的源码 https://github.com/tonyqus/npoi/blob/master/main/SS/Util/SheetUtil.cs#L445

看异常提示以及代码应该是没有字体导致的异常,然后就在安装 libgdiplus 之后再安装一下字体,随便找了一个字体安装了,安装的是 terminus-font,装了字体之后再测试,就可以正常导出了~

使用的 Dockerfile ,完整 Dockerfile 见:https://github.com/WeihanLi/ActivityReservation/blob/dev/Dockerfile

FROM microsoft/dotnet:2.2-aspnetcore-runtime-alpine
RUN apk add libgdiplus --update-cache --repository http://dl-3.alpinelinux.org/alpine/edge/testing/ --allow-untrusted && \
    apk add terminus-font
# ...

Reference

Guess you like

Origin www.cnblogs.com/weihanli/p/use-npoi-in-docker-alpine.html