Magicodes.IE 在 Docker 中使用
更新历史
2019.02.13
[Nuget] 版本更新到 2.0.2
[导入] 修复单列导入的 Bug, 单元测试 "OneColumnImporter_Test". 问题见 ().
[导出] 修复导出 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 地址:( https://github.com/xin-lai/aspnetcore-docker ).
推荐理由:
加快镜像构建和拉取速度, 加速 CI\CD 构建以及提高开发体验
时区默认设置为东八区, 见 "ENV TZ=Asia/Shanghai"
默认安装了 libgdiplus 等库, 以便支持 Excel 导入导出
目前提供了腾讯云的公共镜像和 hub.docker 的公共镜像, 大家可以按需
- Reference
- https://github.com/dotnetcore/Magicodes.IE
来源: https://www.cnblogs.com/yyfh/p/12310628.html