SoFunction
Updated on 2025-04-13

Use EasyExcel to implement simple Excel table parsing operations

Preface

This article records how to use EasyExcel to complete simple table parsing operations, and at the same time realize batch entry of data in large amounts of data, and record the status of each data entry in order to perform results statistics.

Analysis of fixed templates and table data formats

Implement the entity class corresponding to the content of Excel template

import ;
import ;
import ;
import ;
import ;
import ;
import ;

@Data
@ColumnWidth(24)
@HeadRowHeight(20)
@ContentRowHeight(18)
public class RuleExcel implements Serializable {

    private static final long serialVersionUID = 1L;

    // The attribute value in the entity class modified by this annotation will be used as the title header corresponding to Excel    @ExcelProperty(value = "Rules Name")
    private String name;
    
    // The properties in the entity class modified by this annotation will be ignored in the conversion between excel and entity class.    // This variable is used to mark whether the entity class has been successfully entered into the library. After the operation is entered, the value is updated to true    @ExcelIgnore
    private boolean isSuccess;
}

Implement AnalysisEventListener listening class

Create a new listening class for Excel that needs to be read, which is used to process Excel specifically for this type of data. The database parsing logic is mainly placed here.

@Data
@RequiredArgsConstructor
@EqualsAndHashCode(callSuper = true)
public class RuleExcelImportListener extends AnalysisEventListener<RuleExcel> {

    /**
      * By default, every 500 databases are stored
      */
    private int batchCount = 500;
    /**
      * Cache data list
      */
    private List<RuleExcel> list = new ArrayList<>();
    /**
     * service
     */
    private final IRuleExcelService RuleExcelService;
	
	// parse the table header    @Override
    public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
        List<String> titleList = new ArrayList<>();
        try {
	        // Get the necessary header column data        	// Get the value of @ExcelProperty on the target property, that is, the Chinese name of the property            String[] nameValue = (("name"), );
            (nameValue[0]);
        } catch (NoSuchFieldException e) {
            ();
        }
        // Detect whether the obtained Excel header is legal        for (String title : titleList) {
            boolean isExist = false;
            for (Integer i : ()) {
                if (((i))) {
                    isExist = true;
                }
            }
            if (!isExist) {
                throw new ExcelAnalysisException("Template title is not allowed to be modified");
            }
        }
    }


	// Analyze the table contents, one line is a RuleExcel entity class    @Override
    public void invoke(RuleExcel RuleExcel, AnalysisContext analysisContext) {
	    // Data legality verification        if ((()) {
            throw new ExcelAnalysisException("Import failed, the required items are filled in as required");
        }
       
        (RuleExcel);
        // Data storage 2:        // If the table data volume is large, you can set the threshold for temporary data volume. When the number of existing lists exceeds the threshold, a batch of data will be stored in the database first, and the memory will be cleared before continuing to parse.        // When BATCH_COUNT is reached, the importer method is called to the library to prevent tens of thousands of pieces of data from being in memory, making it easy to OOM        if (() >= batchCount) {
            // Call the importer method            (list);
            // Store complete cleaning list            ();
        }
    }

	// This method will be executed after parsing all table row data    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
    	// Data storage 1:    	// Generally speaking, if you are sure that the table data is not large, you can perform data entry operations after parsing the complete excel data.        (list);
        // Store complete cleaning list        ();
    }
}

Calling method example

public void importRuleExcel(MultipartFile file) {
		// File suffix check		(());
		InputStream inputStream;
		//Storing table analysis results		List<RuleExcel> ruleExcels = new ArrayList<>();
		try {
			// Pass the Service layer variables into it as a dependency			RuleImportListener importListener = new RuleImportListener(ruleService);
			inputStream = new BufferedInputStream(());
			// () Pass in: InputStream, tabular data entity class, tabular data entity class listener			ExcelReaderBuilder builder = (inputStream, , importListener);
			// Perform synchronous parsing operation and return the parsing result			ruleExcels = ();
		} catch (Exception e) {
			throw new ServiceException(());
		}
		// Import successfully list		List<RuleExcel> successList = ruleExcels .stream().filter(RuleExcel::isSuccess).collect(());
	}

Analysis of non-fixed templates and non-fixed table data formats

Sometimes the templates that need to be read are not fixed and well declared columns, and the corresponding data cannot be parsed into a fixed entity class. At this time, we need to read the original parsed data of the table and perform subsequent operations on our own.

// () Pass in: InputStream, tabular data entity class, tabular data entity class listener// headRowNumber() passes in: the number of rows in the header rowList<Map<Integer, String>> resultList = (, null, null)
											.headRowNumber(0).doReadAllSync();
// The resultList stores all the data parsed from line 0

This is the end of this article about using EasyExcel to implement simple Excel table analysis operations. For more information about EasyExcel parsing Excel tables, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!