要點 導出特性 如何導出Excel表頭 如何導出數據、如何進行數據的切割、如何使用篩選器 導出特性 ExporterAttribute + Name : 名稱(當前Sheet 名稱) + HeaderFontSize :頭部字體大小 + FontSize :正文字體大小 + MaxRowNumber ...
要點
- 導出特性
- 如何導出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導出,如下圖所示
- ExcelExporter特性可以設置導出的全局設置,比如表格樣式,Sheet名稱,自適應列等等具體參照 導出特性
- ExporterHeader特性我們可以對錶頭名稱、樣式等等進行設置 具體參照 導出特性
- 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