您好,登錄后才能下訂單哦!
這篇文章主要講解了“ASP.NET開源導入導出庫Magicodes.IE如何完成Csv導入導出”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“ASP.NET開源導入導出庫Magicodes.IE如何完成Csv導入導出”吧!
說明
本章主要說明如何使用Magicodes.IE.Csv進行Csv導入導出.
關于Magicodes.IE
導入導出通用庫,通過導入導出DTO模型來控制導入和導出,支持Excel、Word、Pdf和Html。
主要步驟
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; } }
感謝各位的閱讀,以上就是“ASP.NET開源導入導出庫Magicodes.IE如何完成Csv導入導出”的內容了,經過本文的學習后,相信大家對ASP.NET開源導入導出庫Magicodes.IE如何完成Csv導入導出這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。