magicodes.ie在docker中使用

更新历史
2019.02.13
【nuget】版本更新到2.0.2
【导入】修复单列导入的bug,单元测试“onecolumnimporter_test”。问题见(https://github.com/dotnetcore/magicodes.ie/issues/35)。
【导出】修复导出html、pdf、word时,模板在某些情况下编译报错的问题。
【导入】重写空行检查。
2019.02.14
【nuget】版本更新到2.1.0
【导出】pdf导出支持.net 4.6.1,具体见单元测试

说明

本章主要说明使用magicodes.ie,在docker环境中的配置.

要点

  • 通过dto进行excel导出
  • 导出pdf数据
  • docker配置

示例

导出示例:

install-package magicodes.ie.excel
install-package magicodes.ie.pdf
  • 导出excel
    [excelexporter(name = "学生信息", tablestyle = "light10", autofitallcolumn = true,
        maxrownumberonasheet = 2)]
    public class studentexcel
    {

        /// <summary>
        ///     姓名
        /// </summary>
        [exporterheader(displayname = "姓名")]
        public string name { get; set; }
        /// <summary>
        ///     年龄
        /// </summary>
        [exporterheader(displayname = "年龄")]
        public int age { get; set; }
        /// <summary>
        ///     备注
        /// </summary>
        public string remarks { get; set; }
        /// <summary>
        ///     出生日期
        /// </summary>
        [exporterheader(displayname = "出生日期", format = "yyyy-mm-dd")]
        public datetime birthday { get; set; }
    }
        public async task<iactionresult> exporterexcel() {
            iexporter exporter = new excelexporter();
           
            var result = await exporter.export(path.combine("wwwroot","test.xlsx"), new list<studentexcel>()
                {
                    new studentexcel
                    {
                        name = "mr.a",
                        age = 18,
                        remarks = "我叫mr.a,今年18岁",
                        birthday=datetime.now
                    },
                    new studentexcel
                    {
                        name = "mr.b",
                        age = 19,
                        remarks = "我叫mr.b,今年19岁",
                        birthday=datetime.now
                    },
                    new studentexcel
                    {
                        name = "mr.c",
                        age = 20,
                        remarks = "我叫mr.c,今年20岁",
                        birthday=datetime.now
                    }
                });
            return file("test.xlsx", "application/ms-excel", result.filename);
        }
  • 导出pdf
    [pdfexporter(name = "学生信息")]
    public class studentpdf
    {
        /// <summary>
        ///     姓名
        /// </summary>
        [exporterheader(displayname = "姓名")]
        public string name { get; set; }
        /// <summary>
        ///     年龄
        /// </summary>
        [exporterheader(displayname = "年龄")]
        public int age { get; set; }
        /// <summary>
        ///     备注
        /// </summary>
        public string remarks { get; set; }
        /// <summary>
        ///     出生日期
        /// </summary>
        [exporterheader(displayname = "出生日期", format = "yyyy-mm-dd")]
        public datetime birthday { get; set; }
    }
        public async task<iactionresult> exporterpdf() {
            var exporter = new pdfexporter();
            var result = await exporter.exportlistbytemplate(path.combine("wwwroot", "test.pdf"), new list<studentpdf>()
            {
                 new studentpdf
                    {
                        name = "mr.a",
                        age = 18,
                        remarks = "我叫mr.a,今年18岁",
                        birthday=datetime.now
                    },
                    new studentpdf
                    {
                        name = "mr.b",
                        age = 19,
                        remarks = "我叫mr.b,今年19岁",
                        birthday=datetime.now
                    },
                    new studentpdf
                    {
                        name = "mr.c",
                        age = 20,
                        remarks = "我叫mr.c,今年20岁",
                        birthday=datetime.now
                    }
            });
            return file("test.pdf", "application/pdf", result.filename);
        }

通过上述代码我们创建了一个导出示例,
具体特性属性可以看一下前两篇文章 基础教程之导出excel 、基础教程之导出pdf收据

dockerfile配置

from ccr.ccs.tencentyun.com/magicodes/aspnetcore-runtime:latest as base
# 安装libgdiplus库,用于excel导出
#run apt-get update && apt-get install -y libgdiplus libc6-dev
#run ln -s /usr/lib/libgdiplus.so /usr/lib/gdiplus.dll

#run apt-get update && apt-get install -y fontconfig
workdir /src
run ls
copy /src/magicodes.ie.exporter/simsun.ttc /usr/share/fonts/simsun.ttc

workdir /app
expose 80
expose 443

from mcr.microsoft.com/dotnet/core/sdk:latest as build
workdir /src
copy ["magicodes.ie.exporter.csproj", "src/magicodes.ie.exporter/"]
run dotnet restore "src/magicodes.ie.exporter/magicodes.ie.exporter.csproj"
copy . .
workdir "src/magicodes.ie.exporter"
run dotnet build "magicodes.ie.exporter.csproj" -c release -o /app/build

from build as publish
run dotnet publish "magicodes.ie.exporter.csproj" -c release -o /app/publish

from base as final
workdir /app
copy --from= publish /app/publish .
entrypoint ["dotnet", "magicodes.ie.exporter.dll"]
# 安装libgdiplus库,用于excel导出
run apt-get update && apt-get install -y libgdiplus libc6-dev
run ln -s /usr/lib/libgdiplus.so /usr/lib/gdiplus.dll
# 安装fontconfig库,用于pdf导出
run apt-get update && apt-get install -y fontconfig
copy /simsun.ttc /usr/share/fonts/simsun.ttc

注意,以上基础镜像使用:(ccr.ccs.tencentyun.com/magicodes/aspnetcore-runtime:latest) ,该镜像github地址:()。

推荐理由:

  • 加快镜像构建和拉取速度,加速ci\cd构建以及提高开发体验
  • 时区默认设置为东八区,见“env tz=asia/shanghai”
  • 默认安装了libgdiplus等库,以便支持excel导入导出
  • 目前提供了腾讯云的公共镜像和hub.docker的公共镜像,大家可以按需

reference

https://github.com/dotnetcore/magicodes.ie

https://github.com/hueifeng/blogsample/tree/master/src/magicodes.ie.exporter