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
-
-
-
-
- [ExcelImporter(IsLabelingError = true)]
- public class ImportStudentDto {
-
-
-
- [ImporterHeader(Name = "Serial No.")]
- public long SerialNumber {
- get;
- set;
- }
-
-
-
- [ImporterHeader(Name = "Student Registration No.")]
- [MaxLength(30, ErrorMessage = "The number of words exceeds the maximum limit, please modify!")]
- public string StudentCode {
- get;
- set;
- }
-
-
-
- [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;
- }
-
-
-
- [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;
- }
-
-
-
- [ImporterHeader(Name = "Gender")]
- [Required(ErrorMessage = "Gender cannot be empty")]
- [ValueMapping("Male", 0)]
- [ValueMapping("Female", 1)]
- public Genders Gender {
- get;
- set;
- }
-
-
-
- [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;
- }
-
-
-
- [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;
- }
-
-
-
- [ImporterHeader(Name = "Parental contact number")]
- [MaxLength(20, ErrorMessage = "The number of words exceeds the maximum limit, please modify!")]
- public string GuardianPhone {
- get;
- set;
- }
-
-
-
- [ImporterHeader(Name = "Student ID")]
- [MaxLength(30, ErrorMessage = "The number of words exceeds the maximum limit, please modify!")]
- public string StudentNub {
- get;
- set;
- }
-
-
-
- [ImporterHeader(Name = "Dormitory number")]
- [MaxLength(20, ErrorMessage = "The number of words exceeds the maximum limit, please modify!")]
- public string DormitoryNo {
- get;
- set;
- }
-
-
-
- [ImporterHeader(Name = "QQ number")]
- [MaxLength(30, ErrorMessage = "The number of words exceeds the maximum limit, please modify!")]
- public stringQQ {
- get;
- set;
- }
-
-
-
- [ImporterHeader(Name = "Ethnic")]
- [MaxLength(2, ErrorMessage = "The number of words exceeds the maximum limit, please modify!")]
- public string Nation {
- get;
- set;
- }
-
-
-
- [ImporterHeader(Name = "Household")]
- [MaxLength(10, ErrorMessage = "The number of words exceeds the maximum limit, please modify!")]
- public string HouseholdType {
- get;
- set;
- }
-
-
-
- [ImporterHeader(Name = "Contact number")]
- [MaxLength(20, ErrorMessage = "The number of words exceeds the maximum limit, please modify!")]
- public string Phone {
- get;
- set;
- }
-
-
-
-
- [ImporterHeader(Name = "Status")]
- public Student Status ? Status {
- get;
- set;
- }
-
-
-
- [ImporterHeader(Name = "Remarks")]
- [MaxLength(200, ErrorMessage = "The number of words exceeds the maximum limit, please modify!")]
- public string Remark {
- get;
- set;
- }
-
-
-
- [ImporterHeader(IsIgnore = true)]
- public bool ? IsBoarding {
- get;
- set;
- }
-
-
-
- [ImporterHeader(IsIgnore = true)]
- public GuidClassId {
- get;
- set;
- }
-
-
-
- [ImporterHeader(IsIgnore = true)]
- public Guid ? SchoolId {
- get;
- set;
- }
-
-
-
- [ImporterHeader(IsIgnore = true)]
- public Guid ? CampusId {
- get;
- set;
- }
-
-
-
- [ImporterHeader(IsIgnore = true)]
- public Guid ? MajorsId {
- get;
- set;
- }
-
-
-
- [ImporterHeader(IsIgnore = true)]
- public Guid ? GradeId {
- get;
- set;
- }
- }
As shown in the above code, we defined the above student data Dto with the following main considerations :
- 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 Enumeration
The definition is as follows:
Note point 7 above.
-
Student Status Enumeration
-
- {
-
- [Display(Name = "Normal")] Normal = 0,
-
- [Description("Attrition")] PupilsAway = 1,
-
- [Display(Name = "Suspended")] Suspension = 2,
-
- [Display(Name = "Work-Study")] WorkStudy = 3,
-
- [Display(Name = "Internship")] PostPractice = 4,
-
- [Display(Name = "Graduation")] Graduation = 5,
-
- [Display(Name = "Military")] JoinTheArmy = 6
- }
Note point 7 above
3.Generate import templates and populate them with data
Do you have to prepare a template before importing? Handwritten template?Not required! Magicodes.IE.Excel Provided the basis for DTO Automatic generation Excel Methods of importing templates,We can call it directly. Here we look at the relevant methods for importing:
-
-
-
- public interface IImporter {
-
-
-
-
-
- Task < TemplateFileInfo > GenerateTemplate < T > (stringfileName) whereT: class, new();
-
-
-
-
-
- Task < byte[] > GenerateTemplateBytes < T > () whereT: class, new();
-
-
-
-
-
-
- Task < ImportResult < T >> Import < T > (stringfilePath) whereT: class, new();
- }
By using GenerateTemplate in the above method, we can get the required import template. The specific use can be found in the following unit tests.
- public IImporter Importer = new ExcelImporter();
- [Fact(DisplayName = "Generate Student Data Import Template (Test Enumeration Generate Template)")]
- public asyncTaskGenerateStudentImportTemplate_Test() {
- var filePath = Path.Combine(Directory.GetCurrentDirectory(), nameof(GenerateStudentImportTemplate_Test) + ".xlsx");
- if (File.Exists(filePath)) File.Delete(filePath);
- var result = awaitImporter.GenerateTemplate < ImportStudentDto > (filePath);
- result.ShouldNotBeNull();
- File.Exists(filePath).ShouldBeTrue();
-
- }
The above DTO gets the template and populates the data as shown below,
Note
The enumeration will automatically generate a drop-down selection, and the required column headers will be marked in red
4. Get student import verification errors and data
After populating the data according to the template, we are ready to perform the data import. Typically, we have the following steps.
Validate imported data
Importing data through Magicodes.IE.Excel will automatically perform validation and output the validation results for frontend display. Specifically, we can look through its imported result classes to see.
-
-
-
- public class Import Result < T > whereT: class {
-
-
- public Import Result() {
- RowErrors = newList < DataRowErrorInfo > ();
- }
-
-
-
- public virtual I Collection < T > Data {
- get;
- set;
- }
-
-
-
- public virtual I List < DataRowErrorInfo > RowErrors {
- get;
- set;
- }
-
-
-
- public virtual I List < TemplateErrorInfo > TemplateErrors {
- get;
- set;
- }
-
-
-
- public virtualException Exception {
- get;
- set;
- }
-
-
-
- public virtual boolHasError => Exception != null || (TemplateErrors?.Count(p => p.ErrorLevel == ErrorLevels.Error) ?? 0) > 0 || (RowErrors?.Count ?? 0) > 0;
- }
Among them,
- Data is the data result
-
RowErrors is validation errors, such as required, duplicate validation, text length, etc. will give the line number, field and the set of field errors
-
TemplateErrors is template errors, such as missing mandatory columns and other error messages. Support error level (warning, error)
-
Exception is import exception information
-
HasError is existence of errors (without warnings)
Through ImportResult, we can easily get the import validation error without writing additional code. Usually, we need to determine the HasError property during import and return a specific error result to the frontend.
The data import reference code is shown below,
- [Fact(DisplayName = "Student base data import")]
- public async TaskStudentInfoImporter_Test() {
- var filePath = Path.Combine(Directory.GetCurrentDirectory(), "TestFiles", "Import", "????????.xlsx");
- var import = awaitImporter.Import < ImportStudentDto > (filePath);
- import.ShouldNotBeNull();
- if (
- import.Exception != null) _testOutputHelper.WriteLine(
- import.Exception.ToString());
- if (
- import.RowErrors.Count > 0) _testOutputHelper.WriteLine(JsonConvert.SerializeObject(
- import.RowErrors));
- import.HasError.ShouldBeFalse();
- import.Data.ShouldNotBeNull();
- import.Data.Count.ShouldBe(16);
- }
Get verification markup
The customer said although you hinted, but I still do not know what is wrong! What to do?!!!
We have thoughtfully prepared for you the markup of the Excel file for importing data,
How to open this [Epic Plot]? Simply by one step,
- [ExcelImporter(IsLabelingError=true)]
Once turned on, we will automatically save the markup file "{target file name}_.xlsx" to the target location.
Get imported data
No more errors? That is, HasError is false, then we can just get the Data and do whatever we want!
Finally
This concludes the entire tutorial on importing student data. ** Related libraries will be updated all the time, and there may be slight differences in functional experience with this tutorial, please refer to the relevant specific code, version logs, and unit test examples. **
github:https://github.com/dotnetcore/Magicodes.IE