ASP. NET Open Source Import and Export Library Magicodes. IE Method for Csv Import and Export

  • 2021-11-13 07:17:29
  • OfStack

Description

This chapter focuses on how to import and export Csv using Magicodes. IE. Csv.

About Magicodes. IE

Import and export general library, through import and export DTO model to control import and export, support Excel, Word, Pdf and Html.

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

Main steps

1. Installation package Magicodes. IE. Csv


Install-Package Magicodes.IE.Csv

2. Exporting Csv using Magicodes. IE. Csv

Through the following code fragment, we will process the exported content through the corresponding features.

ExporterHeaderAttribute

DisplayName: Display name Format: Formatting IsIgnore: Ignore

  public class ExportTestDataWithAttrs
  {
    [ExporterHeader(DisplayName = " Text ")]
    public string Text { get; set; }
    [ExporterHeader(DisplayName = " Ordinary text ")] public string Text2 { get; set; }
    [ExporterHeader(DisplayName = " Ignore ", IsIgnore = true)]
    public string Text3 { get; set; }
    [ExporterHeader(DisplayName = " Numerical value ", Format = "#,##0")]
    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 = " Date 2", Format = "yyyy-MM-dd HH:mm:ss")]
    public DateTime? Time2 { get; set; }

    public DateTime Time3 { get; set; }

    public DateTime Time4 { get; set; }

    /// <summary>
    ///  Long numerical test 
    /// </summary>
    [ExporterHeader(DisplayName = " Long value ", Format = "#,##0")]
    public long LongNo { get; set; }
  }

Export via 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. Import Csv using Magicodes. IE. Csv

For csv import, we can use the ImporterHeader Name attribute to correspond to our Dto attribute. And we can use ValueMapping to map the enumerated type and return the corresponding value to us


    public async Task StudentInfoImporter_Test()
    {
      var filePath = Path.Combine(Directory.GetCurrentDirectory(), "TestFiles", "Import", " Import of students' basic data .csv");
      var import = await Importer.Import<ImportStudentDto>(filePath);
    }
 /// <summary>
  ///  Import Student Data Dto
  /// </summary>
  public class ImportStudentDto
  {
    /// <summary>
    ///    Serial number 
    /// </summary>
    [ImporterHeader(Name = " Serial number ")]
    public long SerialNumber { get; set; }

    /// <summary>
    ///    Student registration number 
    /// </summary>
    [ImporterHeader(Name = " Student registration 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 = " Name of parent ")]
    public string Guardian { get; set; }

    /// <summary>
    ///    Parents' contact number 
    /// </summary>
    [ImporterHeader(Name = " Parents' 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 No. ")]
    public string QQ { get; set; }

    /// <summary>
    ///    Ethnic group 
    /// </summary>
    [ImporterHeader(Name = " Ethnic group ")]
    public string Nation { get; set; }

    /// <summary>
    ///    Nature of household registration 
    /// </summary>
    [ImporterHeader(Name = " Nature of household registration ")]
    public string HouseholdType { get; set; }

    /// <summary>
    ///    Contact number 
    /// </summary>
    [ImporterHeader(Name = " Student contact telephone number ")]
    public string Phone { get; set; }

    /// <summary>
    ///    Status 
    ///    Test nullable enumeration types 
    /// </summary>
    [ImporterHeader(Name = " Status ")] 
    public StudentStatus? Status { get; set; }

    /// <summary>
    ///    Remarks 
    /// </summary>
    [ImporterHeader(Name = " Remarks ")]
    public string Remark { get; set; }

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

    /// <summary>
    ///    Class to which you belong id
    /// </summary>
    [ImporterHeader(IsIgnore = true)]
    public Guid ClassId { get; set; }

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

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

    /// <summary>
    ///    Specialties 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


Related articles: