隨手筆記(二十八)——— 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<sheet.getLastRowNum()-tailLine+1; i++) {

			row = sheet.getRow(i);
			InvoiceProductDetailsDO ip = new InvoiceProductDetailsDO();
			c:
			for(Cell c : row) {
				c.setCellType(Cell.CELL_TYPE_STRING);
				boolean isMerge = isMergedRegion(sheet, i, c.getColumnIndex());
				//判斷是否具有合併單元格
				if(isMerge) {
					int rowNum = row.getRowNum();
					int columnIndex = c.getColumnIndex();
					String rs = getMergedRegionValue(sheet, row.getRowNum(), c.getColumnIndex());
					
				}else {
					int rowNum = row.getRowNum();
					int columnIndex = c.getColumnIndex();
					String rs = c.getRichStringCellValue()+"";
					System.out.print(c.getRichStringCellValue()+"  :"+rowNum+":"+columnIndex);
					
			}
			System.out.println();

		}
		if (StringUtils.isNotBlank(dids)){
			char s = dids.charAt(0);
			if (s==','){
				dids = dids.substring(1);
			}
		}
		

		return in.getId()==null?0:in.getId();
	}

 

/**
	 * 獲取合併單元格的值
	 * @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 ;
	}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章