Main points
- Use Magicodes.IE.Excel to complete the Excel data import
- Create a Dto for import data.
- Magicodes.IE.Excel can automatically generate imported Excel templates, data validation (including duplicate validation), template validation, read settings, value constraints and mapping, and output Excel validation markup based on Dto and feature settings.
Main steps
Installation package - Magicodes.IE.Excel
In this tutorial, we will only demonstrate the use of Excel to complete the import of student data. We need to install the following packages in the prepared project with the following reference commands,
Install-Package Magicodes.IE.Excel
Create Import Dto
The main code is shown below,
Student Data Dto
- /// <summary>
- /// Importing Student Data Dto
- /// IsLabelingError:Whether to mark data errors
- /// </summary>
- [ExcelImporter(IsLabelingError = true)]
- public class ImportStudentDto {
- /// <summary>
- /// Serial No.
- /// </summary>
- [ImporterHeader(Name = "Serial No.")]
- public long SerialNumber {
- get;
- set;
- }
- /// <summary>
- /// Student Registration No.
- /// </summary>
- [ImporterHeader(Name = "Student Registration No.")]
- [MaxLength(30, ErrorMessage = "The number of words exceeds the maximum limit, please modify!")]
- public string StudentCode {
- get;
- set;
- }
- /// <summary>
- /// Name
- /// </summary>
- [ImporterHeader(Name = "Name")]
- [Required(ErrorMessage = "Name cannot be empty")]
- [MaxLength(50, ErrorMessage = "The number of words exceeds the maximum limit, please modify!")]
- public string Name {
- get;
- set;
- }
- /// <summary>
- /// ID number
- /// </summary>
- [ImporterHeader(Name = "ID number", IsAllowRepeat = false)]
- [Required(ErrorMessage = "ID number cannot be empty")]
- [MaxLength(18, ErrorMessage = "The number of words exceeds the maximum limit, please modify!")]
- public string IdCard {
- get;
- set;
- }
- /// <summary>
- /// Gender
- /// </summary>
- [ImporterHeader(Name = "Gender")]
- [Required(ErrorMessage = "Gender cannot be empty")]
- [ValueMapping("Male", 0)]
- [ValueMapping("Female", 1)]
- public Genders Gender {
- get;
- set;
- }
- /// <summary>
- /// Home Address
- /// </summary>
- [ImporterHeader(Name = "Home Address")]
- [Required(ErrorMessage = "Home Address cannot be empty")]
- [MaxLength(200, ErrorMessage = "The number of words exceeds the maximum limit, please modify!")]
- public string Address {
- get;
- set;
- }
- /// <summary>
- /// Parent's name
- /// </summary>
- [ImporterHeader(Name = "Parent's name")]
- [Required(ErrorMessage = "Parent's name cannot be empty")]
- [MaxLength(50, ErrorMessage = "The number of words exceeds the maximum limit, please modify!")]
- public string Guardian {
- get;
- set;
- }
- /// <summary>
- /// Parental contact number
- /// </summary>
- [ImporterHeader(Name = "Parental contact number")]
- [MaxLength(20, ErrorMessage = "The number of words exceeds the maximum limit, please modify!")]
- public string GuardianPhone {
- get;
- set;
- }
- /// <summary>
- /// Student ID
- /// </summary>
- [ImporterHeader(Name = "Student ID")]
- [MaxLength(30, ErrorMessage = "The number of words exceeds the maximum limit, please modify!")]
- public string StudentNub {
- get;
- set;
- }
- /// <summary>
- /// Dormitory number
- /// </summary>
- [ImporterHeader(Name = "Dormitory number")]
- [MaxLength(20, ErrorMessage = "The number of words exceeds the maximum limit, please modify!")]
- public string DormitoryNo {
- get;
- set;
- }
- /// <summary>
- /// </summary>
- [ImporterHeader(Name = "QQ number")]
- [MaxLength(30, ErrorMessage = "The number of words exceeds the maximum limit, please modify!")]
- public stringQQ {
- get;
- set;
- }
- /// <summary>
- /// Ethnic
- /// </summary>
- [ImporterHeader(Name = "Ethnic")]
- [MaxLength(2, ErrorMessage = "The number of words exceeds the maximum limit, please modify!")]
- public string Nation {
- get;
- set;
- }
- /// <summary>
- /// Household
- /// </summary>
- [ImporterHeader(Name = "Household")]
- [MaxLength(10, ErrorMessage = "The number of words exceeds the maximum limit, please modify!")]
- public string HouseholdType {
- get;
- set;
- }
- /// <summary>
- /// Contact number
- /// </summary>
- [ImporterHeader(Name = "Contact number")]
- [MaxLength(20, ErrorMessage = "The number of words exceeds the maximum limit, please modify!")]
- public string Phone {
- get;
- set;
- }
- /// <summary>
- /// Status
- /// Test for nullable enumeration types
- /// </summary>
- [ImporterHeader(Name = "Status")]
- public Student Status ? Status {
- get;
- set;
- }
- /// <summary>
- /// Remarks
- /// </summary>
- [ImporterHeader(Name = "Remarks")]
- [MaxLength(200, ErrorMessage = "The number of words exceeds the maximum limit, please modify!")]
- public string Remark {
- get;
- set;
- }
- /// <summary>
- /// Whether live on campus (dormitory)
- /// </summary>
- [ImporterHeader(IsIgnore = true)]
- public bool ? IsBoarding {
- get;
- set;
- }
- /// <summary>
- /// Class id
- /// </summary>
- [ImporterHeader(IsIgnore = true)]
- public GuidClassId {
- 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>
- /// MajorsId
- /// </summary>
- [ImporterHeader(IsIgnore = true)]
- public Guid ? MajorsId {
- get;
- set;
- }
- /// <summary>
- /// GradeId
- /// </summary>
- [ImporterHeader(IsIgnore = true)]
- public Guid ? GradeId {
- get;
- set;
- }
- }
- ExcelImporterfeature allows you to set some global settings for importing, such as whether to mark errors, the name of the imported Sheet (if not set, the first one is automatically obtained), the number of columns to be read, and the position of the sheet header.
- Support common data validation settings, such as required and maximum length.
- Support data duplicate verification, such as ID numbers. Refer ImporterHeader features of IsAllowRepeat setting.
- Supports column header settings,For example ImporterHeader - Name Properties?Other than that,ImporterHeader also supports automatic space filtering (enabled by default), disposing off all spaces, column indexing, etc.?
- Ignore setting is enabled for data columns,For example SchoolId "[ImporterHeader(IsIgnore = true)]"?
- Value mapping is used, For example “Gender” attribute. When value mapping is enabled, the value mapping will not be retrieved from the enumeration definition?
- Enumeration support, support for enumeration from the Display?Description Get value mapping?See below for enumeration definitions?
-
Gender EnumerationThe definition is as follows:
- /// <summary> /// Gender /// </summary> public enum Genders
- {
- /// <summary> /// Man /// </summary> Man = 0,
- /// <summary> /// Female /// </summary> Female = 1
- }
Note point 7 above.
-
Student Status Enumeration
- /// <summary> /// Student Status: Normal, Attrition, Suspended, Work-Study, Internship, Graduation, Military /// </summary> public enum StudentStatus
- {
- /// <summary> /// Normal /// </summary>
- [Display(Name = "Normal")] Normal = 0,
- /// <summary> /// Attrition /// </summary>
- [Description("Attrition")] PupilsAway = 1,
- /// <summary> /// Suspended /// </summary>
- [Display(Name = "Suspended")] Suspension = 2,
- /// <summary> /// Work-Study /// </summary>
- [Display(Name = "Work-Study")] WorkStudy = 3,
- /// <summary> /// Internship /// </summary>
- [Display(Name = "Internship")] PostPractice = 4,
- /// <summary> /// Graduation /// </summary>
- [Display(Name = "Graduation")] Graduation = 5,
- /// <summary> /// Military /// </summary>
- [Display(Name = "Military")] JoinTheArmy = 6
- }
Note point 7 above





Join the conversation! Your thoughts help the community grow.