.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.  
    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM