List of usage examples for org.apache.poi.ss.usermodel Sheet getLastRowNum
int getLastRowNum();
From source file:org.bbreak.excella.core.util.PoiUtil.java
License:Open Source License
/** * ???? ??? /* w ww . ja va 2 s .c om*/ * A0???????-1? * * @param sheet * @return ???? */ public static int getLastColNum(Sheet sheet) { int lastColNum = 0; for (int i = 0; i <= sheet.getLastRowNum(); i++) { if (sheet.getRow(i) == null) { continue; } int tmpColNum = sheet.getRow(i).getLastCellNum(); if (lastColNum < tmpColNum) { lastColNum = tmpColNum; } } return lastColNum - 1; }
From source file:org.bbreak.excella.reports.listener.BreakAdapter.java
License:Open Source License
/** * @see org.bbreak.excella.reports.listener.ReportProcessAdaptor#postParse(org.apache.poi.ss.usermodel.Sheet, org.bbreak.excella.core.SheetParser, org.bbreak.excella.core.SheetData) *///from w ww . j av a 2 s .c om @Override public void postParse(Sheet sheet, SheetParser sheetParser, SheetData sheetData) throws ParseException { int firstRowNum = sheet.getFirstRowNum(); int lastRowNum = sheet.getLastRowNum(); for (int rowIndex = firstRowNum; rowIndex <= lastRowNum; rowIndex++) { Row row = sheet.getRow(rowIndex); if (row != null) { parseRow(sheet, sheetParser, sheetData, row, rowIndex); } } }
From source file:org.bbreak.excella.reports.listener.RemoveAdapter.java
License:Open Source License
@Override public void postParse(Sheet sheet, SheetParser sheetParser, SheetData sheetData) throws ParseException { int firstRowNum = sheet.getFirstRowNum(); int lastRowNum = sheet.getLastRowNum(); for (int rowIndex = firstRowNum; rowIndex <= lastRowNum; rowIndex++) { Row row = sheet.getRow(rowIndex); if (row != null) { int firstColNum = row.getFirstCellNum(); int lastColNum = row.getLastCellNum() - 1; boolean isRowFlag = false; for (int colIndex = firstColNum; colIndex <= lastColNum; colIndex++) { Cell cell = row.getCell(colIndex); if (cell != null) { if (cell.getCellTypeEnum() == CellType.STRING && cell.getStringCellValue().contains(RemoveParamParser.DEFAULT_TAG)) { // ?? String[] paramArray = getStrParam(sheet, rowIndex, colIndex); // ?? String removeUnit = paramArray[0]; // ?? row.removeCell(cell); // ???? if (removeUnit.equals("") || removeUnit.equals(ROW)) { removeRegion(sheet, rowIndex, -1); removeControlRow(sheet, rowIndex); isRowFlag = true; break; } else if (removeUnit.equals(CELL) || removeUnit.equals(COLUMN)) { // ??????? removeCellOrCol(paramArray, removeUnit, sheet, row, cell, rowIndex, colIndex); }//from ww w .j a v a 2s .c o m lastColNum = row.getLastCellNum() - 1; colIndex--; } // ?? if (isControlRow(sheet, sheetParser, row, cell)) { removeControlRow(sheet, rowIndex); isRowFlag = true; break; } } } // ??? if (isRowFlag) { lastRowNum = sheet.getLastRowNum(); rowIndex--; } } } }
From source file:org.bbreak.excella.reports.listener.RemoveAdapter.java
License:Open Source License
/** * ???????/* ww w.j a va 2 s .c o m*/ * * @param paramArray * @param removeUnit * @param sheet * @param row * @param cell * @param rowIndex * @param colIndex * @return paramArray */ private void removeCellOrCol(String[] paramArray, String removeUnit, Sheet sheet, Row row, Cell cell, int rowIndex, int colIndex) { if (removeUnit.equals(CELL)) { removeRegion(sheet, rowIndex, colIndex); if (paramArray.length > 1) { Row removeRow = sheet.getRow(rowIndex); if (removeRow != null) { Cell removeCell = removeRow.getCell(colIndex); if (removeCell != null) { removeRow.removeCell(removeCell); } } // ?? String direction = paramArray[1]; if (direction.equals(LEFT)) { shiftLeft(row, cell, colIndex); } else if (direction.equals(UP)) { shiftUp(sheet, cell, rowIndex, colIndex); } } else { // ????????? shiftLeft(row, cell, colIndex); } } else if (removeUnit.equals(COLUMN)) { removeRegion(sheet, -1, colIndex); // ?? for (int rowNum = 0; rowNum <= sheet.getLastRowNum(); rowNum++) { Row removeCol = sheet.getRow(rowNum); if (removeCol != null) { Cell removeCell = removeCol.getCell(colIndex); if (removeCell != null) { removeCol.removeCell(removeCell); } } shiftLeft(sheet.getRow(rowNum), cell, colIndex); } } }
From source file:org.bbreak.excella.reports.listener.RemoveAdapter.java
License:Open Source License
/** * ??????//from ww w .jav a2 s .c o m * * @param sheet * @param cell * @param rowIndex * @param colIndex */ private void shiftUp(Sheet sheet, Cell cell, int rowIndex, int colIndex) { // ? int startCopyIndex = rowIndex + 1; int finishCopyIndex = sheet.getLastRowNum(); for (int copyRowNum = startCopyIndex; copyRowNum <= finishCopyIndex; copyRowNum++) { Row row = sheet.getRow(copyRowNum); if (row != null) { Row preRow = sheet.getRow(copyRowNum - 1); // Cell fromCell = row.getCell(colIndex); if (fromCell != null) { // Cell toCell = null; if (preRow == null) { preRow = sheet.createRow(copyRowNum - 1); } toCell = preRow.getCell(colIndex); if (toCell == null) { toCell = preRow.createCell(colIndex); } PoiUtil.copyCell(fromCell, toCell); row.removeCell(fromCell); } } } }
From source file:org.bbreak.excella.reports.listener.RemoveAdapter.java
License:Open Source License
/** * ?//from ww w.j ava 2 s . c o m * * @param sheet * @param rowIndex */ private void removeControlRow(Sheet sheet, int rowIndex) { // ???? if (rowIndex == sheet.getLastRowNum()) { sheet.removeRow(sheet.getRow(rowIndex)); } else { sheet.removeRow(sheet.getRow(rowIndex)); // #35 POI??????????????????? // ??????0???????? CellRangeAddress rangeAddress = new CellRangeAddress(rowIndex, rowIndex, 0, PoiUtil.getLastColNum(sheet)); PoiUtil.deleteRangeUp(sheet, rangeAddress); // sheet.shiftRows( rowIndex + 1, sheet.getLastRowNum(), -1, true, true); } }
From source file:org.bbreak.excella.reports.processor.ReportsWorkbookTest.java
License:Open Source License
protected List<ParsedReportInfo> parseSheet(ReportsTagParser<?> parser, Sheet sheet, ReportsParserInfo reportsParserInfo) throws ParseException { List<ParsedReportInfo> parsedList = new ArrayList<ParsedReportInfo>(); for (int rowIndex = 0; rowIndex <= sheet.getLastRowNum(); rowIndex++) { Row row = sheet.getRow(rowIndex); if (row == null) { continue; }//from www . ja va 2s .c om for (int columnIndex = 0; columnIndex <= row.getLastCellNum(); columnIndex++) { Cell cell = row.getCell(columnIndex); if (cell == null) { continue; } if (parser.isParse(sheet, cell)) { parsedList.add(parser.parse(sheet, cell, reportsParserInfo)); } } } return parsedList; }
From source file:org.bbreak.excella.reports.ReportsTestUtil.java
License:Open Source License
/** * /*from w w w. ja v a2s. c o m*/ * * @param expected * @param actual * @param isActCopyOfExp ??????true * @throws ReportsCheckException */ public static void checkSheet(Sheet expected, Sheet actual, boolean isActCopyOfExp) throws ReportsCheckException { List<CheckMessage> errors = new ArrayList<CheckMessage>(); Workbook expectedWorkbook = expected.getWorkbook(); Workbook actualWorkbook = actual.getWorkbook(); if (log.isDebugEnabled()) { log.debug("[" + actualWorkbook.getSheetName(actualWorkbook.getSheetIndex(actual)) + "] check start!"); } // ---------------------- // ???? // ---------------------- // ?? String eSheetName = expectedWorkbook.getSheetName(expectedWorkbook.getSheetIndex(expected)); String aSheetName = actualWorkbook.getSheetName(actualWorkbook.getSheetIndex(actual)); if (!isActCopyOfExp) { if (!eSheetName.equals(aSheetName)) { errors.add(new CheckMessage("??", eSheetName, aSheetName)); } } // ? String ePrintSetupString = getPrintSetupString(expected.getPrintSetup()); String aPrintSetupString = getPrintSetupString(actual.getPrintSetup()); if (!ePrintSetupString.equals(aPrintSetupString)) { errors.add(new CheckMessage("?", ePrintSetupString, aPrintSetupString)); } // ? String eHeaderString = getHeaderString(expected.getHeader()); String aHeaderString = getHeaderString(actual.getHeader()); if (!eHeaderString.equals(aHeaderString)) { errors.add(new CheckMessage("", eHeaderString, aHeaderString)); } String eFooterString = getFooterString(expected.getFooter()); String aFooterString = getFooterString(actual.getFooter()); if (!eFooterString.equals(aFooterString)) { errors.add(new CheckMessage("", eFooterString, aFooterString)); } // String eBreaksString = getBreaksString(expected); String aBreaksString = getBreaksString(actual); log.debug(eBreaksString + "/" + aBreaksString); if (!eBreaksString.equals(aBreaksString)) { errors.add(new CheckMessage("", eBreaksString, aBreaksString)); } // ? String expectedPrintArea = expectedWorkbook.getPrintArea(expectedWorkbook.getSheetIndex(expected)); String actualPrintArea = actualWorkbook.getPrintArea(actualWorkbook.getSheetIndex(actual)); if (expectedPrintArea != null || actualPrintArea != null) { // ????????Null????????????? // if ( expectedPrintArea == null || actualPrintArea == null || !equalPrintArea( expectedPrintArea, actualPrintArea, isActCopyOfExp)) { // errors.add( new CheckMessage( "?", expectedPrintArea, actualPrintArea)); // } if (!isActCopyOfExp) { if (expectedPrintArea == null || actualPrintArea == null || !expectedPrintArea.equals(actualPrintArea)) { errors.add(new CheckMessage("?", expectedPrintArea, actualPrintArea)); } } } // (?) String ePaneInformationString = getPaneInformationString(expected.getPaneInformation()); String aPaneInformationString = getPaneInformationString(actual.getPaneInformation()); if (!ePaneInformationString.equals(aPaneInformationString)) { errors.add(new CheckMessage("(?)", expectedPrintArea, actualPrintArea)); } // ?????? // ????? // ????? // // if (expected.isDisplayGridlines() ^ actual.isDisplayGridlines()) { errors.add(new CheckMessage("", String.valueOf(expected.isDisplayGridlines()), String.valueOf(actual.isDisplayGridlines()))); } // ? if (expected.isDisplayRowColHeadings() ^ actual.isDisplayRowColHeadings()) { errors.add(new CheckMessage("?", String.valueOf(expected.isDisplayRowColHeadings()), String.valueOf(actual.isDisplayRowColHeadings()))); } // ? if (expected.isDisplayFormulas() ^ actual.isDisplayFormulas()) { errors.add(new CheckMessage("?", String.valueOf(expected.isDisplayFormulas()), String.valueOf(actual.isDisplayFormulas()))); } // ?? if (expected.getNumMergedRegions() != actual.getNumMergedRegions()) { errors.add(new CheckMessage("??", String.valueOf(expected.getNumMergedRegions()), String.valueOf(actual.getNumMergedRegions()))); } for (int i = 0; i < actual.getNumMergedRegions(); i++) { CellRangeAddress actualAddress = null; if (expected instanceof HSSFSheet) { actualAddress = ((HSSFSheet) actual).getMergedRegion(i); } else if (expected instanceof XSSFSheet) { actualAddress = ((XSSFSheet) actual).getMergedRegion(i); } StringBuffer expectedAdressBuffer = new StringBuffer(); boolean equalAddress = false; for (int j = 0; j < expected.getNumMergedRegions(); j++) { CellRangeAddress expectedAddress = null; if (expected instanceof HSSFSheet) { expectedAddress = ((HSSFSheet) expected).getMergedRegion(j); } else if (expected instanceof XSSFSheet) { expectedAddress = ((XSSFSheet) expected).getMergedRegion(j); } if (expectedAddress.toString().equals(actualAddress.toString())) { equalAddress = true; break; } CellReference crA = new CellReference(expectedAddress.getFirstRow(), expectedAddress.getFirstColumn()); CellReference crB = new CellReference(expectedAddress.getLastRow(), expectedAddress.getLastColumn()); expectedAdressBuffer.append(" [" + crA.formatAsString() + ":" + crB.formatAsString() + "]"); } if (!equalAddress) { errors.add(new CheckMessage("??", expectedAdressBuffer.toString(), actualAddress.toString())); } } int maxColumnNum = -1; if (expected instanceof HSSFSheet) { maxColumnNum = HSSF_MAX_COLUMN_NUMBER; } else if (expected instanceof XSSFSheet) { maxColumnNum = XSSF_MAX_COLUMN_NUMBER; } for (int i = 0; i < maxColumnNum; i++) { try { // checkCellStyle(expected.getWorkbook(), expected.getColumnStyle(i), actual.getWorkbook(), actual.getColumnStyle(i)); } catch (ReportsCheckException e) { CheckMessage checkMessage = e.getCheckMessages().iterator().next(); checkMessage.setMessage("[" + i + "]" + checkMessage.getMessage()); errors.add(checkMessage); } // if (expected.getColumnWidth(i) != actual.getColumnWidth(i)) { errors.add(new CheckMessage("[" + i + "]", String.valueOf(expected.getColumnWidth(i)), String.valueOf(actual.getColumnWidth(i)))); } } // ??? if (expected.getLastRowNum() != actual.getLastRowNum()) { // ?????? if (expected.getLastRowNum() < actual.getLastRowNum()) { int lastRowIndex = -1; if (expected instanceof HSSFSheet) { lastRowIndex = 0; } Iterator<Row> rowIterator = actual.rowIterator(); while (rowIterator.hasNext()) { Row row = rowIterator.next(); // ????? Iterator<Cell> cellIterator = row.cellIterator(); while (cellIterator.hasNext()) { Cell cell = cellIterator.next(); if (cell.getCellTypeEnum() != CellType.BLANK) { lastRowIndex = row.getRowNum(); break; } } } if (expected.getLastRowNum() != lastRowIndex) { errors.add(new CheckMessage("", String.valueOf(expected.getLastRowNum()), String.valueOf(lastRowIndex))); } } else { errors.add(new CheckMessage("", String.valueOf(expected.getLastRowNum()), String.valueOf(actual.getLastRowNum()))); } } if (errors.isEmpty()) { for (int i = 0; i <= expected.getLastRowNum(); i++) { try { checkRow(expected.getRow(i), actual.getRow(i)); } catch (ReportsCheckException e) { errors.addAll(e.getCheckMessages()); } } } if (!errors.isEmpty()) { if (log.isErrorEnabled()) { for (CheckMessage message : errors) { log.error("?[" + message.getMessage() + "]"); log.error(":" + message.getExpected()); log.error(":" + message.getActual()); } } throw new ReportsCheckException(errors); } if (log.isDebugEnabled()) { log.debug("[" + actualWorkbook.getSheetName(actualWorkbook.getSheetIndex(actual)) + "] check end."); } }
From source file:org.bbreak.excella.reports.tag.BlockColRepeatParamParser.java
License:Open Source License
@Override public ParsedReportInfo parse(Sheet sheet, Cell tagCell, Object data) throws ParseException { try {// www .j a v a2s . co m // ?? Map<String, String> paramDef = TagUtil.getParams(tagCell.getStringCellValue()); // ? checkParam(paramDef, tagCell); ReportsParserInfo info = (ReportsParserInfo) data; ParamInfo paramInfo = info.getParamInfo(); // ParsedReportInfo parsedReportInfo = new ParsedReportInfo(); List<Object> resultList = new ArrayList<Object>(); // BC????? String bcTagname = paramDef.get(PARAM_VALUE); if (log.isDebugEnabled()) { log.debug("BC?? : " + bcTagname); } // ????? Object[] paramInfos = getParamData(paramInfo, bcTagname); if (paramInfos == null) { return parsedReportInfo; } // ???? List<SingleParamParser> singleParsers = getSingleReplaceParsers(info); // POJOParamInfo??? List<ParamInfo> paramInfoList = new ArrayList<ParamInfo>(); for (Object obj : paramInfos) { if (obj instanceof ParamInfo) { paramInfoList.add((ParamInfo) obj); continue; } ParamInfo childParamInfo = new ParamInfo(); Map<String, Object> map = PropertyUtils.describe(obj); for (Map.Entry<String, Object> entry : map.entrySet()) { for (ReportsTagParser<?> parser : singleParsers) { childParamInfo.addParam(parser.getTag(), entry.getKey(), entry.getValue()); } } paramInfoList.add(childParamInfo); } // ?? if (paramDef.containsKey(PARAM_MIN_REPEAT_NUM)) { Integer minRepeatNum = Integer.valueOf(paramDef.get(PARAM_MIN_REPEAT_NUM)); if (minRepeatNum > paramInfoList.size()) { int addEmptyRowNum = minRepeatNum - paramInfoList.size(); for (int num = 0; num < addEmptyRowNum; num++) { ParamInfo childParamInfo = new ParamInfo(); paramInfoList.add(childParamInfo); } } } paramInfos = paramInfoList.toArray(new ParamInfo[paramInfoList.size()]); // ? Integer repeatNum = paramInfos.length; if (paramDef.containsKey(PARAM_REPEAT)) { if (Integer.valueOf(paramDef.get(PARAM_REPEAT)) < repeatNum) { repeatNum = Integer.valueOf(paramDef.get(PARAM_REPEAT)); } } // ? // TODO ? /* * Integer turnNum = null; if (paramDef.containsKey( PARAM_TURN)) { turnNum = Integer.valueOf( paramDef.get( PARAM_TURN)); } */ // ?????????? Map<String, Object> beforeBlockSingleDataMap = new HashMap<String, Object>(); // ??? if (paramDef.containsKey(PARAM_DUPLICATE)) { String[] tmp = paramDef.get(PARAM_DUPLICATE).split(";"); // single??? for (String str : tmp) { for (ReportsTagParser<?> parser : singleParsers) { str = parser.getTag() + TAG_PARAM_PREFIX + str + TAG_PARAM_SUFFIX; // ??key??? beforeBlockSingleDataMap.put(str, ""); } } } // removeTag boolean removeTag = false; if (paramDef.containsKey(PARAM_REMOVE_TAG)) { removeTag = Boolean.valueOf(paramDef.get(PARAM_REMOVE_TAG)); } // fromCell int[] fromCellPosition = ReportsUtil.getCellIndex(paramDef.get(PARAM_FROM), PARAM_FROM); int defaultFromCellRowIndex = tagCell.getRow().getRowNum() + fromCellPosition[0]; int defaultFromCellColIndex = tagCell.getColumnIndex() + fromCellPosition[1]; // toCell int[] toCellIndex = ReportsUtil.getCellIndex(paramDef.get(PARAM_TO), PARAM_TO); int defaultToCellRowIndex = tagCell.getRow().getRowNum() + toCellIndex[0]; int defaultToCellColIndex = tagCell.getColumnIndex() + toCellIndex[1]; // ?(??) int blockEndRowIndex = defaultToCellRowIndex; int blockEndColIndex = defaultToCellColIndex; int blockStartRowIndex = defaultFromCellRowIndex; int blockStartColIndex = defaultFromCellColIndex; // BC??Cell[row][col] Object[][] blockCellsValue = ReportsUtil.getBlockCellValue(sheet, defaultFromCellRowIndex, defaultToCellRowIndex, defaultFromCellColIndex, defaultToCellColIndex); CellStyle[][] blockCellsStyle = ReportsUtil.getBlockCellStyle(sheet, defaultFromCellRowIndex, defaultToCellRowIndex, defaultFromCellColIndex, defaultToCellColIndex); CellType[][] blockCellTypes = ReportsUtil.getBlockCellType(sheet, defaultFromCellRowIndex, defaultToCellRowIndex, defaultFromCellColIndex, defaultToCellColIndex); // ????? int[] columnWidths = ReportsUtil.getColumnWidth(sheet, defaultFromCellColIndex, defaultToCellColIndex); // ? int rowlen = defaultToCellRowIndex - defaultFromCellRowIndex + 1; int collen = defaultToCellColIndex - defaultFromCellColIndex + 1; // ??? CellRangeAddress[] margedCells = ReportsUtil.getMargedCells(sheet, defaultFromCellRowIndex, defaultToCellRowIndex, defaultFromCellColIndex, defaultToCellColIndex); // ???? int turnCount = 0; int maxblockEndRowIndex = blockEndRowIndex; TagParser<?> parser = null; ParsedReportInfo result = null; // for (int repeatCount = 0; repeatCount < repeatNum; repeatCount++) { collen = defaultToCellColIndex - defaultFromCellColIndex + 1; // ??????blockStartRowIndex??blockEndColIndex? // TODO ? /* * if (turnNum != null && turnNum <= turnCount) { blockStartRowIndex = blockEndRowIndex + 1; blockEndColIndex = 0; turnCount = 0; } */ // if (turnCount > 0) { blockStartColIndex = blockEndColIndex + 1; blockEndColIndex = blockStartColIndex + collen - 1; } else { // ??????? 2009/11/16 // blockStartColIndex = tagCell.getColumnIndex(); } turnCount++; // ???BC if (repeatCount > 0) { CellRangeAddress rangeAddress = new CellRangeAddress(blockStartRowIndex, sheet.getLastRowNum(), blockStartColIndex, blockStartColIndex + collen - 1); PoiUtil.insertRangeRight(sheet, rangeAddress); if (log.isDebugEnabled()) { log.debug(""); log.debug(blockStartRowIndex + ":" + sheet.getLastRowNum() + ":" + blockStartColIndex + ":" + (blockStartColIndex + collen - 1)); } // ??? // ??????????? // ?????? int targetColNum = blockEndColIndex - defaultToCellColIndex; for (CellRangeAddress address : margedCells) { // ???(BC????????) // ???(BC????????) // ??? + BC?? - BC? // ??? + BC?? - BC? int firstRowNum = address.getFirstRow(); int lastRowNum = address.getLastRow(); int firstColumnNum = address.getFirstColumn() + targetColNum; int lastColumnNum = address.getLastColumn() + targetColNum; CellRangeAddress copyAddress = new CellRangeAddress(firstRowNum, lastRowNum, firstColumnNum, lastColumnNum); sheet.addMergedRegion(copyAddress); } } // ? if (log.isDebugEnabled()) { log.debug("?????? =" + repeatCount); } for (int rowIdx = 0; rowIdx < blockCellsValue.length; rowIdx++) { // ? // int copyToRowIndex = blockStartRowIndex + rowIdx; Row row = sheet.getRow(copyToRowIndex); // ??null??????????????? if (row == null && !ReportsUtil.isEmptyRow(blockCellTypes[rowIdx], blockCellsValue[rowIdx], blockCellsStyle[rowIdx])) { // ???? // ? // (??)?????????? // ??null???(RowCreate?)??????????? row = sheet.createRow(copyToRowIndex); } if (row != null) { // ? for (int colIdx = 0; colIdx < blockCellsValue[rowIdx].length; colIdx++) { // int copyToColIndex = blockStartColIndex + colIdx; // ? sheet.setColumnWidth(copyToColIndex, columnWidths[colIdx]); // ? Cell cell = row.getCell(copyToColIndex); // ?? CellType cellType = blockCellTypes[rowIdx][colIdx]; // ?? Object cellValue = blockCellsValue[rowIdx][colIdx]; // ?? CellStyle cellStyle = blockCellsStyle[rowIdx][colIdx]; // ????????????????? if (cell == null && !ReportsUtil.isEmptyCell(cellType, cellValue, cellStyle)) { cell = row.createCell(copyToColIndex); } // if (cell != null) { // ? cell.setCellType(cellType); // ?? PoiUtil.setCellValue(cell, cellValue); // ?? if (cellStyle != null) { cell.setCellStyle(cellStyle); } if (log.isDebugEnabled()) { log.debug("row=" + (copyToRowIndex) + " col" + (copyToColIndex) + ">>>>>>" + blockCellsValue[rowIdx][colIdx]); } } } } } // ????????? int plusRowNum = 0; // ????????? int plusColNum = 0; collen = defaultToCellColIndex - defaultFromCellColIndex + 1; // for (int targetRow = blockStartRowIndex; targetRow < blockStartRowIndex + rowlen + plusRowNum; targetRow++) { if (sheet.getRow(targetRow) == null) { if (log.isDebugEnabled()) { log.debug("row=" + targetRow + " : row is not available. continued..."); } continue; } // ? Cell chgTargetCell = null; // for (int targetCol = blockStartColIndex; targetCol <= blockStartColIndex + collen + plusColNum - 1; targetCol++) { // ???? chgTargetCell = sheet.getRow(targetRow).getCell(targetCol); if (chgTargetCell == null) { if (log.isDebugEnabled()) { log.debug("row=" + targetRow + " col=" + targetCol + " : cell is not available. continued..."); } continue; } parser = info.getMatchTagParser(sheet, chgTargetCell); if (parser == null) { if (log.isDebugEnabled()) { log.debug("row=" + targetRow + " col=" + targetCol + " parser is not available. continued..."); } continue; } String chgTargetCellString = chgTargetCell.getStringCellValue(); if (log.isDebugEnabled()) { log.debug("########## row=" + targetRow + " col=" + targetCol + " =" + chgTargetCellString + " ##########"); } // ? result = (ParsedReportInfo) parser.parse(sheet, chgTargetCell, info.createChildParserInfo((ParamInfo) paramInfos[repeatCount])); // ??? plusRowNum += result.getRowIndex() - result.getDefaultRowIndex(); // ??? plusColNum += result.getColumnIndex() - result.getDefaultColumnIndex(); // ?single??????????? // ??????????? if (parser instanceof SingleParamParser && beforeBlockSingleDataMap.containsKey(chgTargetCellString)) { if (beforeBlockSingleDataMap.get(chgTargetCellString) .equals(result.getParsedObject())) { // PoiUtil.setCellValue(chgTargetCell, ""); } else { // ???? beforeBlockSingleDataMap.put(chgTargetCellString, result.getParsedObject()); } } // ?????????????? if (blockStartColIndex != result.getDefaultColumnIndex() && maxblockEndRowIndex <= blockEndRowIndex && result.getRowIndex() > result.getDefaultRowIndex()) { CellRangeAddress preRangeAddress = new CellRangeAddress(blockEndRowIndex + 1, blockEndRowIndex + (result.getRowIndex() - result.getDefaultRowIndex()), blockStartColIndex, targetCol - 1); PoiUtil.insertRangeDown(sheet, preRangeAddress); if (log.isDebugEnabled()) { log.debug("******"); log.debug("1 : " + (blockEndRowIndex + 1) + ":" + (blockEndRowIndex + (result.getRowIndex() - result.getDefaultRowIndex())) + ":" + blockStartColIndex + ":" + (targetCol - 1)); } } // R?????? if (parser instanceof RowRepeatParamParser && maxblockEndRowIndex <= blockEndRowIndex && result.getRowIndex() > result.getDefaultRowIndex()) { CellRangeAddress rearRangeAddress = new CellRangeAddress(blockEndRowIndex + 1, blockEndRowIndex + (result.getRowIndex() - result.getDefaultRowIndex()), result.getDefaultColumnIndex() + 1, blockEndColIndex); PoiUtil.insertRangeDown(sheet, rearRangeAddress); if (log.isDebugEnabled()) { log.debug("******"); log.debug("2 : " + (blockEndRowIndex + 1) + ":" + (blockEndRowIndex + (result.getRowIndex() - result.getDefaultRowIndex())) + ":" + (result.getDefaultColumnIndex() + 1) + ":" + blockEndColIndex); } } blockEndRowIndex = defaultToCellRowIndex + plusRowNum; resultList.add(result.getParsedObject()); if (parser instanceof BlockColRepeatParamParser || parser instanceof BlockRowRepeatParamParser) { collen += result.getColumnIndex() - result.getDefaultColumnIndex(); plusColNum -= result.getColumnIndex() - result.getDefaultColumnIndex(); } // ???????? if (blockStartColIndex + collen + plusColNum - 1 > blockEndColIndex) { int beforeLastColIndex = blockEndColIndex; blockEndColIndex = blockStartColIndex + collen + plusColNum - 1; // ??????????? CellRangeAddress preRangeAddress = new CellRangeAddress(tagCell.getRowIndex(), targetRow - 1, beforeLastColIndex + 1, blockEndColIndex); PoiUtil.insertRangeRight(sheet, preRangeAddress); if (log.isDebugEnabled()) { log.debug("******"); log.debug("1 : " + tagCell.getRowIndex() + ":" + (targetRow - 1) + ":" + (beforeLastColIndex + 1) + ":" + blockEndColIndex); } // ?????? int lastRowNum = sheet.getLastRowNum(); if ((blockEndRowIndex + 1) <= lastRowNum && (beforeLastColIndex + 1) <= blockEndColIndex) { CellRangeAddress rangeAddress = new CellRangeAddress(blockEndRowIndex + 1, lastRowNum, beforeLastColIndex + 1, blockEndColIndex); PoiUtil.insertRangeRight(sheet, rangeAddress); if (log.isDebugEnabled()) { log.debug("******"); log.debug("3 : " + (blockEndRowIndex + 1) + ":" + lastRowNum + ":" + (beforeLastColIndex + 1) + ":" + blockEndColIndex); } } } // ? } // ?????? if (blockStartColIndex + collen + plusColNum - 1 < blockEndColIndex) { CellRangeAddress rearRangeAddress = new CellRangeAddress(targetRow, targetRow, blockStartColIndex + collen + plusColNum, blockEndColIndex); PoiUtil.insertRangeRight(sheet, rearRangeAddress); if (log.isDebugEnabled()) { log.debug("******"); log.debug("2 : " + targetRow + ":" + targetRow + ":" + (blockStartColIndex + collen + plusColNum) + ":" + blockEndColIndex); } } plusColNum = 0; // ? } // ??????? if (maxblockEndRowIndex < blockEndRowIndex) { if (log.isDebugEnabled()) { log.debug("******"); } if (repeatCount != 0) { CellRangeAddress preRangeAddress = new CellRangeAddress(maxblockEndRowIndex + 1, blockEndRowIndex, defaultFromCellColIndex, blockStartColIndex - 1); PoiUtil.insertRangeDown(sheet, preRangeAddress); if (log.isDebugEnabled()) { log.debug("1 : " + (maxblockEndRowIndex + 1) + ":" + blockEndRowIndex + ":" + defaultFromCellColIndex + ":" + (blockStartColIndex - 1)); } } // ??????? CellRangeAddress rearRangeAddress = new CellRangeAddress(maxblockEndRowIndex + 1, blockEndRowIndex, blockEndColIndex + 1, PoiUtil.getLastColNum(sheet)); PoiUtil.insertRangeDown(sheet, rearRangeAddress); if (log.isDebugEnabled()) { log.debug("2 : " + (maxblockEndRowIndex + 1) + ":" + blockEndRowIndex + ":" + (blockEndColIndex + 1) + ":" + PoiUtil.getLastColNum(sheet)); } maxblockEndRowIndex = blockEndRowIndex; } } // if (removeTag) { tagCell.setCellType(CellType.BLANK); } // parsedReportInfo.setDefaultRowIndex(defaultToCellRowIndex); parsedReportInfo.setDefaultColumnIndex(defaultToCellColIndex); parsedReportInfo.setColumnIndex(blockEndColIndex); parsedReportInfo.setParsedObject(resultList); parsedReportInfo.setRowIndex(maxblockEndRowIndex); if (log.isDebugEnabled()) { log.debug("finalBlockRowIndex= " + maxblockEndRowIndex); log.debug("finalBlockColIndex=" + blockEndColIndex); } return parsedReportInfo; } catch (Exception e) { throw new ParseException(tagCell, e); } }
From source file:org.bbreak.excella.trans.tag.sheet2java.SheetToJavaExecuter.java
License:Open Source License
/** * ??????<BR>//from w w w.j ava 2s. c om * ???????<BR> * * @param targetSheet ? * @param targetColumnInfoList * @return * @throws ParseException */ protected List<Object> parseTargetSheet(Sheet targetSheet, SheetToJavaParseInfo sheetInfo, List<SheetToJavaSettingInfo> targetColumnInfoList) throws ParseException { // ?? List<Object> results = new ArrayList<Object>(); int logicalRowNum = sheetInfo.getLogicalNameRowNum() - 1; int valueStartRowNum = sheetInfo.getValueRowNum() - 1; int valueEndRowNum = targetSheet.getLastRowNum(); // ????index? Map<String, Integer> colLogicalNameMap = new HashMap<String, Integer>(); // colLogicalNameMap? Row row = targetSheet.getRow(logicalRowNum); if (row != null) { // ????? int firstColIdx = row.getFirstCellNum(); int lastColIdx = row.getLastCellNum(); for (int colIdx = firstColIdx; colIdx <= lastColIdx; colIdx++) { Cell cell = row.getCell(colIdx); if (cell != null) { try { // ??? String logicalCellValue = cell.getStringCellValue(); if (!logicalCellValue.startsWith(BookController.COMMENT_PREFIX)) { colLogicalNameMap.put(logicalCellValue, colIdx); } } catch (Exception e) { throw new ParseException(cell, e); } } } } // ????????????? List<Class<?>> classList = new ArrayList<Class<?>>(); // ?SettingInfo? Map<Class<?>, List<SheetToJavaSettingInfo>> settingInfoListMap = new HashMap<Class<?>, List<SheetToJavaSettingInfo>>(); // ??????? Map<Class<?>, List<String>> uniquePropertyListMap = new HashMap<Class<?>, List<String>>(); for (SheetToJavaSettingInfo settingInfo : targetColumnInfoList) { // ?? Class<?> clazz = settingInfo.getClazz(); List<SheetToJavaSettingInfo> settingInfoList = settingInfoListMap.get(clazz); if (settingInfoList == null) { // ????????? settingInfoList = new ArrayList<SheetToJavaSettingInfo>(); } List<String> uniquePropertyList = uniquePropertyListMap.get(clazz); if (uniquePropertyList == null) { // ????????? uniquePropertyList = new ArrayList<String>(); } // ?? settingInfoList.add(settingInfo); if (settingInfo.isUnique()) { uniquePropertyList.add(settingInfo.getPropertyName()); } // ??? if (!classList.contains(clazz)) { classList.add(clazz); } // ?? settingInfoListMap.put(clazz, settingInfoList); uniquePropertyListMap.put(clazz, uniquePropertyList); } // ??? for (Class<?> clazz : classList) { // ?? List<Object> objList = new ArrayList<Object>(); Object obj = null; try { // ??? for (int valueRowIdx = valueStartRowNum; valueRowIdx <= valueEndRowNum; valueRowIdx++) { Row valueRow = targetSheet.getRow(valueRowIdx); if (valueRow == null) { continue; } boolean isProcessRow = true; for (SheetToJavaListener propertyListener : sheetToJavaListeners) { if (!propertyListener.preProcessRow(valueRow)) { isProcessRow = false; } } if (!isProcessRow) { continue; } obj = Class.forName(clazz.getName()).newInstance(); // ??? List<SheetToJavaSettingInfo> settingInfoList = settingInfoListMap.get(clazz); for (SheetToJavaSettingInfo settingInfo : settingInfoList) { // ?? String propertyName = settingInfo.getPropertyName(); // Object value = settingInfo.getValue(); // ? Object settingValue = value; Cell valueCell = null; if (value instanceof String) { // ?? String settingValueStr = (String) value; if (settingValueStr.startsWith(TAG_PREFIX)) { // ?? if (settingValueStr.startsWith(TAG_LOGICAL_NAME_PREFIX)) { // ????? String logicalKey = TagUtil.getParam(settingValueStr, LNAME_TAG_PARAM_PREFIX, LNAME_TAG_PARAM_SUFFIX); Integer logicalKeyCol = colLogicalNameMap.get(logicalKey); if (logicalKeyCol == null) { Cell errorCell = null; for (SheetToJavaSettingInfo columnInfo : targetColumnInfoList) { if (columnInfo.getValue().equals(settingValueStr)) { errorCell = columnInfo.getValueCell(); } } throw new ParseException(errorCell, "????:" + logicalKey); } valueCell = valueRow.getCell(logicalKeyCol); if (valueCell != null) { Class<?> propertyClass = PropertyUtils.getPropertyType(obj, settingInfo.getPropertyName()); try { settingValue = PoiUtil.getCellValue(valueCell, propertyClass); } catch (RuntimeException e) { throw new ParseException(valueCell, "???????(" + propertyClass + ")", e); } } else { // ?null?? settingValue = null; valueCell = null; } } else { // ????? // ?? parseCustomProperty(valueCell, colLogicalNameMap, obj, valueRow, settingValueStr); // ?? continue; } } } // try { // ????? for (SheetToJavaListener propertyListener : sheetToJavaListeners) { propertyListener.preSetProperty(valueCell, obj, propertyName, settingValue); } PropertyUtils.setProperty(obj, propertyName, settingValue); // ???? for (SheetToJavaListener propertyListener : sheetToJavaListeners) { propertyListener.postSetProperty(valueCell, obj, propertyName, settingValue); } } catch (ParseException parseEx) { throw parseEx; } catch (RuntimeException e) { throw new ParseException(valueCell, "??????(" + propertyName + "=" + settingValue + "[" + settingValue.getClass().getCanonicalName() + "]" + ")", e); } } for (SheetToJavaListener propertyListener : sheetToJavaListeners) { if (!propertyListener.postProcessRow(valueRow, obj)) { isProcessRow = false; } } if (!isProcessRow) { continue; } List<String> uniquePropertyList = uniquePropertyListMap.get(clazz); if (!isDuplicateObj(obj, objList, uniquePropertyList)) { // ??????? objList.add(obj); } } // ???? results.addAll(objList); } catch (ParseException parseEx) { throw parseEx; } catch (Exception e) { throw new ParseException(e.toString()); } } return results; }