.net core 使用 FreeSpire.XLS 实现 excel 转 png - docker 部署

背景

前后端分离项目, 后端需返回大量数据, 由于样式复杂,导致前端操作卡顿。通过返回图片解决。

踩坑日记

FreeSpire.XLS && Aspose.Cells包都可以实现。实现过程中发现如下问题:

  1. 本地测试通过, docker部署服务器后报错:
    The type initializer for 'Spire.Xls.Core.Spreadsheet.XlsPageSetupBase' threw an exception.
    由于缺少依赖: libc6-dev,libgdiplus,libx11-dev。由于目标服务器为内网环境,无外网环境。官网给出答案在dockerfile中 增加

    RUN apt-get update \ && apt-get install -y --allow-unauthenticated \ libc6-dev \  libgdiplus \ libx11-dev \   && rm -rf /var/lib/apt/lists/*
    

    由于国内网络非常慢,且服务器为内网环境,在线安装直接否定了。

    尝试解决1: 手动安装依赖,libc6-dev为例

    # 进入一个联网服务器的docker容器, 默认镜像 
    docker exec -it xxx /bin/bash (mcr.microsoft.com/dotnet/aspnet:5.0 为基础镜像)
    # 清除缓存包
    sudo apt-get clean
    
    #只读,下载libc6-dev 及其所有依赖。
    sudo apt-get -d install libc6-dev  
    
    #进入缓存目录
    cd /var/cache/apt/archives 
    
    # 此时可以看到所有依赖。拷贝至目标服务器,离线安装所有依赖。
    sudo dpkg -i *.deb
    
    # 再次运行, 还是报错
    # 由于基础镜像不同, 部分依赖已安装,版本号无法对应,且后期更新每次都需手动安装依赖,太麻烦。
    

    尝试解决2: 创建docker镜像。由于项目已 mcr.microsoft.com/dotnet/aspnet:5.0 为基础镜像,以此新建一个环境镜像。

    # 进入容器部署环境
    docker run -it mcr.microsoft.com/dotnet/aspnet:5.0 /bin/bash
    apt-get update \ && apt-get install -y --allow-unauthenticated \ libc6-dev \  libgdiplus \ libx11-dev \   && rm -rf /var/lib/apt/lists/*
    
    #部署成功后, 退出容器,并将容器打包为新镜像。
    docker commit -a="djc" -m="add libc6-dev,libgdiplus,libx11-dev based on .netcore5.0" 28a66ebccd55 dotnetcore-djc:5.2
    # -a :作者; -m: 备注信息 ; 28a66xxx : 容器id(可通过docker ps -a 查看);  
    
    #新的镜像名称为:dotnetcore-djc:5.2
    

    vs项目dockerfile中修改基础镜像:FROM dotnetcore-djc:5.2 AS base

  2. 发布后,又报错:System.TypeInitializationException: The type initializer for ‘Gdip’ threw an exception.
    google后发现 libgdiplus 包使用了 System.Drawing.Common, .net6后仅在window上支持,我目前使用.net5, 也是平台兼容性报错,不知为何。
    尝试解决: 在项目中添加如下代码:

    	AppContext.SetSwitch("System.Drawing.EnableUnixSupport", true);
    
  3. 发布后,又报错Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index')
    尝试解决:
    升级 FreeSpire.XLS 包, 由原来的 10.10.0 升级为 12.7.0

  4. 导出图片后,发现中文字体乱码。
    尝试解决:

    #将window中 C:\Windows\fonts 内需要得字体,拷贝至容器内 /usr/share/fonts/windows文件夹内。
    #更改字体库的权限
    chmod 755 /usr/share/fonts/windows/*
    
    #进入文件夹内
    mkfontscale  //字体扩展
    mkfontdir  //新增字体目录
    fc-cache-fv  //刷新缓存
    
    #肯定会报错:mkfontscale: command not found 等,
    # 由于Docker mcr.microsoft.com/dotnet/aspnet:5.0 基础镜像中不包含该包,需手动安装。
    apt-get update && apt-get upgrade 
    apt-get install ttf-mscorefonts-installer // 安装mkfontscale  mkfontdir  命令
    
    #报错: package 'ttf-mscorefonts-intaller' has no installation candidate 错误。
    #由于系统初始的资源库找不到指定的包,需要更新 对应 的镜像地址。
    #我的/etc/apt/sources.list 中, 初始镜像地址为 Debian buster  ,所以添加如下镜像至sources.list中。
    #如果添加版本错误的话,update 会报错。
    #华为云
    deb https://mirrors.huaweicloud.com/debian/ buster main contrib non-free
    deb https://mirrors.huaweicloud.com/debian/ buster-updates main contrib non-free
    deb https://mirrors.huaweicloud.com/debian/ buster-backports main contrib non-free
    deb https://mirrors.huaweicloud.com/debian-security/ buster/updates main contrib non-free
    
    deb-src https://mirrors.huaweicloud.com/debian/ buster main contrib non-free
    deb-src https://mirrors.huaweicloud.com/debian/ buster-updates main contrib non-free
    deb-src https://mirrors.huaweicloud.com/debian/ buster-backports main contrib non-free 
    
    #中科大
    deb https://mirrors.ustc.edu.cn/debian/ buster main contrib non-free
    deb https://mirrors.ustc.edu.cn/debian/ buster-updates main contrib non-free
    deb https://mirrors.ustc.edu.cn/debian/ buster-backports main contrib non-free
    deb https://mirrors.ustc.edu.cn/debian-security/ buster/updates main contrib non-free
    
    deb-src https://mirrors.ustc.edu.cn/debian/ buster main contrib non-free
    deb-src https://mirrors.ustc.edu.cn/debian/ buster-updates main contrib non-free
    deb-src https://mirrors.ustc.edu.cn/debian/ buster-backports main contrib non-free
    deb-src https://mirrors.ustc.edu.cn/debian-security/ buster/updates main contrib non-free
    
    # 添加后刷新
    apt-get update && apt-get upgrade 
    apt-get install ttf-mscorefonts-installer // 安装mkfontscale  mkfontdir  命令
    apt-get install fontconfig //安装 fc-cache 命令
    

重新部署后,一切正常。

参考
[1]: https://www.e-iceblue.com/forum/exception-the-type-initializer-for-spire-xls-core-spreadsh-t10260.html
[2]: https://docs.telerik.com/reporting/knowledge-base/system-drawing-common-is-not-supported-on-non-windows-platforms
[3]: https://www.lllxy.net/Blog/Detail/634b2769-5046-45eb-b71b-fe2a87b7c1fe

Free Spire.XLS for .NET 是e-iceblue公司开发的一款支持对所有Excel格式类型文件进行操作的.NET 控件。它适用于任何类型的应用程序比如ASP.NET Web应用程序或者Windows桌面应用程序。Spire.XLS for .NET允许开发人员创建和管理Excel,而不需要安装Microsoft Excel或者Microsoft Office。相比Microsoft,Spire.XLS for .NET对于开发人员来说是一个更好的选择 - 自动化,安全,稳定,可扩展性延伸,速度快,价格便宜和功能强大。 通过使用Free Spire.XLS for .NET,开发人员可以快速方便的查看,生成,读写以及自定义编辑Excel文件。Spire.XLS for .NET支持Excel 97-2003, Excel 2007, Excel 2010 and Excel 2013。它还支持C#, VB.NET, ASP.NET和 ASP.NET MVC编程语言 作为一款专业,强大,方便易用的控件,Spire.XLS for .NET有一下主要功能: 新建Excel文件,编辑已有的Excel文件,多种保存Excel方式,设置Excel文件属性,文件打印设置,合并,拆分或者拷贝单元格,支持对Excel文件加锁或者加密保护。 Spire.XLS for .NET支持把数据从数据库导出到Excel工作表,或者把数据从Excel数据导入到数据库。 Spire.XLS for .NET 提供强大的功能为开发者设置单元格格式。这次格式化数字,文字缩进,定位调整,设置单元格边线,单元格填充以及保护等。 开发人员可以轻松的利用Spire.XLS for .NETExcel文件里面写入内容,包括超过530多种的公式,70多种表格,支持插入图片,不同类型的文字,链接,注释甚至VBA等。 Spire.XLS for .NET 还允许开发人员将Excel文件换成PDF, HTML, CSV, Text, XML以及图片格式。 注:下载包含有Spire.xls.dll文件以及部分Demo, 你可以调用DLL并参照Demo提供的代码处理Excel文档。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值