欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-03-21 11:42:01
说明本章主要说明如何使用magicodes.ie.csv进行csv导入导出.关于magicodes.ie导入导出通用库,通过导入导出dto模型来控制导入和导出,支持excel、word、pdf和htm...

说明

本章主要说明如何使用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导入导出内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!