.net framework导出Excel、Word、Pdf和Html:Magicodes.IE的简单使用

 

这里介绍一个很方便实用的库:Magicodes.IE,导入导出通用库,通过导入导出DTO模型来控制导入和导出,支持Excel、Word、Pdf和Html。

本篇介绍基本使用方法,使用方法非常简单,不需要学习npoi的应用。

在vs中,新建一个控制台项目(测试用,可以建其他项目,比如asp.net mvc ,winform都可以)

右键单击项目中的引用,选择nuget包管理,导入nuget包:这里只使用excel,所以只导入了Magicodes.IE.Excel

  1.  
    Magicodes.IE.Core
  2.  
    Magicodes.IE.Excel
  3.  
    Magicodes.IE.Pdf
  4.  
    Magicodes.IE.Word
  5.  
    Magicodes.IE.Html


1. 首先准备好一个类,这个类包含需要导出的属性一、导出数据

  1.  
    public class Question
  2.  
    {
  3.  
    public string Title { get; set; }
  4.  
    public string Content { get; set; }
  5.  
    public string Options { get; set; }
  6.  
    public string Answer { get; set; }
  7.  
    }

2.导出方法

这里如果new List<Question>()不设置数据,就可以直接导出Question的模板

  1.  
    static void ExportQuestion()
  2.  
    {
  3.  
    //这里需要补充,如果路径不存在要创建路径,否则会报错
  4.  
    var filePath = Path.Combine(Directory.GetCurrentDirectory(), "TestFiles", "question.xlsx");
  5.  
    var exporter = new ExcelExporter();
  6.  
    var result = exporter.Export(filePath, new List<Question>() {
  7.  
    new Question()
  8.  
    {
  9.  
    Title = "Question1",
  10.  
    Content = "Question content 1",
  11.  
    Options = "A:option1,B:option2,C:option3,D:option4",
  12.  
    Answer = "A",
  13.  
    },
  14.  
    new Question()
  15.  
    {
  16.  
    Title = "Question2",
  17.  
    Content = "Question content 2",
  18.  
    Options = "A:option11,B:option22,C:option33,D:option43",
  19.  
    Answer = "B",
  20.  
    }
  21.  
    });
  22.  
    }

 

