1. 程式人生 > >開源匯入匯出庫Magicodes.IE 匯出教程

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

要點

  • 匯出特性
  • 如何匯出Excel表頭
  • 如何匯出資料、如何進行資料的切割、如何使用篩選器

匯出特性

ExporterAttribute

  • Name: 名稱(當前Sheet 名稱)
  • HeaderFontSize:頭部字型大小
  • FontSize:正文字型大小
  • MaxRowNumberOnASheet:Sheet最大允許的行數,設定了之後將輸出多個Sheet
  • TableStyle:表格樣式風格
  • AutoFitAllColumn:bool 自適應所有列
  • 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匯出,如下圖所示

  • 特性匯出示例

        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匯出,如下圖所示

  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以便支援多語言、動態控制列展示等場景

Reference

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