About Easyexcel read EXCEL merged cell information

About Easyexcel read EXCEL merged cell information

1. excel template

@Configuration
public class TestModel {
    
    
	/**
	 *  开发部
	 */
	@ExcelProperty(value = "开发部",index = 0)
	private String kaifabu;
	
	/**
	 * A2
	 */
	@ExcelProperty(value = "A2",index = 1)
	private String a2qiyoutian;
	
	/**
	 * 区块
	 */
	@ExcelProperty(value = "区块",index =2)
	private String qukuai;
	
	/**
	 * 项目部
	 */
	@ExcelProperty(value = "项目部",index =3)
	private String xiangmubu;
	
	/**
	 * 开发部
	 */
	@ExcelProperty(value = "开发部1部", index =4)
	private String kaifabu1;

	public String getKaifabu() {
    
    
		return kaifabu;
	}

	public void setKaifabu(String kaifabu) {
    
    
		this.kaifabu = kaifabu;
	}

	public String geta2qiyoutian() {
    
    
		return a2qiyoutian;
	}

	public void seta2qiyoutian(String a2qiyoutian) {
    
    
		this.a2qiyoutian = a2qiyoutian;
	}

	public String getQukuai() {
    
    
		return qukuai;
	}

	public void setQukuai(String qukuai) {
    
    
		this.qukuai = qukuai;
	}

	public String getXiangmubu() {
    
    
		return xiangmubu;
	}

	public void setXiangmubu(String xiangmubu) {
    
    
		this.xiangmubu = xiangmubu;
	}

	public String getKaifabu1() {
    
    
		return kaifabu1;
	}

	public void setKaifabu1(String kaifabu1) {
    
    
		this.kaifabu1 = kaifabu1;
	}

	@Override
	public String toString() {
    
    
		return "TestModel [kaifabu=" + kaifabu + ", a2qiyoutian=" + a2qiyoutian + ", qukuai=" + qukuai + ", xiangmubu="
				+ xiangmubu + ", kaifabu1=" + kaifabu1 + "]";
	}
}

2. Service layer (processing cells)


@Service
public class TryService extends AnalysisEventListener<Map<Integer,String>>{
    
    
// class readModel extends AnalysisEventListener<Map<Integer, String>>{
    
    
	
	private static final Logger log = LoggerFactory.getLogger(TryService.class);

		// 所有要插入的数据
		 public static List<TestModel> datas = new ArrayList<>();
			// 缓存上一级跨行的数据
			Map<Integer, String>preContent;
			@Override
			public void invoke(Map<Integer, String> o, AnalysisContext analysisContext) {
    
    
				// TODO Auto-generated method stub
				TestModel testModel = new TestModel();
				String kaifabu = o.get(0);
				String a2qiyoutian = o.get(1);
				String qukuai = o.get(2);
				String xiangmubu = o.get(3);
				String kaifabu1 = o.get(4);
				
				// 如果数据是空的话,从缓存对象中取值
				if(StringUtils.isEmpty(kaifabu)){
    
    
				kaifabu = preContent.get(0);
				}
				if(StringUtils.isEmpty(a2qiyoutian)) {
    
    
				a2qiyoutian = preContent.get(1);
				}
				if(StringUtils.isEmpty(qukuai)) {
    
    
				qukuai = preContent.get(2);
				}
				if(StringUtils.isEmpty(xiangmubu)) {
    
    
				xiangmubu = preContent.get(3);
				}
				if(StringUtils.isEmpty(kaifabu1)) {
    
    
				kaifabu1 = preContent.get(4);
				}
        // 为单元格赋值
				testModel.setKaifabu(kaifabu);
				testModel.seta2qiyoutian(a2qiyoutian);
				testModel.setQukuai(qukuai);
				testModel.setXiangmubu(xiangmubu);
				testModel.setKaifabu1(kaifabu1);
				
				// 将数据放入list
				datas.add(testModel);
				// 复用跨行数据的实现
				String uniquKey = o.get(0);
				if(!StringUtils.isEmpty(uniquKey) || preContent == null) {
    
    
					preContent = o;
				}
			}

			@Override
			public void doAfterAllAnalysed(AnalysisContext context) {
    
    
				// TODO Auto-generated method stub
				log.info("读取数据完成");
			}
}

	

It is mainly based on the principle of traditional EasyExcel reading information, and the cells are processed, and the excel table data is mainly stored in datas

		// 将数据放入list
			datas.add(testModel);

The principle is to put the merged cell data into the Map for storage, and then judge the contents of the cell. If the cell in this interval is empty, assign the value in the cache to the cell.

	String uniquKey = o.get(0);
				if(!StringUtils.isEmpty(uniquKey) || preContent == null) {
    
    
					preContent = o;
				}

If the value of column A is empty, it means that this is across rows and columns. If the value is not empty, it means that this is the first time to read across rows and columns of data. If preContent is empty, it means that this is the first time to read the sheet form The data

3. Rest layer

The code here is relatively rough (there may be unnecessary codes), and it is mainly used to test whether the method is reasonable and correct.

@RequestMapping("uploadProjectDepartmentExcel")
	public JSONObject uploadProjectDepartmentExcel(@RequestParam(value = "file") MultipartFile file) {
    
    
		JSONObject returnData = new JSONObject();
		JSONObject response = new JSONObject();
		returnData.put("cmd", "uploadProjectDepartmentExcel");
		returnData.put("type", "response");
		returnData.put("response", response);
		
		try {
    
    
			// 检验前端数据
			if(file == null || file.isEmpty()) {
    
    
				throw new MyException("未找到 file字段");
			}
			// 暂存临时文件
			String path = FilePath.CACHE_PATH + "/uploadFile" + System.currentTimeMillis() + "/";
			String originalFilename = file.getOriginalFilename();
			log.info("缓存文件:"+originalFilename );
			File filepath = new File(path);
			
			// 创建文件夹
			if(!filepath.exists()) {
    
    
				filepath.mkdirs();
			}
			// 文件路径
			File uploadFile = new File(path + originalFilename);
			file.transferTo(uploadFile);
			/*************************** 1、读取数据 ***************************/
			// 表单
			int sheetNo = 0;
			// 行数(从该行开始读取)
			int headRownumber = 0;
			EasyExcel.read(uploadFile,new ProjectManagementExcelService()).sheet(sheetNo).headRowNumber(headRownumber).doRead();
			List<ProjectManagementExcelModel> message = ProjectManagementExcelService.datas;
			response.put("res", true);
			response.put("message", message);
		}catch (MyException e) {
    
    
			// TODO: handle exception
			log.error("uploadProjectDepartmentExcel",e.getReason());
			response.put("res", false);
			response.put("exception", e.getReason());
		}catch (Exception e) {
    
    
			// TODO: handle exception
			log.error("uploadProjectDepartmentExcel",e.getMessage());
			response.put("res", false);
			response.put("exception", e.getMessage());
		}
		return returnData;
	}

I tried two methods of text address and text flow to import excel, and later found that it is feasible to import excel through address.

Guess you like

Origin blog.csdn.net/weixin_51220967/article/details/126433314