開源導入導出通用庫Magicodes.ExporterAndImporter發佈

  • 2019 年 10 月 3 日
  • 筆記

 

導入導出通用庫

Magicodes.ExporterAndImporter為心萊團隊封裝的導入導出通用庫,並且仍在跟隨項目不斷地打磨。

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

 


 

目錄

 

特點

相關官方Nuget包

導出 Demo

  • 普通導出

  • 特性導出

  • 列頭處理或者多語言支持

導入Demo

  • 普通模板

  • 多數據類型

  • 數據驗證

Docker中使用

Dockerfile Demo


 

特點

  • 封裝導入導出業務,目前僅支持 Excel,有興趣的小夥伴可以考慮支持 CSV 以及PDF或其他格式的導出

  • 配置特性即可控制相關邏輯和顯示結果,無需修改邏輯代碼

  • 推薦配合 導入導出DTO 使用

  • 導出支持列頭自定義處理以便支持多語言等場景

  • 導出支持文本自定義過濾或處理

  • 導入支持自動根據導入DTO生成導入模板及模板驗證

  • 導入支持數據驗證邏輯

  • 導入支持數據下拉選擇

  • 導入支持注釋添加

 

相關官方Nuget包

 

導出 Demo

Demo1-1

普通導出

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<ExportTestData>()  {      new ExportTestData()      {          Name1 = "1",          Name2 = "test",          Name3 = "12",          Name4 = "11",      },      new ExportTestData()      {          Name1 = "1",          Name2 = "test",          Name3 = "12",          Name4 = "11",      }  });


Demo1-2

特性導出

[ExcelExporter(Name = "測試", TableStyle = "Light10")]public class ExportTestDataWithAttrs  {      [ExporterHeader(DisplayName = "加粗文本", IsBold = true)]      public string Text { get; set; }        [ExporterHeader(DisplayName = "普通文本")]      public string Text2 { get; set; }        [ExporterHeader(DisplayName = "忽略", IsIgnore = true)]      public string Text3 { get; set; }        [ExporterHeader(DisplayName = "數值", Format = "#,##0")]      public double Number { get; set; }        [ExporterHeader(DisplayName = "名稱", IsAutoFit = true)]      public string Name { get; set; }  }          var result = await Exporter.Export(filePath, new List<ExportTestDataWithAttrs>()          {              new ExportTestDataWithAttrs()              {                  Text = "啊實打實大蘇打撒",                  Name="aa",                  Number =5000,                  Text2 = "w薩達薩達薩達撒",                  Text3 = "sadsad打發打發士大夫的"              },             new ExportTestDataWithAttrs()              {                  Text = "啊實打實大蘇打撒",                  Name="啊實打實大蘇打撒",                  Number =6000,                  Text2 = "w薩達薩達薩達撒",                  Text3 = "sadsad打發打發士大夫的"              },             new ExportTestDataWithAttrs()              {                  Text = "啊實打實速度大蘇打撒",                  Name="薩達薩達",                  Number =6000,                  Text2 = "突然他也讓他人",                  Text3 = "sadsad打發打發士大夫的"              },            });

 

Demo1-3

列頭處理或者多語言支持

 

 

 

[ExcelExporter(Name = "測試", TableStyle = "Light10")]  public class AttrsLocalizationTestData  {      [ExporterHeader(DisplayName = "加粗文本", IsBold = true)]      public string Text { get; set; }        [ExporterHeader(DisplayName = "普通文本")]      public string Text2 { get; set; }        [ExporterHeader(DisplayName = "忽略", IsIgnore = true)]      public string Text3 { get; set; }        [ExporterHeader(DisplayName = "數值", Format = "#,##0")]      public double Number { get; set; }        [ExporterHeader(DisplayName = "名稱", IsAutoFit = true)]      public string Name { get; set; }  }          ExcelBuilder.Create().WithLocalStringFunc((key) =>          {              if (key.Contains("文本"))              {                  return "Text";              }              return "未知語言";          }).Build();            var filePath = Path.Combine(Directory.GetCurrentDirectory(), "testAttrsLocalization.xlsx");          if (File.Exists(filePath)) File.Delete(filePath);            var result = await Exporter.Export(filePath, new List<AttrsLocalizationTestData>()          {              new AttrsLocalizationTestData()              {                  Text = "啊實打實大蘇打撒",                  Name="aa",                  Number =5000,                  Text2 = "w薩達薩達薩達撒",                  Text3 = "sadsad打發打發士大夫的"              },             new AttrsLocalizationTestData()              {                  Text = "啊實打實大蘇打撒",                  Name="啊實打實大蘇打撒",                  Number =6000,                  Text2 = "w薩達薩達薩達撒",                  Text3 = "sadsad打發打發士大夫的"              },             new AttrsLocalizationTestData()              {                  Text = "啊實打實速度大蘇打撒",                  Name="薩達薩達",                  Number =6000,                  Text2 = "突然他也讓他人",                  Text3 = "sadsad打發打發士大夫的"              },            });

 

導入 Demo

導入特性(ImporterHeader):

  • Namestring 表頭顯示名稱(不可為空)。

  • Descriptionstring 表頭添加註釋。

  • Authorstring 注釋作者,默認值為X.M。

