Magicodes.IE Csv導入導出 說明 本章主要說明如何使用Magicodes.IE.Csv進行Csv導入導出. 主要步驟 1.安裝包Magicodes.IE.Csv 2.使用Magicodes.IE.Csv導出Csv 通過如下代碼片段我們將導出的內容通過相應的特性做出相應的處理. Expo ...
Magicodes.IE Csv導入導出
說明
本章主要說明如何使用Magicodes.IE.Csv進行Csv導入導出.
主要步驟
1.安裝包Magicodes.IE.Csv
Install-Package Magicodes.IE.Csv
2.使用Magicodes.IE.Csv導出Csv
通過如下代碼片段我們將導出的內容通過相應的特性做出相應的處理.
ExporterHeaderAttribute
- DisplayName: 顯示名稱
- Format: 格式化
- IsIgnore: 是否忽略
public class ExportTestDataWithAttrs
{
[ExporterHeader(DisplayName = "文本")]
public string Text { get; set; }
[ExporterHeader(DisplayName = "普通文本")] public string Text2 { get; set; }
[ExporterHeader(DisplayName = "忽略", IsIgnore = true)]
public string Text3 { get; set; }
[ExporterHeader(DisplayName = "數值", Format = "#,##0")]
public decimal Number { get; set; }
[ExporterHeader(DisplayName = "名稱", IsAutoFit = true)]
public string Name { get; set; }
/// <summary>
/// 時間測試
/// </summary>
[ExporterHeader(DisplayName = "日期1", Format = "yyyy-MM-dd")]
public DateTime Time1 { get; set; }
/// <summary>
/// 時間測試
/// </summary>
[ExporterHeader(DisplayName = "日期2", Format = "yyyy-MM-dd HH:mm:ss")]
public DateTime? Time2 { get; set; }
public DateTime Time3 { get; set; }
public DateTime Time4 { get; set; }
/// <summary>
/// 長數值測試
/// </summary>
[ExporterHeader(DisplayName = "長數值", Format = "#,##0")]
public long LongNo { get; set; }
}
通過DTO導出
public async Task ExportHeaderAsByteArray_Test()
{
IExporter exporter = new CsvExporter();
var filePath = GetTestFilePath($"{nameof(ExportHeaderAsByteArray_Test)}.csv");
DeleteFile(filePath);
var result = await exporter.ExportHeaderAsByteArray(GenFu.GenFu.New<ExportTestDataWithAttrs>());
}
3.使用Magicodes.IE.Csv導入Csv
對於csv導入我們可以通過,ImporterHeader Name屬性去對應我們的Dto屬性.並且可以通過ValueMapping對枚舉類型進行相關的映射,並向我們返回相對應的值
public async Task StudentInfoImporter_Test()
{
var filePath = Path.Combine(Directory.GetCurrentDirectory(), "TestFiles", "Import", "學生基礎數據導入.csv");
var import = await Importer.Import<ImportStudentDto>(filePath);
}
/// <summary>
/// 導入學生數據Dto
/// </summary>
public class ImportStudentDto
{
/// <summary>
/// 序號
/// </summary>
[ImporterHeader(Name = "序號")]
public long SerialNumber { get; set; }
/// <summary>
/// 學籍號
/// </summary>
[ImporterHeader(Name = "學籍號")]
public string StudentCode { get; set; }
/// <summary>
/// 姓名
/// </summary>
[ImporterHeader(Name = "姓名")]
public string Name { get; set; }
/// <summary>
/// 身份證號碼
/// </summary>
[ImporterHeader(Name = "身份證號")]
public string IdCard { get; set; }
/// <summary>
/// 性別
/// </summary>
[ImporterHeader(Name = "性別")]
[ValueMapping("男", 0)]
[ValueMapping("女", 1)]
public Genders Gender { get; set; }
/// <summary>
/// 家庭地址
/// </summary>
[ImporterHeader(Name = "家庭住址")]
public string Address { get; set; }
/// <summary>
/// 家長姓名
/// </summary>
[ImporterHeader(Name = "家長姓名")]
public string Guardian { get; set; }
/// <summary>
/// 家長聯繫電話
/// </summary>
[ImporterHeader(Name = "家長聯繫電話")]
public string GuardianPhone { get; set; }
/// <summary>
/// 學號
/// </summary>
[ImporterHeader(Name = "學號")]
public string StudentNub { get; set; }
/// <summary>
/// 宿舍號
/// </summary>
[ImporterHeader(Name = "宿舍號")]
public string DormitoryNo { get; set; }
/// <summary>
/// QQ
/// </summary>
[ImporterHeader(Name = "QQ號")]
public string QQ { get; set; }
/// <summary>
/// 民族
/// </summary>
[ImporterHeader(Name = "民族")]
public string Nation { get; set; }
/// <summary>
/// 戶口性質
/// </summary>
[ImporterHeader(Name = "戶口性質")]
public string HouseholdType { get; set; }
/// <summary>
/// 聯繫電話
/// </summary>
[ImporterHeader(Name = "學生聯繫電話")]
public string Phone { get; set; }
/// <summary>
/// 狀態
/// 測試可為空的枚舉類型
/// </summary>
[ImporterHeader(Name = "狀態")]
public StudentStatus? Status { get; set; }
/// <summary>
/// 備註
/// </summary>
[ImporterHeader(Name = "備註")]
public string Remark { get; set; }
/// <summary>
/// 是否住校(宿舍)
/// </summary>
[ImporterHeader(IsIgnore = true)]
public bool? IsBoarding { get; set; }
/// <summary>
/// 所屬班級id
/// </summary>
[ImporterHeader(IsIgnore = true)]
public Guid ClassId { get; set; }
/// <summary>
/// 學校Id
/// </summary>
[ImporterHeader(IsIgnore = true)]
public Guid? SchoolId { get; set; }
/// <summary>
/// 校區Id
/// </summary>
[ImporterHeader(IsIgnore = true)]
public Guid? CampusId { get; set; }
/// <summary>
/// 專業Id
/// </summary>
[ImporterHeader(IsIgnore = true)]
public Guid? MajorsId { get; set; }
/// <summary>
/// 年級Id
/// </summary>
[ImporterHeader(IsIgnore = true)]
public Guid? GradeId { get; set; }
}
Reference
https://github.com/dotnetcore/Magicodes.IE