Categories
Uncategorized

Import and export generic open source library released Magicodes.ExporterAndImporter

 

Import and export common library

Magicodes.ExporterAndImporter team for the heart Levin package import and export common library, and still continue to follow the grinding project.

GitHub address: https: //github.com/xin-lai/Magicodes.ExporterAndImporter

 


 

table of Contents

 

Feature

Relevant official Nuget package

Export Demo

  • Ordinary Export

  • Characteristic deriving

  • Column header processing or multi-language support

Import Demo

  • Normal template

  • Multiple data types

  • data verification

Docker use

Dockerfile Demo


 

Feature

  • Package import and export business, currently only supports Excel, interested partners can be considered small to support export CSV and PDF or other formats

  • Characteristics associated logic configured to control and display the results, without modifying the logic code

  • Recommended fits import and export use DTO

  • Export Support column header custom processing to support multi-language scene

  • Export support for custom filters or text processing

  • Import support template automatically generated and verified in accordance with an import template to import DTO

  • Import support data validation logic

  • Supports data import drop-down selection

  • Import support to add comments

 

Relevant official Nuget package

 

Export Demo

Demo1-1

Ordinary Export

public class ExportTestData
{
    public string Name1 { get; set; }
    public string Name2 { get; set; }
    public string Name3 { get; set; }
    public string Name4 { get; set; }
}

var result = await Exporter.Export(filePath, new List()
{
    new ExportTestData()
    {
        Name1 = "1",
        Name2 = "test",
        Name3 = "12",
        Name4 = "11",
    },
    new ExportTestData()
    {
        Name1 = "1",
        Name2 = "test",
        Name3 = "12",
        Name4 = "11",
    }
});

Demo1-2

Characteristic deriving

