Magicodes.ExporterAndImporter

导入导出通用库

特点

  • 封装导入导出业务,目前仅支持Excel,后续将支持CSV以及其他业务
  • 配置特性即可控制相关逻辑和显示结果,无需修改逻辑代码
  • 推荐配合DTO使用
  • 导出支持列头自定义处理以便支持多语言等场景
  • 导出支持文本自定义过滤或处理
  • 导入支持验证逻辑(即将支持)
  • 导入支持自定义过滤或处理(即将支持)
  • 导入支持自动根据DTO生成导入模板(即将支持)

导出Demo

Demo1

普通导出

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",
    }
});

Demo2

特性导出

[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打发打发士大夫的"
            },
        });

Demo3

列头处理或者多语言支持

[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打发打发士大夫的"
            },
        });