開源導入導出庫Magicodes.IE 導出教程

要點

  • 導出特性
  • 如何導出Excel表頭
  • 如何導出數據、如何進行數據的切割、如何使用篩選器

導出特性

ExporterAttribute

  • Name: 名稱(當前Sheet 名稱)
  • HeaderFontSize:頭部字體大小
  • FontSize:正文字體大小
  • MaxRowNumberOnASheet:Sheet最大容許的行數,設置了以後將輸出多個Sheet
  • TableStyle:表格樣式風格
  • AutoFitAllColumnbool 自適應全部列
  • Author:做者
  • ExporterHeaderFilter:頭部篩選器

ExporterHeaderAttribute

  • DisplayName: 顯示名稱
  • FontSize: 字體大小
  • IsBold: 是否加粗
  • Format: 格式化
  • IsAutoFit: 是否自適應
  • IsIgnore: 是否忽略

主要步驟

1.安裝包Magicodes.IE.Excel

Install-Package Magicodes.IE.Excel

2.導出Excel表頭

  • 經過數組導出
public async Task ExportHeader()
        {
            IExporter exporter = new ExcelExporter();

            var filePath = "h.xlsx";
            var arr = new[] { "Name1", "Name2", "Name3", "Name4", "Name5", "Name6" };
            var sheetName = "Test";
            var result = await exporter.ExportHeaderAsByteArray(arr, sheetName);
            result.ToExcelExportFileInfo(filePath);

        }
  • 經過DTO導出
public async Task ExportHeader()
        {
            IExporter exporter = new ExcelExporter();

            var filePath = "h.xlsx";
            var result = await exporter.ExportHeaderAsByteArray<Student>( new Student());
            result.ToExcelExportFileInfo(filePath);

        }

3.導出Excel

  • 基礎導出
public class Student
        {
            /// <summary>
            ///     姓名
            /// </summary>
            public string Name { get; set; }
            /// <summary>
            ///     年齡
            /// </summary>
            public int Age { get; set; }
        }

        public async Task Export()
        {
            IExporter exporter = new ExcelExporter();
            var result = await exporter.Export("a.xlsx", new List<Student>()
                {
                    new Student
                    {
                        Name = "MR.A",
                        Age = 18
                    },
                    new Student
                    {
                        Name = "MR.B",
                        Age = 19
                    },
                    new Student
                    {
                        Name = "MR.B",
                        Age = 20
                    }
                });
        }

經過如上代碼咱們能夠將Excel導出,以下圖所示git

  • 特性導出示例
public async Task Export()
        {
            IExporter exporter = new ExcelExporter();
            var result = await exporter.Export("test.xlsx", new List<Student>()
                {
                    new Student
                    {
                        Name = "MR.A",
                        Age = 18,
                        Remarks = "我叫MR.A,今年18歲",
                        Birthday=DateTime.Now
                    },
                    new Student
                    {
                        Name = "MR.B",
                        Age = 19,
                        Remarks = "我叫MR.B,今年19歲",
                        Birthday=DateTime.Now
                    },
                    new Student
                    {
                        Name = "MR.C",
                        Age = 20,
                        Remarks = "我叫MR.C,今年20歲",
                        Birthday=DateTime.Now
                    }
                });
        }
        /// <summary>
        ///     學生信息
        /// </summary>
        [ExcelExporter(Name = "學生信息", TableStyle = "Light10", AutoFitAllColumn = true, MaxRowNumberOnASheet = 2))]
        public class Student
        {
            /// <summary>
            ///     姓名
            /// </summary>
            [ExporterHeader(DisplayName = "姓名")]
            public string Name { get; set; }
            /// <summary>
            ///     年齡
            /// </summary>
            [ExporterHeader(DisplayName = "年齡")]
            public int Age { get; set; }
            /// <summary>
            ///     備註
            /// </summary>
            public string Remarks { get; set; }
            /// <summary>
            ///     出生日期
            /// </summary>
            [ExporterHeader(DisplayName = "出生日期", Format = "yyyy-mm-DD")]
            public DateTime Birthday { get; set; }
        }

經過如上代碼咱們能夠將Excel導出,以下圖所示github

  1. ExcelExporter特性能夠設置導出的全局設置,好比表格樣式,Sheet名稱,自適應列等等具體參照 導出特性
  2. ExporterHeader特性咱們能夠對錶頭名稱、樣式等等進行設置 具體參照 導出特性
  3. ExcelExporter MaxRowNumberOnASheet 屬性對數據進行拆分,經過該屬性指定Sheet數據長度從而實現自動切割
  • 表頭篩選器
/// <summary>
        ///     學生信息
        /// </summary>
        [ExcelExporter(Name = "學生信息", TableStyle = "Light10", AutoFitAllColumn = true, MaxRowNumberOnASheet = 2,ExporterHeaderFilter = typeof(ExporterStudentHeaderFilter))]
        public class Student
        {
            /// <summary>
            ///     姓名
            /// </summary>
            [ExporterHeader(DisplayName = "姓名")]
            public string Name { get; set; }
            /// <summary>
            ///     年齡
            /// </summary>
            [ExporterHeader(DisplayName = "年齡")]
            public int Age { get; set; }
            /// <summary>
            ///     備註
            /// </summary>
            public string Remarks { get; set; }
        }

        
        public class ExporterStudentHeaderFilter : IExporterHeaderFilter
        {
            /// <summary>
            /// 表頭篩選器(修更名稱)
            /// </summary>
            /// <param name="exporterHeaderInfo"></param>
            /// <returns></returns>
            public ExporterHeaderInfo Filter(ExporterHeaderInfo exporterHeaderInfo)
            {
                if (exporterHeaderInfo.DisplayName.Equals("姓名"))
                {
                    exporterHeaderInfo.DisplayName = "name";
                }
                return exporterHeaderInfo;
            }
        }

經過如上代碼片斷咱們實現 IExporterHeaderFilter 接口,IExporterHeaderFilter以便支持多語言、動態控制列展現等場景shell

Reference

https://github.com/dotnetcore/Magicodes.IE數組

相關文章
相關標籤/搜索