java讀取excel層級結構的遞迴寫法

missxgao發表於2020-09-25

java poi讀取excel這種層級結構的遞迴寫法,思路:同列的迴圈獲取,子級遞迴獲取,想清楚每一個單元格變化的地方,傳不同的引數。exel的單元格的值只能先讀取行,再獲取列。

包括合併單元格的獲取並不是有序的,需要根據行列的起始索引來判斷是否是合併單元格。每次都要先進行判斷。

public List<KnowledgeSystemField> readExcel(String filename, InputStream inputStream) {
		Workbook workbook = null;
		try {
			filename = filepath;
			String filetype = filename.substring(filename.lastIndexOf(".") + 1, filename.length());

			File excelfile = new File(filename);
			if (!excelfile.exists()) {
				log.info("路徑為" + filename + "的檔案不存在!");
				return null;
			}
			inputStream = new FileInputStream(excelfile);

			workbook = getWorkBook(inputStream, filetype);
			// 讀取excel中的資料
			addRowKLevel(workbook);
			List<KnowledgeSystemField> resultDataList = null;
			// resultDataList = parseExcel(workbook);
			return resultDataList;
		} catch (Exception e) {
			log.warn("解析Excel失敗,檔名:" + filename + " 錯誤資訊:" + StrFunc.exception2str(e));
			return null;
		} finally {
			try {
				if (null != workbook) {
					workbook.close();
				}
				if (null != inputStream) {
					inputStream.close();
				}
			} catch (Exception e) {
				log.warn("關閉資料流出錯!錯誤資訊:" + e.getMessage());
				return null;
			}
		}
	}
public Workbook getWorkBook(InputStream in, String filetype) throws Exception {
		Workbook workbook = null;
		if (StrFunc.compareStr(filetype, XLS)) {
			workbook = new HSSFWorkbook(in);
		} else if (StrFunc.compareStr(filetype, XLSX)) {
			workbook = new XSSFWorkbook(in);
		}
		return workbook;
	}
public void addRowKLevel(Workbook workbook) {
		Sheet sheet = workbook.getSheetAt(0);
		if (sheet == null) {
			return;
		}
		int startRow = STARTROW;
		int endRow = sheet.getLastRowNum();
		endRow = 26;
		String dirId = "";
		getCellValue(startRow, endRow, 1, sheet, dirId);
	}

public void getCellValue(int firstRowIndex, int lastRowIndex, int column, Sheet sheet, String dirid) {
		if (column > 4) {
			return;
		}
		int lastRow = firstRowIndex;
		JSONObject multiRow = isMultiRow(firstRowIndex, lastRowIndex, column, sheet);
		if (multiRow.getBoolean("isMulti")) {
			lastRow = multiRow.getInt("lastRow");
		}
		while (lastRow <= lastRowIndex) {
			Cell cell = sheet.getRow(firstRowIndex).getCell(column);
			String knowId = addKnowOrGroup(column, dirid, cell);
			getCellValue(firstRowIndex, lastRow, column + 1, sheet, StrFunc.isNull(knowId) ? dirid : knowId);
			firstRowIndex = lastRow + 1;
			lastRow = firstRowIndex;
			multiRow = isMultiRow(firstRowIndex, lastRowIndex, column, sheet);
			if (multiRow.getBoolean("isMulti")) {
				lastRow = multiRow.getInt("lastRow");
			}
		}
	}
public JSONObject isMultiRow(int firstRowIndex, int lastRowIndex, int column, Sheet sheet) {
		JSONObject obj = new JSONObject();
		obj.put("isMulti", false);
		List<CellRangeAddress> mergedRegions = sheet.getMergedRegions();
		for (int j = 0; j < mergedRegions.size(); j++) {
			CellRangeAddress cr = mergedRegions.get(j);
			int firstColumn = cr.getFirstColumn();
			int lastColumn = cr.getLastColumn();
			int firstRow = cr.getFirstRow();
			int lastRow = cr.getLastRow();
			if (firstColumn == lastColumn && firstColumn == column) {
				if (firstRowIndex == firstRow && lastRow <= lastRowIndex) {
					obj.put("isMulti", true);
					obj.put("lastRow", lastRow);
					break;
				}
			}
		}
		return obj;
	}
private String addKnowOrGroup(int column, String dirid, Cell cell2) {
		String knowId = "";
		String cellvalue = convertCellValueToString(cell2);
		if (!StrFunc.isNull(cellvalue)) {
			boolean ifKnowExist = ifKnowExist(cellvalue);
			if (!ifKnowExist) {
				if (column == 1) {// 第一級分組
					knowId = addGroup(cellvalue, this.knowledgeSetId);
				} else if (column == 4) {// 最後一級圖譜
					knowId = addKnowWithName(cellvalue, dirid);
				} else {
					knowId = addGroup(cellvalue, dirid);
				}
			}
		}
		return knowId;
	}

 

相關文章