ASP.NET 開源匯入匯出庫Magicodes.IE 完成Csv匯入匯出
阿新 • • 發佈:2020-05-14
# Magicodes.IE Csv匯入匯出
## 說明
本章主要說明如何使用Magicodes.IE.Csv進行Csv匯入匯出.
## 主要步驟
### 1.安裝包Magicodes.IE.Csv
```powershell
Install-Package Magicodes.IE.Csv
```
### 2.使用Magicodes.IE.Csv匯出Csv
通過如下程式碼片段我們將匯出的內容通過相應的特性做出相應的處理.
**ExporterHeaderAttribute**
- DisplayName: 顯示名稱
- Format: 格式化
- IsIgnore: 是否忽略
```csharp
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; }
///
/// 時間測試
///
[ExporterHeader(DisplayName = "日期1", Format = "yyyy-MM-dd")]
public DateTime Time1 { get; set; }
///
/// 時間測試
///
[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; }
///
/// 長數值測試
///
[ExporterHeader(DisplayName = "長數值", Format = "#,##0")]
public long LongNo { get; set; }
}
```
通過DTO匯出
```csharp
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());
}
```
### 3.使用Magicodes.IE.Csv匯入Csv
對於csv匯入我們可以通過,**ImporterHeader** Name屬性去對應我們的Dto屬性.並且可以通過**ValueMapping**對列舉型別進行相關的對映,並向我們返回相對應的值
```csharp
public async Task StudentInfoImporter_Test()
{
var filePath = Path.Combine(Directory.GetCurrentDirectory(), "TestFiles", "Import", "學生基礎資料匯入.csv");
var import = await Importer.Import(filePath);
}
```
```csharp
///
/// 匯入學生資料Dto
///
public class ImportStudentDto
{
///
/// 序號
///
[ImporterHeader(Name = "序號")]
public long SerialNumber { get; set; }
///
/// 學籍號
///
[ImporterHeader(Name = "學籍號")]
public string StudentCode { get; set; }
///
/// 姓名
///
[ImporterHeader(Name = "姓名")]
public string Name { get; set; }
///
/// 身份證號碼
///
[ImporterHeader(Name = "身份證號")]
public string IdCard { get; set; }
///
/// 性別
///
[ImporterHeader(Name = "性別")]
[ValueMapping("男", 0)]
[ValueMapping("女", 1)]
public Genders Gender { get; set; }
///
/// 家庭地址
///
[ImporterHeader(Name = "家庭住址")]
public string Address { get; set; }
///
/// 家長姓名
///
[ImporterHeader(Name = "家長姓名")]
public string Guardian { get; set; }
///
/// 家長聯絡電話
///
[ImporterHeader(Name = "家長聯絡電話")]
public string GuardianPhone { get; set; }
///
/// 學號
///
[ImporterHeader(Name = "學號")]
public string StudentNub { get; set; }
///
/// 宿舍號
///
[ImporterHeader(Name = "宿舍號")]
public string DormitoryNo { get; set; }
///
/// QQ
///
[ImporterHeader(Name = "QQ號")]
public string QQ { get; set; }
///
/// 民族
///
[ImporterHeader(Name = "民族")]
public string Nation { get; set; }
///
/// 戶口性質
///
[ImporterHeader(Name = "戶口性質")]
public string HouseholdType { get; set; }
///
/// 聯絡電話
///
[ImporterHeader(Name = "學生聯絡電話")]
public string Phone { get; set; }
///
/// 狀態
/// 測試可為空的列舉型別
///
[ImporterHeader(Name = "狀態")]
public StudentStatus? Status { get; set; }
///
/// 備註
///
[ImporterHeader(Name = "備註")]
public string Remark { get; set; }
///
/// 是否住校(宿舍)
///
[ImporterHeader(IsIgnore = true)]
public bool? IsBoarding { get; set; }
///
/// 所屬班級id
///
[ImporterHeader(IsIgnore = true)]
public Guid ClassId { get; set; }
///
/// 學校Id
///
[ImporterHeader(IsIgnore = true)]
public Guid? SchoolId { get; set; }
///
/// 校區Id
///
[ImporterHeader(IsIgnore = true)]
public Guid? CampusId { get; set; }
///
/// 專業Id
///
[ImporterHeader(IsIgnore = true)]
public Guid? MajorsId { get; set; }
///
/// 年級Id
///
[ImporterHeader(IsIgnore = true)]
public Guid? GradeId { get; set; }
}
```
## Reference
https://github.com/dotnetcore/Magicodes.IE