EasyExcel reads data from the specified position

I have come into contact with easyExcel a little bit before, instead of the traditional poi. When I used it last time, the header was more traditional and not complicated, but this time the header is a bit more complicated. Reading data should start from the specified position. The easyExcel was read from the specified location, so after constant exploration, a suitable solution was found.

Big guys, please take a detour. This record is mainly for your convenience next time.

1. First look at my header, as shown in the figure below: Rows 1-5 are headers

To start reading the data, the 6th line is the real data. Just go to the code and look at the red icon, headRowNumber(). If you don't write it, the default is 1, that is, start reading the data from the second line.

2. Talk about the process of importing data:

1.) Header verification: invokeHeadMap() method.

/**
	 * @param headMap 传入excel头部(第一行数据)数据的index,name
	 * 					校验excel头部格式,必须完全匹配
	 */
	int x=0;
	@Override
	public void invokeHeadMap(Map<Integer,String> headMap,AnalysisContext context) {
		x++;
		super.invokeHeadMap(headMap, context);
		System.out.println(headMap.size());
		if(x==3 && headMap.size()!=20) {
			teamList.add("解析Excel出错,请传入正确模板的Excel");
		}
	}

2.) Data analysis one by one invoke() method, the method contains my business logic and data verification. MedBatchDetailsPerExcel is the specific data value of each row

/**
	 * 这个每一条数据解析的时候都回来调用
	 */
	List<MedBatchDetailsPer> medBatchDetailsList = new ArrayList<>();//要导入的数据
	List<MedBatchDetailsPer> medBatchDetailsList3=null;
	@Override
	public void invoke(MedBatchDetailsPerExcel data, AnalysisContext context) {
		log.info("解析到一条数据:{}",JSON.toJSONString(data));
		total++;
		Integer rowIndex=context.readRowHolder().getRowIndex()+1;//+1行(实际中excel中第几行的数据)
		MedBatchDetailsPer medBatchDetailsPer=importDataCheck1(data,rowIndex);
		importDataCheck(data,rowIndex);
		List<MedBatchDetailsPer> medBatchDetailsList2 =repeatImportCheck(data,rowIndex);//重复导入校验
		if (medBatchDetailsList2 != null && medBatchDetailsList2.size() > 0) {
			medBatchDetailsList3.addAll(medBatchDetailsList2);
		}
		medBatchDetailsList.add(medBatchDetailsPer);
	}

3.) After all the data is analyzed, the doAfterAllAnalysed() method has a method for saving data.

/**
	 * 所有数据解析都完成之后,调用
	 */
	@Override
	public void doAfterAllAnalysed(AnalysisContext context) {
		// 这里也要保存数据,确保最后遗留的数据也存储到数据库
		insertAllData(empId,medBatchDetailsList.size(),medBatchDetailsList,medBatchDetailsList3);
		log.info("所有数据都解析完成!");		
	}

The easyExcel steps are just these steps. The specific business logic is different. It is just a knock~~~ This time, it mainly records the import and read of the complex header.

 

Guess you like

Origin blog.csdn.net/zhangleiyes123/article/details/106670561