導入結果(ImportModel<T>):

  • DataIList<T>  導入的數據集合。

  • ValidationResultsIList<ValidationResultModel> 數據驗證結果。

  • HasValidTemplatebool 模板驗證是否通過。

數據驗證結果(ValidationResultModel):

  • Indexint  錯誤數據所在行。

  • ErrorsIDictionary<string, string> 整個Excel錯誤集合。目前僅支持數據驗證錯誤。

  • FieldErrorsIDictionary<string, string> 數據驗證錯誤。

 

Demo2-1 普通模板

生成模板

public class ImportProductDto  {      /// <summary>      /// 產品名稱      /// </summary>      [ImporterHeader(Name = "產品名稱")]      public string Name { get; set; }      /// <summary>      /// 產品代碼      /// </summary>      [ImporterHeader(Name = "產品代碼")]      public string Code { get; set; }      /// <summary>      /// 產品條碼      /// </summary>      [ImporterHeader(Name = "產品條碼")]      public string BarCode { get; set; }    }

 

導入模板

 

 

 

 

Demo2-2 多數據類型

生成模板

 

 

public class ImportProductDto  {      /// <summary>      /// 產品名稱      /// </summary>      [ImporterHeader(Name = "產品名稱")]      public string Name { get; set; }      /// <summary>      /// 產品代碼      /// </summary>      [ImporterHeader(Name = "產品代碼")]      public string Code { get; set; }      /// <summary>      /// 產品條碼      /// </summary>      [ImporterHeader(Name = "產品條碼")]      public string BarCode { get; set; }      /// <summary>      /// 客戶Id      /// </summary>      [ImporterHeader(Name = "客戶代碼")]      public long ClientId { get; set; }      /// <summary>      /// 產品型號      /// </summary>      [ImporterHeader(Name = "產品型號")]      public string Model { get; set; }      /// <summary>      /// 申報價值      /// </summary>      [ImporterHeader(Name = "申報價值")]      public double DeclareValue { get; set; }      /// <summary>      /// 貨幣單位      /// </summary>      [ImporterHeader(Name = "貨幣單位")]      public string CurrencyUnit { get; set; }      /// <summary>      /// 品牌名稱      /// </summary>      [ImporterHeader(Name = "品牌名稱")]      public string BrandName { get; set; }      /// <summary>      /// 尺寸      /// </summary>      [ImporterHeader(Name = "尺寸(長x寬x高)")]      public string Size { get; set; }      /// <summary>      /// 重量      /// </summary>      [ImporterHeader(Name = "重量(KG)")]      public double Weight { get; set; }        /// <summary>      /// 類型      /// </summary>      [ImporterHeader(Name = "類型")]      public ImporterProductType Type { get; set; }        /// <summary>      /// 是否行      /// </summary>      [ImporterHeader(Name = "是否行")]      public bool IsOk { get; set; }  }

 

public enum ImporterProductType  {      [Display(Name = "第一")]      One,      [Display(Name = "第二")]      Two    }


導入模板

 

 

 

 

Demo2-3 數據驗證

生成模板

必填項表頭文本為紅色

public class ImportProductDto  {      /// <summary>      /// 產品名稱      /// </summary>      [ImporterHeader(Name = "產品名稱",Description ="必填")]      [Required(ErrorMessage = "產品名稱是必填的")]      public string Name { get; set; }      /// <summary>      /// 產品代碼      /// </summary>      [ImporterHeader(Name = "產品代碼", Description = "最大長度為8")]      [MaxLength(8,ErrorMessage = "產品代碼最大長度為8")]      public string Code { get; set; }      /// <summary>      /// 產品條碼      /// </summary>      [ImporterHeader(Name = "產品條碼")]      [MaxLength(10, ErrorMessage = "產品條碼最大長度為10")]      [RegularExpression(@"^d*$", ErrorMessage = "產品條碼只能是數字")]      public string BarCode { get; set; }      /// <summary>      /// 客戶Id      /// </summary>      [ImporterHeader(Name = "客戶代碼")]      public long ClientId { get; set; }      /// <summary>      /// 產品型號      /// </summary>      [ImporterHeader(Name = "產品型號")]      public string Model { get; set; }      /// <summary>      /// 申報價值      /// </summary>      [ImporterHeader(Name = "申報價值")]      public double DeclareValue { get; set; }      /// <summary>      /// 貨幣單位      /// </summary>      [ImporterHeader(Name = "貨幣單位")]      public string CurrencyUnit { get; set; }      /// <summary>      /// 品牌名稱      /// </summary>      [ImporterHeader(Name = "品牌名稱")]      public string BrandName { get; set; }      /// <summary>      /// 尺寸      /// </summary>      [ImporterHeader(Name = "尺寸(長x寬x高)")]      public string Size { get; set; }      /// <summary>      /// 重量      /// </summary>      [ImporterHeader(Name = "重量(KG)")]      public double Weight { get; set; }        /// <summary>      /// 類型      /// </summary>      [ImporterHeader(Name = "類型")]      public ImporterProductType Type { get; set; }        /// <summary>      /// 是否行      /// </summary>      [ImporterHeader(Name = "是否行")]      public bool IsOk { get; set; }  }  public enum ImporterProductType  {      [Display(Name = "第一")]      One,      [Display(Name = "第二")]      Two    }

 

導入模板

 

 

Docker中使用

# 安裝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"]