ASP.NET open source import and export library Magicodes.IE completes the import and export of Csv

Magicodes.IE Csv import and export

explain

This chapter mainly describes how to use Magicodes.IE.Csv to import and export Csv

Main steps

1. Installation package Magicodes.IE.Csv

Install-Package Magicodes.IE.Csv

2. Export Csv using Magicodes.IE.Csv

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

ExporterHeaderAttribute

  • DisplayName: display name
  • Format: format
  • IsIgnore: ignore or not
    public class ExportTestDataWithAttrs
    {
        [ExporterHeader(DisplayName = "Bold text", IsBold = true)]
        public string Text { get; set; }
        [ExporterHeader(DisplayName = "Plain 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 value test
        /// </summary>
        [ExporterHeader(DisplayName = "Long value", Format = "#,##0")]
        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. Import Csv using Magicodes.IE.Csv

For csv import, we can use the ImporterHeader Name property to correspond to our Dto property. We can also use ValueMapping to map enumeration types and return corresponding values 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>
        ///No
        /// </summary>
        [ImporterHeader(Name = "No")]
        public long SerialNumber { get; set; }

        /// <summary>
        ///Student No
        /// </summary>
        [ImporterHeader(Name = "Student No")]
        public string StudentCode { get; set; }
        /// <summary>
        ///Name
        /// </summary>
        [ImporterHeader(Name = "full name")]
        public string Name { get; set; }

        /// <summary>
        ///ID card No
        /// </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>
        ///Parent contact number
        /// </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 No
        /// </summary>
        [ImporterHeader(Name = "Dormitory No")]
        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>
        ///Account nature
        /// </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 nullable enumeration types
        /// </summary>
        [ImporterHeader(Name = "state")] 
        public StudentStatus? Status { get; set; }

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

        /// <summary>
        ///Whether to live in the school (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>
        ///Major 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

Tags: ASP.NET IE github

Posted on Thu, 14 May 2020 08:03:49 -0700 by llanitedave