随手笔记(二十八)——— Java解析不规则Excel

@Override
	public R stackExcelProduct(Integer id, Integer fileId) {
		//1.通过fileId定位文件
		FilesDO filesDO = FilesService.get(fileId);
		String filePath = filesDO.getFilePath();
		//2.对文件进行解析
		Integer aId = readExcelToObjProduct(filePath);
		eDO DO = Dao.get(id);
		String appIds = DO.getAIds();
		if (aId!=0){
			if (StringUtils.isNotBlank(appIds)){
				appIds = appIds + ','+aId;
				DO.setAIds(appIds);
				updateBase(DO);
			}else{
				DO.setAIds(aId.toString());
				updateBase(DO);
			}
			return R.ok();
		}else return R.error("解析异常请检查文件并重新上传");
	}
  • 通过实体类ID以及文件类ID获取文件类与实体类信息
private Integer readExcelToObjProduct(String filePath) {
		Workbook wb = null;
		Integer id = 0;
		try {
			wb = WorkbookFactory.create(new File(filePath));
			id = readExcelProduct(wb, 0, 0, 0);
		} catch (InvalidFormatException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return id==0?0:id;
	}

 接收ID返回值

private Integer readExcelProduct(Workbook wb,int sheetIndex, int startReadLine, int tailLine) {
		Date now = new Date();
		SimpleDateFormat dateFormats = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		String nowStr = dateFormats.format(now);
		Long userId = ShiroUtils.getUserId();
		Sheet sheet = wb.getSheetAt(sheetIndex);
		String dids = "";
		Row row = null;
		InvoiceApplicationGoodsDO in = new InvoiceApplicationGoodsDO();
		s:
		for(int i=startReadLine; i

 

/**
	 * 获取合并单元格的值
	 * @param sheet
	 * @param row
	 * @param column
	 * @return
	 */
	public String getMergedRegionValue(Sheet sheet ,int row , int column){

		int sheetMergeCount = sheet.getNumMergedRegions();

		for(int i = 0 ; i < sheetMergeCount ; i++){
			CellRangeAddress ca = sheet.getMergedRegion(i);
			int firstColumn = ca.getFirstColumn();
			int lastColumn = ca.getLastColumn();
			int firstRow = ca.getFirstRow();
			int lastRow = ca.getLastRow();

			if(row >= firstRow && row <= lastRow){

				if(column >= firstColumn && column <= lastColumn){
					Row fRow = sheet.getRow(firstRow);
					Cell fCell = fRow.getCell(firstColumn);
					return getCellValue(fCell) ;
				}
			}
		}

		return null ;
	}

 

你可能感兴趣的:(Java工作日记,日记)