Install-Package Magicodes.IE.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); }
public async Task ExportHeader() { IExporter exporter = new ExcelExporter(); var filePath = "h.xlsx"; var result = await exporter.ExportHeaderAsByteArray<Student>( new Student()); result.ToExcelExportFileInfo(filePath); }
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
/// <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