ASP.NET 開源導入導出庫Magicodes.IE Docker中使用

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,具體見單元測試git

說明

本章主要說明使用Magicodes.IE,在Docker環境中的配置.github

要點

  • 經過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收據docker

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)。shell

推薦理由:bash

  • 加快鏡像構建和拉取速度,加速CI\CD構建以及提升開發體驗
  • 時區默認設置爲東八區,見「ENV TZ=Asia/Shanghai」
  • 默認安裝了libgdiplus等庫,以便支持Excel導入導出
  • 目前提供了騰訊雲的公共鏡像和hub.docker的公共鏡像,你們能夠按需

Reference

https://github.com/dotnetcore/Magicodes.IEapp

https://github.com/hueifeng/BlogSample/tree/master/src/Magicodes.IE.Exporterasync

相關文章
相關標籤/搜索