EasyExcel simple tutorial (a large amount of data into the database in batches; non-fixed Excel analysis)

foreword

This article records how to use EasyExcel to complete simple table analysis operations, and at the same time realize the batch storage of data in batches in the case of a large amount of data, and record the status of each data storage for statistical results.

Analysis of fixed template and table data format

Realize the entity class corresponding to the content of the Excel template

import com.alibaba.excel.annotation.ExcelIgnore;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.ColumnWidth;
import com.alibaba.excel.annotation.write.style.ContentRowHeight;
import com.alibaba.excel.annotation.write.style.HeadRowHeight;
import lombok.Data;
import java.io.Serializable;

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

    private static final long serialVersionUID = 1L;

    // 被该注解修饰的实体类中的属性值,会作为excel对应的标题头文字
    @ExcelProperty(value = "规则名称")
    private String name;
    
    // 被该注解修饰的实体类中的属性,会在excel与实体类的转换中被忽略
    // 该变量用于标记该条实体类是否成功入库,在入库后操作后更新值为true
    @ExcelIgnore
    private boolean isSuccess;
}


Implement the AnalysisEventListener listening class

Create a new listener class for Excel that needs to be read, which is used for Excel that specializes in processing this type of data. The analysis logic of the database is mainly placed here.


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

    /**
     * 默认每隔500条存储数据库
     */
    private int batchCount = 500;
    /**
     * 缓存的数据列表
     */
    private List<RuleExcel> list = new ArrayList<>();
    /**
     * service
     */
    private final IRuleExcelService RuleExcelService;
	
	// 解析表头
    @Override
    public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
    
    
        List<String> titleList = new ArrayList<>();
        try {
    
    
	        // 获取必要表头列数据
        	// 获取目标属性上@ExcelProperty的值,即属性的中文名称
            String[] nameValue = AnnotationUtil.getAnnotationValue(RuleExcel.class.getDeclaredField("name"), ExcelProperty.class);
            titleList.add(nameValue[0]);
        } catch (NoSuchFieldException e) {
    
    
            e.printStackTrace();
        }
        // 检测获取到的Excel标题头是否合法
        for (String title : titleList) {
    
    
            boolean isExist = false;
            for (Integer i : headMap.keySet()) {
    
    
                if (title.equals(headMap.get(i))) {
    
    
                    isExist = true;
                }
            }
            if (!isExist) {
    
    
                throw new ExcelAnalysisException("模版标题不允许修改");
            }
        }
    }


	// 解析表格内容,一行就是一个RuleExcel实体类
    @Override
    public void invoke(RuleExcel RuleExcel, AnalysisContext analysisContext) {
    
    
	    // 数据合法性校验
        if (String.isEmpty(RuleExcel.getName()) {
    
    
            throw new ExcelAnalysisException("导入失败,必填项按要求填写");
        }
       
        list.add(RuleExcel);
        // 数据入库2:
        // 如果表格数据量很大,可以设定暂存数据量阈值,当现存list数量超过阈值后,会先向数据库中存入一批数据、清空内存后再继续解析
        // 达到BATCH_COUNT,则调用importer方法入库,防止数据几万条数据在内存,容易OOM
        if (list.size() >= batchCount) {
    
    
            // 调用importer方法
            RuleExcelService.importRuleExcel(list);
            // 存储完成清理list
            list.clear();
        }
    }

	// 将所有的表格行数据解析完毕后会执行该方法
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
    
    
    	// 数据入库1:
    	// 一般来说,如果确定表格数据量不大,那么可以在解析完全部excel数据后进行数据入库操作
        RuleExcelService.importRuleExcel(list);
        // 存储完成清理list
        list.clear();
    }
}

Call method example

public void importRuleExcel(MultipartFile file) {
    
    
		// 文件后缀检查
		CommonUtil.checkImportFile(file.getOriginalFilename());
		InputStream inputStream;
		// 存储表格解析结果
		List<RuleExcel> ruleExcels = new ArrayList<>();
		try {
    
    
			// 将Service层变量作为依赖传入
			RuleImportListener importListener = new RuleImportListener(ruleService);
			inputStream = new BufferedInputStream(file.getInputStream());
			// EasyExcel.read()传入:InputStream, 表格数据实体类,表格数据实体类监听器
			ExcelReaderBuilder builder = EasyExcel.read(inputStream, RuleExcel.class, importListener);
			// 执行同步解析操作并返回解析结果
			ruleExcels = builder.doReadAllSync();
		} catch (Exception e) {
    
    
			throw new ServiceException(e.getMessage());
		}
		// 导入成功的列表
		List<RuleExcel> successList = ruleExcels .stream().filter(RuleExcel::isSuccess).collect(Collectors.toList());
	}

Analysis of non-fixed template and non-fixed table data format

Sometimes the template that needs to be read is not fixed, and the declared columns are implemented, and the corresponding data cannot be parsed into a fixed entity class. At this time, we need to read the original parsing data of the table and perform subsequent operations by ourselves .


// EasyExcel.read()传入:InputStream, 表格数据实体类,表格数据实体类监听器
// headRowNumber()传入:表头行的所在行数
List<Map<Integer, String>> resultList = EasyExcelUtil.getEasyExcelReaderBuilder(file.getInputStream, null, null)
											.headRowNumber(0).doReadAllSync();
// resultList 中存储的就是自0行开始解析到的所有数据

Guess you like

Origin blog.csdn.net/Ka__ze/article/details/131811311
Recommended