Import and Export
Files Referenced in this Document
- MyEntityNamesExportModel.cs
- MyEntityNamesImportModel.cs
- MyEntityName.cs
- MyEntityNameSpecification.cs
- MyEntityNameController.cs
- IMyEntityNameRepository.cs
Table of Contents
- Introduction
- Model Structure Design
- Export Feature Implementation
- Import Flow Analysis
- Optimization Strategies for Large Data Import
- Import Template Design Specifications
- Common Error Codes Explanation
- Summary
Introduction
This document comprehensively explains the implementation details of the Excel import and export features in the system, focusing on the two core data transfer models MyEntityNamesExportModel and MyEntityNamesImportModel. The document details how these models transform data with the domain entity MyEntityName, analyzes how the export feature uses MiniExcelLibs to generate formatted Excel templates (including headers, data validation rules, and example data), and deeply dissects the file parsing, data validation, error collection, and feedback mechanisms in the import flow. Additionally, the document focuses on memory optimization strategies for processing large data imports, such as stream reading and batch insertion, providing a complete implementation reference for developers.
Model Structure Design
This section analyzes the structure design and conversion relationships between MyEntityNamesExportModel, MyEntityNamesImportModel, and the domain entity MyEntityName.
MyEntityNamesExportModel Structure
MyEntityNamesExportModel is the data model used for the export feature, internally defining the nested class WorkSectionExportModel to map each row of data in Excel. This model uses attributes under the MiniExcelLibs.Attributes namespace to define Excel column metadata.
- ExcelColumn Attribute: Annotate properties with the
[ExcelColumn]attribute to specify column names (Name) and widths (Width), ensuring the exported Excel file has clear Chinese headers and appropriate column widths. - Property Mapping:
Name,Code, andRemarkproperties correspond to the "Name", "Code", and "Remark" columns respectively, consistent with the properties of the domain entityMyEntityName.
Section sources
MyEntityNamesImportModel Structure
MyEntityNamesImportModel is the data model used for the import feature, cleverly reusing the structure of the export model.
- Inheritance Relationship: The
MyEntityNameImportModelclass inherits fromWorkSectionExportModel, implementing model reuse and ensuring the column structure of the import template is completely consistent with the export template. - Row Index Tracking: Adds a
RowIndexproperty to record the row number of the data in Excel (starting from row 2), facilitating precise error location and friendly error feedback when data validation fails. - Collection Property: The
MyEntityNamesproperty is aList<MyEntityNameImportModel>, which automatically assigns row numbers to each record when values are set, simplifying row number management logic.
Section sources
Domain Entity MyEntityName
MyEntityName is the core entity located in the domain layer, representing an aggregate root in the business.
- Property Definition: Contains core business properties such as
Code,Name,Sort,Remark, andIsDisabled. - Constructor and Validation: Uses the
Volo.Abp.Checkutility class to validate input parameters for non-nullability and length in the constructor andUpdatemethod, ensuring the integrity of the domain model. - Behavior Methods: Provides methods like
UpdateandAdjustSortto encapsulate business logic, adhering to Domain-Driven Design (DDD) principles.
Section sources
Model Conversion Relationship
Diagram sources
Export Feature Implementation
The export feature is implemented via the ExportAsync method in MyEntityNameController, utilizing the MiniExcelLibs library to populate domain data into a predefined Excel template.
Implementation Flow
- Data Preparation: Call
_myEntityNameAppService.ExportAsync(input)to get the data to be exported.input.MaxResultCountis set toint.MaxValueto ensure all data is retrieved. - Template Location: The program first looks for a template file named
MyEntityName导出模板.xlsxin theResources/Templates/path under the application root directory; if not found, it tries to find it in the directory where the assembly is located. - Template Population: Use the
memoryStream.SaveAsByTemplateAsync(templatePath, exportData.Sheets)method to populate the retrieved data (exportData.Sheets) into the corresponding positions in the template. - File Return: Return the generated Excel file stream to the client as a
FileStreamResultand set the download filename (including a timestamp to avoid duplicates).
Template Design
The export template (MyEntityName导出模板.xlsx) is a pre-designed Excel file containing:
- Formatted Headers: Such as "Name", "Code", "Remark", consistent with the definitions in the
ExcelColumnattribute ofWorkSectionExportModel. - Data Validation Rules: May contain uniqueness constraints on the "Code" column or length limits on the "Name" column.
- Example Data: The template may contain a row of example data to guide users on how to fill it correctly.
Diagram sources
Section sources
Import Flow Analysis
The import feature is implemented via the ImportAsync method in MyEntityNameController, involving file parsing, data validation, and persistence.
Implementation Flow
- File Reception and Reading: Receive
IFormFile fileand copy its content toMemoryStreamfor subsequent processing. - Worksheet Identification: Use
stream.GetSheetNames()to get all worksheet names and check for the existence of a worksheet named "Configuration". - Data Parsing: If the "Configuration" worksheet exists, use
MiniExcel.Query<MyEntityNamesImportModel.MyEntityNameImportModel>(stream, sheetName: "Configuration")to directly map Excel data to a list ofMyEntityNameImportModelobjects. This process uses theExcelColumnattribute on the model for column name matching. - Empty Data Check: If the parsed data is empty, throw a
UserFriendlyExceptionprompting the user to check the table. - Business Processing: Encapsulate the parsed data into a
MyEntityNamesImportModelobject and call_myEntityNameAppService.ImportAsync(...)for subsequent business validation and data persistence.
Data Validation Mechanism
Data validation is mainly implemented in the application service layer (MyEntityNameAppService), possibly using specification patterns like MyEntityNameSpecification.
- MyEntityNameSpecification: This specification class can be used to build query expressions, such as querying entities by name. During import, this class can be used to check if a "Name" already exists to prevent duplicate data.
- Error Collection: The validation process collects all errors (such as required items being empty, length exceeded, uniqueness conflicts, etc.) and associates these errors with the corresponding
RowIndex. - Feedback Mechanism: Upon validation failure, the service returns a result object containing all error details (e.g., "Row 3: Name cannot be empty"), allowing the frontend to display a detailed error report.
Diagram sources
Section sources
Optimization Strategies for Large Data Import
To address potential memory overflow issues caused by large data imports, the system adopts the following optimization strategies:
Stream Reading
Although the current code example uses MemoryStream, when handling extra-large files, MiniExcel's streaming API (such as QueryAsync with IAsyncEnumerable) should be used to read and process data row by row instead of loading all data into memory at once. This significantly reduces memory peaks.
Batch Insertion
In the application service's ImportAsync method, do not call the repository's InsertManyAsync all at once. The correct approach is:
- Split the imported data list into smaller batches (e.g., 1000 items per batch).
- Traverse each batch and call the repository's batch insert method.
- Explicit
SaveChangesor transactions can be performed between batches to ensure data consistency while avoiding overly large single database operations.
This batch processing strategy effectively controls database connection load and memory usage, improving import stability and performance.
Section sources
Import Template Design Specifications
To ensure smooth execution of the import feature, the import template design should follow these specifications:
- Worksheet Name: Must contain a worksheet named "Configuration", which is the hardcoded lookup name in the code.
- Column Name Consistency: Column headers must exactly match the
Nameproperty of theExcelColumnattribute inWorkSectionExportModel, i.e., "Name", "Code", "Remark". - Data Format:
- "Code" and "Name" are required and cannot exceed the maximum length defined by the system (defined by
MyEntityNameConsts). - "Remark" is optional.
- "Code" and "Name" are required and cannot exceed the maximum length defined by the system (defined by
- Example Data: It is recommended to provide 1-2 rows of example data in the template to help users understand the data format.
- Avoid Merged Cells: Avoid using merged cells in the data area to prevent parsing issues.
Common Error Codes Explanation
| Error Code/Info | Cause | Solution |
|---|---|---|
Please check the imported table | The "Configuration" worksheet does not exist in the uploaded file, or there is no data in it. | Confirm the file contains a worksheet named "Configuration" and ensure data starts from row 2. |
Name cannot be empty / Code cannot be empty | The "Name" or "Code" column of a row is empty. | Check the reported row number and ensure required items are filled. |
Name already exists | The "Name" of a row duplicates an existing record in the database. | Modify the name to ensure uniqueness, or delete/disable the old record first. |
Code already exists | The "Code" of a row duplicates an existing record in the database. | Modify the code to ensure uniqueness. |
Name length cannot exceed XX characters | The content of the "Name" column exceeds the system-set maximum length. | Shorten the name length. |
Code length cannot exceed XX characters | The content of the "Code" column exceeds the system-set maximum length. | Shorten the code length. |
Summary
This document detailed the implementation of Excel import and export features based on DDD architecture. Efficient data conversion with the domain entity MyEntityName is achieved through carefully designed MyEntityNamesExportModel and MyEntityNamesImportModel models. Using the MiniExcelLibs library, the system can generate formatted export templates and parse import files. The import flow uses specification patterns for data validation and combines row index tracking to provide precise error feedback. For large data scenarios, stream reading and batch insertion optimization strategies are recommended to ensure system stability and performance. By following the template design specifications and error handling guidelines in this document, developers can quickly locate and resolve issues encountered during import and export processes.