ASP.NET open source import and export library used in Magicodes.IE Docker

Magicodes.IE used in Docker

Update history
2019.02.13
Update Nuget to 2.0.2
[import] to repair the single column imported Bug, unit test "onecolumnimporter" '. See you. https://github.com/dotnetcore/Magicodes.IE/issues/35).
[export] fixed the problem of template compiling and error reporting in some cases when exporting HTML, Pdf and Word.
Import rewrites the blank line check.
2019.02.14
Update Nuget to 2.1.0
[export] PDF export supports. NET 4.6.1. See unit test for details

Explain

This chapter mainly describes the configuration of using Magicodes.IE in Docker environment

Main points

  • Excel export through Dto
  • Export PDF data
  • Docker configuration

Example

Export example:

Install-Package Magicodes.IE.Excel
Install-Package Magicodes.IE.Pdf
  • Export Excel
    [ExcelExporter(Name = "Student information", TableStyle = "Light10", AutoFitAllColumn = true,
        MaxRowNumberOnASheet = 2)]
    public class StudentExcel
    {

        /// <summary>
        /// name
        /// </summary>
        [ExporterHeader(DisplayName = "Full name")]
        public string Name { get; set; }
        /// <summary>
        //Age
        /// </summary>
        [ExporterHeader(DisplayName = "Age")]
        public int Age { get; set; }
        /// <summary>
        /// remarks
        /// </summary>
        public string Remarks { get; set; }
        /// <summary>
        ///Date of birth
        /// </summary>
        [ExporterHeader(DisplayName = "Date of birth", 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 = "My name is MR.A,18 years old",
                        Birthday=DateTime.Now
                    },
                    new StudentExcel
                    {
                        Name = "MR.B",
                        Age = 19,
                        Remarks = "My name is MR.B,19 years old",
                        Birthday=DateTime.Now
                    },
                    new StudentExcel
                    {
                        Name = "MR.C",
                        Age = 20,
                        Remarks = "My name is MR.C,20 years old",
                        Birthday=DateTime.Now
                    }
                });
            return File("test.xlsx", "application/ms-excel", result.FileName);
        }
  • Export PDF
    [PdfExporter(Name = "Student information")]
    public class StudentPdf
    {
        /// <summary>
        /// name
        /// </summary>
        [ExporterHeader(DisplayName = "Full name")]
        public string Name { get; set; }
        /// <summary>
        //Age
        /// </summary>
        [ExporterHeader(DisplayName = "Age")]
        public int Age { get; set; }
        /// <summary>
        /// remarks
        /// </summary>
        public string Remarks { get; set; }
        /// <summary>
        ///Date of birth
        /// </summary>
        [ExporterHeader(DisplayName = "Date of birth", 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 = "My name is MR.A,18 years old",
                        Birthday=DateTime.Now
                    },
                    new StudentPdf
                    {
                        Name = "MR.B",
                        Age = 19,
                        Remarks = "My name is MR.B,19 years old",
                        Birthday=DateTime.Now
                    },
                    new StudentPdf
                    {
                        Name = "MR.C",
                        Age = 20,
                        Remarks = "My name is MR.C,20 years old",
                        Birthday=DateTime.Now
                    }
            });
            return File("test.pdf", "application/pdf", result.FileName);
        }

Through the above code, we create an export example,
See the previous two articles for specific property attributes Export of basic course to Excel ,Export of basic course Pdf receipt

Dockerfile configuration

FROM ccr.ccs.tencentyun.com/magicodes/aspnetcore-runtime:latest AS base
# Install libgdiplus library for Excel export
#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"]
# Install libgdiplus library for Excel export
RUN apt-get update && apt-get install -y libgdiplus libc6-dev
RUN ln -s /usr/lib/libgdiplus.so /usr/lib/gdiplus.dll
# Install fontconfig library for Pdf export
RUN apt-get update && apt-get install -y fontconfig
COPY /simsun.ttc /usr/share/fonts/simsun.ttc

Note that the above basic image uses: (ccr.ccs.tencentyun.com/magicodes/aspnetcore-runtime:latest), the image GitHub address:( https://github.com/xin-lai/aspnetcore-docker).

Reasons for recommendation:

  • Speed up image building and pulling, CI\CD building and development experience
  • By default, the time zone is set to zone 8 in the East, see "ENV TZ=Asia/Shanghai"
  • libgdiplus and other libraries are installed by default to support Excel import and export
  • At present, Tencent cloud's public image and hub.docker's public image are provided. You can use them on demand

Reference

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

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

Keywords: ASP.NET IE Excel Docker github

Added by fsumba on Sat, 15 Feb 2020 18:41:12 +0200