ASP.NET Open source import and export Library Magicodes.IE How to complete CSV import and export

Time:2020-7-31

explain

This chapter mainly explains how to use it Magicodes.IE.Csv Import and export CSV

about Magicodes.IE

Import and export general library. Import and export dto model is used to control import and export. Excel, word, PDF and HTML are supported.

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

Main steps

1. Installation package Magicodes.IE.Csv


Install-Package Magicodes.IE.Csv

2. Use Magicodes.IE.Csv Export CSV

Through the following code fragment, we will export the content through the corresponding features to make the corresponding processing

ExporterHeaderAttribute

  • DisplayName: display name
  • Format: format
  • Isignore: ignore
public class ExportTestDataWithAttrs
  {
    [exporterheader (displayName = text)]
    public string Text { get; set; }
    [exporterheader (displayName = normal text)] public string text2 {get; set;}
    [exporterheader (displayName = ignored, isignore = true)]
    public string Text3 { get; set; }
    [exporterheader (displayName = numeric value ", format =" #, ාාාාාාාාාාාාාාාාා
    public decimal Number { get; set; }
    [exporterheader (displayName = name ", isautofit = true)]
    public string Name { get; set; }

    /// <summary>
    ///Time test
    /// </summary>
    [exporterheader (displayName = date 1, format = yyyy MM DD)]
    public DateTime Time1 { get; set; }

    /// <summary>
    ///Time test
    /// </summary>
    [exporterheader (displayName = date2 ", format =" yyyy MM DD HH: mm: SS ")]
    public DateTime? Time2 { get; set; }

    public DateTime Time3 { get; set; }

    public DateTime Time4 { get; set; }

    /// <summary>
    ///Long value test
    /// </summary>
    [exporterheader (displayName = long value ", format =" #, #ාාාාාාාාාාාාාාාා
    public long LongNo { get; set; }
  }

Export through 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. Use Magicodes.IE.Csv Import CSV

For the CSV import, we can use the importerheader name property to correspond to our dto attribute, and we can map the enumeration type through valuemapping, and return the corresponding value to us

public async Task StudentInfoImporter_Test()
    {
      var filePath =  Path.Combine ( Directory.GetCurrentDirectory (), "testfiles", "import", "student basic data import. CSV");
      var import = await Importer.Import<ImportStudentDto>(filePath);
    }
 /// <summary>
  ///Import student data dto
  /// </summary>
  public class ImportStudentDto
  {
    /// <summary>
    ///Serial number
    /// </summary>
    [importerheader (name = sequence number)]
    public long SerialNumber { get; set; }

    /// <summary>
    ///Student status number
    /// </summary>
    [importerheader (name = student status number)]
    public string StudentCode { get; set; }
    /// <summary>
    ///Name
    /// </summary>
    [importerheader (name = name)]
    public string Name { get; set; }

    /// <summary>
    ///ID card number
    /// </summary>
    [importerheader (name = ID number)]
    public string IdCard { get; set; }

    /// <summary>
    ///Gender
    /// </summary>
    [importerheader (name = gender)]
    [valuemapping ("male", 0)]
    [valuemapping ("female", 1)]
    public Genders Gender { get; set; }

    /// <summary>
    ///Home address
    /// </summary>
    [importerheader (name = home address)]
    public string Address { get; set; }

    /// <summary>
    ///Name of parent
    /// </summary>
    [importerheader (name = parent name)]
    public string Guardian { get; set; }

    /// <summary>
    ///Contact number of parents
    /// </summary>
    [importerheader (name = parent contact number)]
    public string GuardianPhone { get; set; }

    /// <summary>
    ///Student number
    /// </summary>
    [importerheader (name = student number)]
    public string StudentNub { get; set; }

    /// <summary>
    ///Dormitory number
    /// </summary>
    [importerheader (name = dormitory number)]
    public string DormitoryNo { get; set; }

    /// <summary>
    ///   QQ
    /// </summary>
    [importerheader (name = QQ number)]
    public string QQ { get; set; }

    /// <summary>
    ///Nationality
    /// </summary>
    [importerheader (name = nation)]
    public string Nation { get; set; }

    /// <summary>
    ///Nature of household registration
    /// </summary>
    [importerheader (name = account nature)]
    public string HouseholdType { get; set; }

    /// <summary>
    ///Contact number
    /// </summary>
    [importerheader (name = student contact number)]
    public string Phone { get; set; }

    /// <summary>
    ///Status
    ///Test for nullable enumeration types
    /// </summary>
    [importerheader (name = status)] 
    public StudentStatus? Status { get; set; }

    /// <summary>
    ///Remarks
    /// </summary>
    [importerheader (name = note)]
    public string Remark { get; set; }

    /// <summary>
    ///Whether to live on campus (dormitory)
    /// </summary>
    [ImporterHeader(IsIgnore = true)]
    public bool? IsBoarding { get; set; }

    /// <summary>
    ///Class ID
    /// </summary>
    [ImporterHeader(IsIgnore = true)]
    public Guid ClassId { get; set; }

    /// <summary>
    ///School ID
    /// </summary>
    [ImporterHeader(IsIgnore = true)]
    public Guid? SchoolId { get; set; }

    /// <summary>
    ///Campus ID
    /// </summary>
    [ImporterHeader(IsIgnore = true)]
    public Guid? CampusId { get; set; }

    /// <summary>
    ///Professional ID
    /// </summary>
    [ImporterHeader(IsIgnore = true)]
    public Guid? MajorsId { get; set; }

    /// <summary>
    ///Grade ID
    /// </summary>
    [ImporterHeader(IsIgnore = true)]
    public Guid? GradeId { get; set; }
  }

Reference

https://github.com/dotnetcore/Magicodes.IE

This is about ASP.NET Open source import and export Library Magicodes.IE This is the end of the article on how to complete the CSV import and export ASP.NET CSV import and export content, please search the previous articles of developeppaer or continue to browse the related articles below. I hope you can support developeppaer more in the future!