[ExcelExporter(Name = "

test

", TableStyle = "Light10")]public class ExportTestDataWithAttrs { [ExporterHeader(DisplayName = "

Bold text

", IsBold = true)] public string Text { get; set; } [ExporterHeader(DisplayName = "

Plain text

")] public string Text2 { get; set; } [ExporterHeader(DisplayName = "

ignore

", IsIgnore = true)] public string Text3 { get; set; } [ExporterHeader(DisplayName = "

Numerical

", Format = "#,##0")] public double Number { get; set; } [ExporterHeader(DisplayName = "

name

", IsAutoFit = true)] public string Name { get; set; } } var result = await Exporter.Export(filePath, new List() { new ExportTestDataWithAttrs() { Text = "

Ah real deal soda sprinkled

", Name="aa", Number =5000, Text2 = "

w 萨达萨达萨 Dasa

", Text3 = "

sadsad sent to kill the literati

" }, new ExportTestDataWithAttrs() { Text = "

Ah real deal soda sprinkled

", Name="

Ah real deal soda sprinkled

", Number =6000, Text2 = "

w 萨达萨达萨 Dasa

", Text3 = "

sadsad sent to kill the literati

" }, new ExportTestDataWithAttrs() { Text = "

Ah honest rate spread soda

", Name="

Sada Sada

", Number =6000, Text2 = "

Suddenly he let others

", Text3 = "

sadsad sent to kill the literati

" }, });

 

Demo1-3

Column header processing or multi-language support

 

 

 

[ExcelExporter(Name = "

test

", TableStyle = "Light10")] public class AttrsLocalizationTestData { [ExporterHeader(DisplayName = "

Bold text

", IsBold = true)] public string Text { get; set; } [ExporterHeader(DisplayName = "

Plain text

")] public string Text2 { get; set; } [ExporterHeader(DisplayName = "

ignore

", IsIgnore = true)] public string Text3 { get; set; } [ExporterHeader(DisplayName = "

Numerical

", Format = "#,##0")] public double Number { get; set; } [ExporterHeader(DisplayName = "

name

", IsAutoFit = true)] public string Name { get; set; } } ExcelBuilder.Create().WithLocalStringFunc((key) => { if (key.Contains("

text

")) { return "Text"; } return "

Unknown Language

"; }).Build(); var filePath = Path.Combine(Directory.GetCurrentDirectory(), "testAttrsLocalization.xlsx"); if (File.Exists(filePath)) File.Delete(filePath); var result = await Exporter.Export(filePath, new List() { new AttrsLocalizationTestData() { Text = "

Ah real deal soda sprinkled

", Name="aa", Number =5000, Text2 = "

w 萨达萨达萨 Dasa

", Text3 = "

sadsad sent to kill the literati

" }, new AttrsLocalizationTestData() { Text = "

Ah real deal soda sprinkled

", Name="

Ah real deal soda sprinkled

", Number =6000, Text2 = "

w 萨达萨达萨 Dasa

", Text3 = "

sadsad sent to kill the literati

" }, new AttrsLocalizationTestData() { Text = "

Ah honest rate spread soda

", Name="

Sada Sada

", Number =6000, Text2 = "

Suddenly he let others

", Text3 = "

sadsad sent to kill the literati

" }, });

 

Import Demo

Import feature (ImporterHeader):

  • Name: String header display name (not null).

  • Description: string header to add comments.

  • Author: string of comments, default is X.M.

Import Results (ImportModel ):

  • Data: Data IList collection of import.

  • ValidationResults: IList data validation results.

  • HasValidTemplate: bool template verifies that it passes.

Data verification result (ValidationResultModel):

  • Index: int error data row.

  • Errors: IDictionary Excel error entire collection. Currently only supports data validation errors.

  • FieldErrors: IDictionary data validation error.

 

Demo2-1 common template

Generate Template

public class ImportProductDto
{
    /// 
    /// 产品名称
    /// 
    [ImporterHeader(Name = "

product name

")] public string Name { get; set; } /// /// 产品代码 /// [ImporterHeader(Name = "

Product Code

")] public string Code { get; set; } /// /// 产品条码 /// [ImporterHeader(Name = "

Product bar code

")] public string BarCode { get; set; } }

 

Import Template

 

 

 

 

Demo2-2 multiple data types

Generate a template

 

 

public class ImportProductDto
{
    /// 
    /// 产品名称
    /// 
    [ImporterHeader(Name = "

Product Name

")] public string Name { get; set; } /// /// 产品代码 /// [ImporterHeader(Name = "

Product Code

")] public string Code { get; set; } /// /// 产品条码 /// [ImporterHeader(Name = "

Product barcodes

")] public string BarCode { get; set; } /// /// 客户Id /// [ImporterHeader(Name = "

Customer code

")] public long ClientId { get; set; } /// /// 产品型号 /// [ImporterHeader(Name = "

Product Model

")] public string Model { get; set; } /// /// 申报价值 /// [ImporterHeader(Name = "

Declared value

")] public double DeclareValue { get; set; } /// /// 货币单位 /// [ImporterHeader(Name = "

Monetary unit

")] public string CurrencyUnit { get; set; } /// /// 品牌名称 /// [ImporterHeader(Name = "

brand name

")] public string BrandName { get; set; } /// /// 尺寸 /// [ImporterHeader(Name = "

Dimensions (L x W x H)

")] public string Size { get; set; } /// /// 重量 /// [ImporterHeader(Name = "

Weight (KG)

")] public double Weight { get; set; } /// /// 类型 /// [ImporterHeader(Name = "

Types of

")] public ImporterProductType Type { get; set; } /// /// 是否行 /// [ImporterHeader(Name = "

Whether row

")] public bool IsOk { get; set; } }

 

public enum ImporterProductType
{
    [Display(Name = "

the first

")] One, [Display(Name = "

second

")] Two }

Import Template

 

 

 

 

Demo2-3 data validation

Generate a template

Required header text is red

public class ImportProductDto
{
    /// 
    /// 产品名称
    /// 
    [ImporterHeader(Name = "

Product Name

",Description ="

Required

")] [Required(ErrorMessage = "

Product name is required

")] public string Name { get; set; } /// /// 产品代码 /// [ImporterHeader(Name = "

Product Code

", Description = "

The maximum length of 8

")] [MaxLength(8,ErrorMessage = "

The maximum length of the product code 8

")] public string Code { get; set; } /// /// 产品条码 /// [ImporterHeader(Name = "

Product bar code

")] [MaxLength(10, ErrorMessage = "

Product bar code maximum length of 10

")] [RegularExpression(@"^\d*$", ErrorMessage = "

Product bar code can only be a number

")] public string BarCode { get; set; } /// /// 客户Id /// [ImporterHeader(Name = "

Customer code

")] public long ClientId { get; set; } /// /// 产品型号 /// [ImporterHeader(Name = "

Product number

")] public string Model { get; set; } /// /// 申报价值 /// [ImporterHeader(Name = "

Declared value

")] public double DeclareValue { get; set; } /// /// 货币单位 /// [ImporterHeader(Name = "

Currency Unit

")] public string CurrencyUnit { get; set; } /// /// 品牌名称 /// [ImporterHeader(Name = "

brand name

")] public string BrandName { get; set; } /// /// 尺寸 /// [ImporterHeader(Name = "

Dimensions (L x W x H)

")] public string Size { get; set; } /// /// 重量 /// [ImporterHeader(Name = "

Weight (KG)

")] public double Weight { get; set; } /// /// 类型 /// [ImporterHeader(Name = "

Types of

")] public ImporterProductType Type { get; set; } /// /// 是否行 /// [ImporterHeader(Name = "

Whether row

")] public bool IsOk { get; set; } } public enum ImporterProductType { [Display(Name = "

the first

")] One, [Display(Name = "

second

")] Two }

 

Import Template

 

 

Docker use

# 安装libgdiplus库,用于Excel导出
RUN apt-get update && apt-get install -y libgdiplus libc6-dev
RUN ln -s /usr/lib/libgdiplus.so /usr/lib/gdiplus.dll

Dockerfile Demo

FROM microsoft/dotnet:2.2-aspnetcore-runtime 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
WORKDIR /app
EXPOSE 80

FROM microsoft/dotnet:2.2-sdk AS build
WORKDIR /src
COPY ["src/web/Admin.Host/Admin.Host.csproj", "src/web/Admin.Host/"]
COPY ["src/web/Admin.Web.Core/Admin.Web.Core.csproj", "src/web/Admin.Web.Core/"]
COPY ["src/application/Admin.Application/Admin.Application.csproj", "src/application/Admin.Application/"]
COPY ["src/core/Magicodes.Admin.Core/Magicodes.Admin.Core.csproj", "src/core/Magicodes.Admin.Core/"]
COPY ["src/data/Magicodes.Admin.EntityFrameworkCore/Magicodes.Admin.EntityFrameworkCore.csproj", "src/data/Magicodes.Admin.EntityFrameworkCore/"]
COPY ["src/core/Magicodes.Admin.Core.Custom/Magicodes.Admin.Core.Custom.csproj", "src/core/Magicodes.Admin.Core.Custom/"]
COPY ["src/application/Admin.Application.Custom/Admin.Application.Custom.csproj", "src/application/Admin.Application.Custom/"]
RUN dotnet restore "src/web/Admin.Host/Admin.Host.csproj"
COPY . .
WORKDIR "/src/src/web/Admin.Host"
RUN dotnet build "Admin.Host.csproj" -c Release -o /app

FROM build AS publish
RUN dotnet publish "Admin.Host.csproj" -c Release -o /app

FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "Magicodes.Admin.Web.Host.dll"]

 

 

Leave a Reply