This chapter mainly explains the use of Magicodes.IE, configuration in a Docker environment.
- Excel export via Dto
- Exporting PDF
- Docker Configuration
Install-Package Magicodes.IE.Excel
Install-Package Magicodes.IE.Pdf
- Export 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);
}
With the above code we have created an export demo, The specific characteristics and properties can be seen in the previous two articles. 基础教程之导出Excel 、基础教程之导出Pdf收据
#It is recommended that you build with this base image for the reasons given below. This image build has the libgdiplus library installed.
FROM ccr.ccs.tencentyun.com/magicodes/aspnetcore-runtime:latest AS base
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"]
# If you do not use the above base image, then you need to add the following command to install the 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 font file
COPY /simsun.ttc /usr/share/fonts/simsun.ttc
Note that the above base image uses:(ccr.ccs.tencentyun.com/magicodes/aspnetcore-runtime:latest) ,该镜像GitHub地址:(https://github.com/xin-lai/aspnetcore-docker)。
Reasons for Recommendation.
- Speed up image building and pulling, accelerate CI/CD building and improve development experience
- Time zone is set to East 8 by default, see "ENV TZ=Asia/Shanghai"
- By default, libgdiplus and other libraries are installed to support Excel import and export(No installation is required after version 2.7)
- At present, we provide Tencent Cloud public mirror and hub.docker public mirror, you can use them as needed.