ASP.NET开源导入导出库Magicodes.IE完成Csv导入导出的方法

与此形成鲜明对比的是另一位巨星——葡萄牙队的c罗。上赛季他虽说有42个进球,但经统计,他的效率不到10%。在这个习惯统计学的社会,c罗的42球从深层分析充满了水分。此外,他的球场道德也属于低劣派,假摔贯穿全场,无处不在,江湖人送外号 "跳水运动员 ",桃色风波更是五花八门,而两者后果呢?看看吧:梅西凭借着08—09赛季带领巴萨取得西甲、国王杯、欧洲冠军联赛三冠王,取得433分,获得金球奖,而c罗只有他的一半不到!

说明

本章主要说明如何使用Magicodes.IE.Csv进行Csv导入导出.

关于Magicodes.IE

导入导出通用库,通过导入导出DTO模型来控制导入和导出,支持Excel、Word、Pdf和Html。

GitHub地址:https://github.com/xin-lai/Magicodes.IE

主要步骤

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

到此这篇关于ASP.NET开源导入导出库Magicodes.IE完成Csv导入导出的方法的文章就介绍到这了,更多相关ASP.NET Csv导入导出内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

您可能有感兴趣的文章
.NET 开源项目Polly的简单介绍

.NET 开源配置组件 AgileConfig的使用简介

开源跨平台运行服务插件TaskCore.MainForm

生成二维码的开源工具对比(附源码)

详解免费开源的.NET多类型文件解压缩组件SharpZipLib(.NET组件介绍之七)