其他方法参考下代码吧:

  1.  
    using Magicodes.ExporterAndImporter.Core;
  2.  
    using Magicodes.ExporterAndImporter.Excel;
  3.  
    using Magicodes.ExporterAndImporter.Excel.Builder;
  4.  
    using System;
  5.  
    using System.Collections.Generic;
  6.  
    using System.ComponentModel.DataAnnotations;
  7.  
    using System.IO;
  8.  
    using System.Text;
  9.  
    using System.Threading.Tasks;
  10.  
     
  11.  
    namespace Test
  12.  
    {
  13.  
    class Program
  14.  
    {
  15.  
    //static void Main(string[] args)
  16.  
    static async Task Main(string[] args)
  17.  
    {
  18.  
    await Import3();
  19.  
    Console.WriteLine("Hello World!");
  20.  
    }
  21.  
     
  22.  
    #region 导出excel demo
  23.  
     
  24.  
    /// <summary>
  25.  
    /// 导出excel测试:excel1
  26.  
    /// </summary>
  27.  
    static void Demo1()
  28.  
    {
  29.  
    //这里需要补充,如果路径不存在要创建路径,否则会报错
  30.  
    var filePath = Path.Combine(Directory.GetCurrentDirectory(), "TestFiles", "demo1.xlsx");
  31.  
    var exporter = new ExcelExporter();
  32.  
    var result = exporter.Export(filePath, new List<ExportTestData>() {
  33.  
    new ExportTestData()
  34.  
    {
  35.  
    Name1 = "1",
  36.  
    Name2 = "test",
  37.  
    Name3 = "12",
  38.  
    Name4 = "11",
  39.  
    },
  40.  
    new ExportTestData()
  41.  
    {
  42.  
    Name1 = "1",
  43.  
    Name2 = "test",
  44.  
    Name3 = "12",
  45.  
    Name4 = "11",
  46.  
    }
  47.  
    });
  48.  
    }
  49.  
     
  50.  
    static void ExportQuestion()
  51.  
    {
  52.  
    //这里需要补充,如果路径不存在要创建路径,否则会报错
  53.  
    var filePath = Path.Combine(Directory.GetCurrentDirectory(), "TestFiles", "question.xlsx");
  54.  
    var exporter = new ExcelExporter();
  55.  
    var result = exporter.Export(filePath, new List<Question>() {
  56.  
    new Question()
  57.  
    {
  58.  
    Title = "Question1",
  59.  
    Content = "Question content 1",
  60.  
    Options = "A:option1,B:option2,C:option3,D:option4",
  61.  
    Answer = "A",
  62.  
    },
  63.  
    new Question()
  64.  
    {
  65.  
    Title = "Question2",
  66.  
    Content = "Question content 2",
  67.  
    Options = "A:option11,B:option22,C:option33,D:option43",
  68.  
    Answer = "B",
  69.  
    }
  70.  
    });
  71.  
    }
  72.  
     
  73.  
    /// <summary>
  74.  
    /// 导出空模板测试:excel11
  75.  
    /// </summary>
  76.  
    static void Demo11()
  77.  
    {
  78.  
    //这里需要补充,如果路径不存在要创建路径,否则会报错
  79.  
    var filePath = Path.Combine(Directory.GetCurrentDirectory(), "TestFiles", "ImportProduct2Dto.xlsx");
  80.  
    var exporter = new ExcelExporter();
  81.  
    var result = exporter.Export(filePath, new List<ImportProduct2Dto>());
  82.  
    }
  83.  
     
  84.  
     
  85.  
    /// <summary>
  86.  
    /// 利用特性导出excel2
  87.  
    /// </summary>
  88.  
    static void Demo2()
  89.  
    {
  90.  
    var filePath = Path.Combine(Directory.GetCurrentDirectory(), "TestFiles", "demo2.xlsx");
  91.  
    ExcelExporter exporter = new ExcelExporter();
  92.  
    var result = exporter.Export(filePath, new List<ExportTestDataWithAttrs>()
  93.  
    {
  94.  
    new ExportTestDataWithAttrs()
  95.  
    {
  96.  
    Text1 = "啊实打实大苏打撒",
  97.  
    Name="aa",
  98.  
    Number =5000,
  99.  
    Text2 = "w萨达萨达萨达撒",
  100.  
    Text3 = "sadsad打发打发士大夫的"
  101.  
    },
  102.  
    new ExportTestDataWithAttrs()
  103.  
    {
  104.  
    Text1 = "啊实打实大苏打撒",
  105.  
    Name="啊实打实大苏打撒",
  106.  
    Number =6000,
  107.  
    Text2 = "w萨达萨达萨达撒",
  108.  
    Text3 = "sadsad打发打发士大夫的"
  109.  
    },
  110.  
    new ExportTestDataWithAttrs()
  111.  
    {
  112.  
    Text1 = "啊实打实速度大苏打撒",
  113.  
    Name="萨达萨达",
  114.  
    Number =6000,
  115.  
    Text2 = "突然他也让他人",
  116.  
    Text3 = "sadsad打发打发士大夫的"
  117.  
    },
  118.  
    });
  119.  
    }
  120.  
     
  121.  
    /// <summary>
  122.  
    /// 列头处理或者多语言支持测试
  123.  
    /// </summary>
  124.  
    static void Demo3()
  125.  
    {
  126.  
    var filePath = Path.Combine(Directory.GetCurrentDirectory(), "TestFiles","testAttrsLocalization.xlsx");
  127.  
    if (File.Exists(filePath)) File.Delete(filePath);
  128.  
    var exporter = new ExcelExporter();
  129.  
    ExcelBuilder.Create().WithColumnHeaderStringFunc((key) =>
  130.  
    {
  131.  
    if (key.Contains("文本"))
  132.  
    {
  133.  
    return "Text";
  134.  
    }
  135.  
    return "未知语言";
  136.  
    }).Build();
  137.  
    var result = exporter.Export(filePath, new List<AttrsLocalizationTestData>()
  138.  
    {
  139.  
    new AttrsLocalizationTestData()
  140.  
    {
  141.  
    Text = "啊实打实大苏打撒",
  142.  
    Name="aa",
  143.  
    Number =5000,
  144.  
    Text2 = "w萨达萨达萨达撒",
  145.  
    Text3 = "sadsad打发打发士大夫的"
  146.  
    },
  147.  
    new AttrsLocalizationTestData()
  148.  
    {
  149.  
    Text = "啊实打实大苏打撒",
  150.  
    Name="啊实打实大苏打撒",
  151.  
    Number =6000,
  152.  
    Text2 = "w萨达萨达萨达撒",
  153.  
    Text3 = "sadsad打发打发士大夫的"
  154.  
    },
  155.  
    new AttrsLocalizationTestData()
  156.  
    {
  157.  
    Text = "啊实打实速度大苏打撒",
  158.  
    Name="萨达萨达",
  159.  
    Number =6000,
  160.  
    Text2 = "突然他也让他人",
  161.  
    Text3 = "sadsad打发打发士大夫的"
  162.  
    },
  163.  
    });
  164.  
    }
  165.  
     
  166.  
    #endregion
  167.  
     
  168.  
    /// <summary>
  169.  
    /// 从excel导入数据测试1
  170.  
    /// </summary>
  171.  
    /// <returns></returns>
  172.  
    private static async Task Import1()
  173.  
    {
  174.  
    var importer = new ExcelImporter();
  175.  
    var importResult = await importer.Import<ImportProductDto>(Path.Combine(Directory.GetCurrentDirectory(), "TestFiles", "importer1.xlsx"));
  176.  
    foreach (var item in importResult.Data)
  177.  
    {
  178.  
    Console.WriteLine(item.Name+"-"+item.Code+"-"+item.BarCode);
  179.  
    }
  180.  
    }
  181.  
     
  182.  
    /// <summary>
  183.  
    /// 导入带有枚举值的数据
  184.  
    /// </summary>
  185.  
    /// <returns></returns>
  186.  
    private static async Task Import2()
  187.  
    {
  188.  
    var importer = new ExcelImporter();
  189.  
    var importResult = await importer.Import<ImportProduct2Dto>(Path.Combine(Directory.GetCurrentDirectory(), "TestFiles", "ImportProduct2Dto.xlsx"));
  190.  
    foreach (var item in importResult.Data)
  191.  
    {
  192.  
    Console.WriteLine(item.Name + "-" + item.Code + "-" + item.BarCode+"-"+item.Type.ToString());
  193.  
    }
  194.  
    }
  195.  
     
  196.  
    /// <summary>
  197.  
    /// 导入数据验证
  198.  
    /// </summary>
  199.  
    /// <returns></returns>
  200.  
    private static async Task Import3()
  201.  
    {
  202.  
    var importer = new ExcelImporter();
  203.  
    var importResult = await importer.Import<ImportProduct3Dto>(Path.Combine(Directory.GetCurrentDirectory(), "TestFiles", "ImportProduct2Dto.xlsx"));
  204.  
    if (importResult.HasError)
  205.  
    {
  206.  
    StringBuilder stringBuilder = new StringBuilder();
  207.  
    foreach (var item in importResult.RowErrors)
  208.  
    {
  209.  
    stringBuilder.AppendLine("出错行数:" + item.RowIndex);
  210.  
    foreach (var fielderror in item.FieldErrors)
  211.  
    {
  212.  
    stringBuilder.AppendLine("\t出错列:" + fielderror.Key+"\n\t出错原因:"+fielderror.Value);
  213.  
    }
  214.  
    }
  215.  
    Console.WriteLine(stringBuilder.ToString());
  216.  
    }
  217.  
    else
  218.  
    {
  219.  
    foreach (var item in importResult.Data)
  220.  
    {
  221.  
    Console.WriteLine(item.Name + "-" + item.Code + "-" + item.BarCode + "-" + item.Type.ToString());
  222.  
    }
  223.  
    }
  224.  
     
  225.  
    }
  226.  
     
  227.  
     
  228.  
    }
  229.  
     
  230.  
    #region 导出excel demo class
  231.  
    public class ExportTestData
  232.  
    {
  233.  
    public string Name1 { get; set; }
  234.  
    public string Name2 { get; set; }
  235.  
    public string Name3 { get; set; }
  236.  
    public string Name4 { get; set; }
  237.  
    }
  238.  
     
  239.  
    public class Question
  240.  
    {
  241.  
    public string Title { get; set; }
  242.  
    public string Content { get; set; }
  243.  
    public string Options { get; set; }
  244.  
    public string Answer { get; set; }
  245.  
    }
  246.  
     
  247.  
    /// <summary>
  248.  
    /// 特性导出Excel
  249.  
    /// </summary>
  250.  
    [ExcelExporter(Name ="测试",TableStyle ="Light10")]
  251.  
    public class ExportTestDataWithAttrs
  252.  
    {
  253.  
    [ExporterHeader(DisplayName="加粗文本",IsBold=true)]
  254.  
    public string Text1 { get; set; }
  255.  
     
  256.  
    [ExporterHeader(DisplayName ="普通文本")]
  257.  
    public string Text2 { get; set; }
  258.  
     
  259.  
    [ExporterHeader(DisplayName = "忽略", IsIgnore = true)]
  260.  
    public string Text3 { get; set; }
  261.  
     
  262.  
    [ExporterHeader(DisplayName = "数值", Format = "#,##0")]
  263.  
    public double Number { get; set; }
  264.  
     
  265.  
    [ExporterHeader(DisplayName = "名称", IsAutoFit = true)]
  266.  
    public string Name { get; set; }
  267.  
     
  268.  
    }
  269.  
     
  270.  
    /// <summary>
  271.  
    /// 列头处理或者多语言支持
  272.  
    /// </summary>
  273.  
    [ExcelExporter(Name = "测试", TableStyle = "Light10")]
  274.  
    public class AttrsLocalizationTestData
  275.  
    {
  276.  
    [ExporterHeader(DisplayName = "加粗文本", IsBold = true)]
  277.  
    public string Text { get; set; }
  278.  
     
  279.  
    [ExporterHeader(DisplayName = "普通文本")]
  280.  
    public string Text2 { get; set; }
  281.  
     
  282.  
    [ExporterHeader(DisplayName = "忽略", IsIgnore = true)]
  283.  
    public string Text3 { get; set; }
  284.  
     
  285.  
    [ExporterHeader(DisplayName = "数值", Format = "#,##0")]
  286.  
    public double Number { get; set; }
  287.  
     
  288.  
    [ExporterHeader(DisplayName = "名称", IsAutoFit = true)]
  289.  
    public string Name { get; set; }
  290.  
    }
  291.  
     
  292.  
    #endregion
  293.  
     
  294.  
    /// <summary>
  295.  
    /// 从excel导入数据 dto
  296.  
    /// </summary>
  297.  
    public class ImportProductDto
  298.  
    {
  299.  
    /// <summary>
  300.  
    /// 产品名称
  301.  
    /// </summary>
  302.  
    [ImporterHeader(Name = "产品名称")]
  303.  
    public string Name { get; set; }
  304.  
    /// <summary>
  305.  
    /// 产品代码
  306.  
    /// </summary>
  307.  
    [ImporterHeader(Name = "产品代码")]
  308.  
    public string Code { get; set; }
  309.  
    /// <summary>
  310.  
    /// 产品条码
  311.  
    /// </summary>
  312.  
    [ImporterHeader(Name = "产品条码")]
  313.  
    public string BarCode { get; set; }
  314.  
    }
  315.  
     
  316.  
    [ExcelExporter(Name = "测试1", TableStyle = "Light10")]
  317.  
    public class ImportProduct2Dto
  318.  
    {
  319.  
    /// <summary>
  320.  
    /// 产品名称
  321.  
    /// </summary>
  322.  
    [ImporterHeader(Name = "产品名称")]
  323.  
    [ExporterHeader(DisplayName = "产品名称")]
  324.  
    public string Name { get; set; }
  325.  
    /// <summary>
  326.  
    /// 产品代码
  327.  
    /// </summary>
  328.  
    [ImporterHeader(Name = "产品代码")]
  329.  
    [ExporterHeader(DisplayName = "产品代码")]
  330.  
    public string Code { get; set; }
  331.  
    /// <summary>
  332.  
    /// 产品条码
  333.  
    /// </summary>
  334.  
    [ImporterHeader(Name = "产品条码")]
  335.  
    [ExporterHeader(DisplayName = "产品条码")]
  336.  
    public string BarCode { get; set; }
  337.  
    /// <summary>
  338.  
    /// 客户Id
  339.  
    /// </summary>
  340.  
    [ImporterHeader(Name = "客户代码")]
  341.  
    [ExporterHeader(DisplayName = "客户代码")]
  342.  
    public long ClientId { get; set; }
  343.  
    /// <summary>
  344.  
    /// 产品型号
  345.  
    /// </summary>
  346.  
    [ImporterHeader(Name = "产品型号")]
  347.  
    [ExporterHeader(DisplayName = "产品型号")]
  348.  
    public string Model { get; set; }
  349.  
    /// <summary>
  350.  
    /// 申报价值
  351.  
    /// </summary>
  352.  
    [ImporterHeader(Name = "申报价值")]
  353.  
    [ExporterHeader(DisplayName = "申报价值")]
  354.  
    public double DeclareValue { get; set; }
  355.  
    /// <summary>
  356.  
    /// 货币单位
  357.  
    /// </summary>
  358.  
    [ImporterHeader(Name = "货币单位")]
  359.  
    [ExporterHeader(DisplayName = "货币单位")]
  360.  
    public string CurrencyUnit { get; set; }
  361.  
    /// <summary>
  362.  
    /// 品牌名称
  363.  
    /// </summary>
  364.  
    [ImporterHeader(Name = "品牌名称")]
  365.  
    [ExporterHeader(DisplayName = "品牌名称")]
  366.  
    public string BrandName { get; set; }
  367.  
    /// <summary>
  368.  
    /// 尺寸
  369.  
    /// </summary>
  370.  
    [ImporterHeader(Name = "尺寸(长x宽x高)")]
  371.  
    [ExporterHeader(DisplayName = "尺寸(长x宽x高)")]
  372.  
    public string Size { get; set; }
  373.  
    /// <summary>
  374.  
    /// 重量
  375.  
    /// </summary>
  376.  
    [ImporterHeader(Name = "重量(KG)")]
  377.  
    [ExporterHeader(DisplayName = "重量(KG)")]
  378.  
    public double Weight { get; set; }
  379.  
     
  380.  
    /// <summary>
  381.  
    /// 类型
  382.  
    /// </summary>
  383.  
    [ImporterHeader(Name = "类型")]
  384.  
    [ExporterHeader(DisplayName = "类型")]
  385.  
    public ImporterProductType Type { get; set; }
  386.  
     
  387.  
    /// <summary>
  388.  
    /// 是否行
  389.  
    /// </summary>
  390.  
    [ImporterHeader(Name = "是否行")]
  391.  
    [ExporterHeader(DisplayName = "是否行")]
  392.  
    public bool IsOk { get; set; }
  393.  
    }
  394.  
     
  395.  
     
  396.  
    public class ImportProduct3Dto
  397.  
    {
  398.  
    /// <summary>
  399.  
    /// 产品名称
  400.  
    /// </summary>
  401.  
    [ImporterHeader(Name = "产品名称", Description = "必填")]
  402.  
    [Required(ErrorMessage = "产品名称是必填的")]
  403.  
    public string Name { get; set; }
  404.  
    /// <summary>
  405.  
    /// 产品代码
  406.  
    /// </summary>
  407.  
    [ImporterHeader(Name = "产品代码", Description = "最大长度为8")]
  408.  
    [MaxLength(8, ErrorMessage = "产品代码最大长度为8")]
  409.  
    public string Code { get; set; }
  410.  
    /// <summary>
  411.  
    /// 产品条码
  412.  
    /// </summary>
  413.  
    [ImporterHeader(Name = "产品条码")]
  414.  
    [MaxLength(10, ErrorMessage = "产品条码最大长度为10")]
  415.  
    [RegularExpression(@"^\d*$", ErrorMessage = "产品条码只能是数字")]
  416.  
    public string BarCode { get; set; }
  417.  
    /// <summary>
  418.  
    /// 客户Id
  419.  
    /// </summary>
  420.  
    [ImporterHeader(Name = "客户代码")]
  421.  
    public long ClientId { get; set; }
  422.  
    /// <summary>
  423.  
    /// 产品型号
  424.  
    /// </summary>
  425.  
    [ImporterHeader(Name = "产品型号")]
  426.  
    public string Model { get; set; }
  427.  
    /// <summary>
  428.  
    /// 申报价值
  429.  
    /// </summary>
  430.  
    [ImporterHeader(Name = "申报价值")]
  431.  
    public double DeclareValue { get; set; }
  432.  
    /// <summary>
  433.  
    /// 货币单位
  434.  
    /// </summary>
  435.  
    [ImporterHeader(Name = "货币单位")]
  436.  
    public string CurrencyUnit { get; set; }
  437.  
    /// <summary>
  438.  
    /// 品牌名称
  439.  
    /// </summary>
  440.  
    [ImporterHeader(Name = "品牌名称")]
  441.  
    public string BrandName { get; set; }
  442.  
    /// <summary>
  443.  
    /// 尺寸
  444.  
    /// </summary>
  445.  
    [ImporterHeader(Name = "尺寸(长x宽x高)")]
  446.  
    public string Size { get; set; }
  447.  
    /// <summary>
  448.  
    /// 重量
  449.  
    /// </summary>
  450.  
    [ImporterHeader(Name = "重量(KG)")]
  451.  
    public double Weight { get; set; }
  452.  
     
  453.  
    /// <summary>
  454.  
    /// 类型
  455.  
    /// </summary>
  456.  
    [ImporterHeader(Name = "类型")]
  457.  
    public ImporterProductType Type { get; set; }
  458.  
     
  459.  
    /// <summary>
  460.  
    /// 是否行
  461.  
    /// </summary>
  462.  
    [ImporterHeader(Name = "是否行")]
  463.  
    public bool IsOk { get; set; }
  464.  
    }
  465.  
     
  466.  
    public enum ImporterProductType
  467.  
    {
  468.  
    [Display(Name = "第一")]
  469.  
    One,
  470.  
    [Display(Name = "第二")]
  471.  
    Two
  472.  
    }
  473.  
    }

 

posted @ 2020-01-02 08:16  咖啡无眠  阅读(1026)  评论(0编辑  收藏  举报