Example usage for org.apache.poi.ss.usermodel Cell getColumnIndex

List of usage examples for org.apache.poi.ss.usermodel Cell getColumnIndex

Introduction

In this page you can find the example usage for org.apache.poi.ss.usermodel Cell getColumnIndex.

Prototype

int getColumnIndex();

Source Link

Document

Returns column index of this cell

Usage

From source file:org.bbreak.excella.core.util.PoiUtil.java

License:Open Source License

/**
 * ??????/*w  ww  .  ja  va 2  s .c om*/
 * 
 * @param sheet 
 * @param firstRowIndex 
 * @param lastRowIndex 
 * @return ?
 */
public static int getLastColumnNum(Sheet sheet, int firstRowIndex, int lastRowIndex) {
    // ??
    int rangeLastColumn = -1;
    for (int i = firstRowIndex; i <= lastRowIndex; i++) {
        Row row = sheet.getRow(i);
        if (row == null) {
            continue;
        }
        Iterator<Cell> rowIterator = row.iterator();
        while (rowIterator.hasNext()) {
            Cell cell = rowIterator.next();
            if (cell != null) {
                if (rangeLastColumn < cell.getColumnIndex()) {
                    rangeLastColumn = cell.getColumnIndex();
                }
            }
        }
    }
    return rangeLastColumn;
}

From source file:org.bbreak.excella.core.util.PoiUtil.java

License:Open Source License

/**
 * ??//from   w ww.ja v a  2s .  c  o  m
 * 
 * @param sheet 
 * @param rangeAddress 
 */
public static void clearCell(Sheet sheet, CellRangeAddress rangeAddress) {
    int fromRowIndex = rangeAddress.getFirstRow();
    int fromColumnIndex = rangeAddress.getFirstColumn();

    int toRowIndex = rangeAddress.getLastRow();
    int toColumnIndex = rangeAddress.getLastColumn();

    // ???
    List<Row> removeRowList = new ArrayList<Row>();
    Iterator<Row> rowIterator = sheet.rowIterator();
    while (rowIterator.hasNext()) {
        Row row = rowIterator.next();
        if (fromRowIndex <= row.getRowNum() && row.getRowNum() <= toRowIndex) {
            Set<Cell> removeCellSet = new HashSet<Cell>();
            Iterator<Cell> cellIterator = row.cellIterator();
            while (cellIterator.hasNext()) {
                Cell cell = cellIterator.next();

                if (fromColumnIndex <= cell.getColumnIndex() && cell.getColumnIndex() <= toColumnIndex) {
                    removeCellSet.add(cell);
                }
            }
            for (Cell cell : removeCellSet) {
                row.removeCell(cell);
            }
        }
        if (row.getLastCellNum() == -1) {
            removeRowList.add(row);
        }
    }
    for (Row row : removeRowList) {
        sheet.removeRow(row);
    }
}

From source file:org.bbreak.excella.reports.listener.BreakAdapter.java

License:Open Source License

private boolean isInMergedRegion(Sheet sheet, Row row, Cell cell) {
    // ???????/*from w ww  . j a v a 2s. c o  m*/
    for (int i = 0; i < sheet.getNumMergedRegions(); i++) {
        CellRangeAddress region = sheet.getMergedRegion(i);
        if (region.isInRange(row.getRowNum(), cell.getColumnIndex())) {
            return true;
        }
    }
    return false;
}

From source file:org.bbreak.excella.reports.listener.BreakAdapter.java

License:Open Source License

protected void setRowBreakMergedRegion(Sheet sheet, Row row, Cell cell) {
    PoiUtil.setCellValue(cell, null);//from  w ww.  java 2  s . c o m
    // ???????
    for (int i = 0; i < sheet.getNumMergedRegions(); i++) {
        CellRangeAddress region = sheet.getMergedRegion(i);
        if (region.isInRange(row.getRowNum(), cell.getColumnIndex())) {
            // ??????????
            sheet.setRowBreak(row.getRowNum() + 1);
            return;
        }
    }
    // ????????????
    sheet.setRowBreak(row.getRowNum());
}

From source file:org.bbreak.excella.reports.ReportsTestUtil.java

License:Open Source License

/**
 * /*from w  w w . j  a  v  a 2 s  .co m*/
 * 
 * @param expected 
 * @param actual 
 * @throws ReportsCheckException 
 */
public static void checkCell(Cell expected, Cell actual) throws ReportsCheckException {

    List<CheckMessage> errors = new ArrayList<CheckMessage>();

    // ----------------------
    // ????
    // ----------------------

    if (expected == null && actual == null) {
        return;
    }

    if (expected == null) {
        // if(actual.getCellStyle() != null || actual.getCellType() != Cell.CELL_TYPE_BLANK){
        errors.add(new CheckMessage("(" + actual.getRowIndex() + "," + actual.getColumnIndex() + ")",
                null, actual.toString()));
        throw new ReportsCheckException(errors);
        // }
    }
    if (actual == null) {
        errors.add(new CheckMessage("(" + expected.getRowIndex() + "," + expected.getColumnIndex() + ")",
                expected.toString(), null));
        throw new ReportsCheckException(errors);
    }

    // 
    if (expected.getCellTypeEnum() != actual.getCellTypeEnum()) {
        errors.add(new CheckMessage(
                "[" + "(" + expected.getRowIndex() + "," + expected.getColumnIndex() + ")" + "]",
                getCellTypeString(expected.getCellTypeEnum()), getCellTypeString(actual.getCellTypeEnum())));
        throw new ReportsCheckException(errors);
    }

    try {
        checkCellStyle(expected.getRow().getSheet().getWorkbook(), expected.getCellStyle(),
                actual.getRow().getSheet().getWorkbook(), actual.getCellStyle());
    } catch (ReportsCheckException e) {
        CheckMessage checkMessage = e.getCheckMessages().iterator().next();
        checkMessage.setMessage("(" + expected.getRowIndex() + "," + expected.getColumnIndex() + ")"
                + checkMessage.getMessage());
        errors.add(checkMessage);
        throw new ReportsCheckException(errors);
    }

    // 
    if (!getCellValue(expected).equals(getCellValue(actual))) {
        log.error(getCellValue(expected) + " / " + getCellValue(actual));
        errors.add(new CheckMessage(
                "[" + "(" + expected.getRowIndex() + "," + expected.getColumnIndex() + ")" + "]",
                String.valueOf(getCellValue(expected)), String.valueOf(getCellValue(actual))));
        throw new ReportsCheckException(errors);
    }

}

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 {//from w  w  w  .ja v  a 2 s.  c  o 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.reports.tag.BlockRowRepeatParamParser.java

License:Open Source License

@Override
public ParsedReportInfo parse(Sheet sheet, Cell tagCell, Object data) throws ParseException {
    try {/*from  w  w  w.  j  a va2 s.  com*/
        // ??
        Map<String, String> paramDef = TagUtil.getParams(tagCell.getStringCellValue());

        // ?
        checkParam(sheet, paramDef, tagCell);

        ReportsParserInfo reportsParserInfo = (ReportsParserInfo) data;
        ParamInfo paramInfo = reportsParserInfo.getParamInfo();

        // parse?
        ParsedReportInfo parsedReportInfo = new ParsedReportInfo();
        List<Object> resultList = new ArrayList<Object>();

        // ?
        int finalBlockRowIndex = 0;
        int finalBlockColIndex = 0;

        String brTagName = paramDef.get(PARAM_VALUE);
        if (log.isDebugEnabled()) {
            log.debug("BR??: " + brTagName);
        }

        // ?????
        Object[] paramInfos = getParamData(paramInfo, brTagName);
        if (paramInfos == null) {
            return parsedReportInfo;
        }

        // ????
        List<SingleParamParser> singleParsers = getSingleReplaceParsers(reportsParserInfo);

        // 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()]);

        // repeatNum
        Integer repeatNum = paramInfos.length;
        if (paramDef.containsKey(PARAM_REPEAT_NUM)) {
            if (Integer.valueOf(paramDef.get(PARAM_REPEAT_NUM)) < repeatNum) {
                repeatNum = Integer.valueOf(paramDef.get(PARAM_REPEAT_NUM));
            }
        }

        // duplicateParams  ????????
        Map<String, Object> unduplicableParamMap = new HashMap<String, Object>();
        if (paramDef.containsKey(PARAM_DUPLICATE)) {
            String[] params = paramDef.get(PARAM_DUPLICATE).split(";");
            for (String param : params) {
                for (ReportsTagParser<?> parser : singleParsers) {
                    param = parser.getTag() + TAG_PARAM_PREFIX + param + TAG_PARAM_SUFFIX;
                    unduplicableParamMap.put(param, "");
                }
            }
        }

        // removeTag
        boolean removeTag = false;
        if (paramDef.containsKey(PARAM_REMOVE_TAG)) {
            removeTag = Boolean.valueOf(paramDef.get(PARAM_REMOVE_TAG));
        }

        // 
        int tagCellRowIndex = tagCell.getRowIndex();
        int tagCellColIndex = tagCell.getColumnIndex();

        // fromCell
        String fromCellParamDef = paramDef.get(PARAM_FROM_CELL);
        int[] fromCellPosition = ReportsUtil.getCellIndex(fromCellParamDef, PARAM_FROM_CELL);
        int defaultFromCellRowIndex = tagCellRowIndex + fromCellPosition[0];
        int defaultFromCellColIndex = tagCellColIndex + fromCellPosition[1];

        // toCell
        String toCellParamDef = paramDef.get(PARAM_TO_CELL);
        int[] toCellPosition = ReportsUtil.getCellIndex(toCellParamDef, PARAM_TO_CELL);
        int defaultToCellRowIndex = tagCellRowIndex + toCellPosition[0];
        int defaultToCellColIndex = tagCellColIndex + toCellPosition[1];

        // 
        Object[][] blockCellValues = ReportsUtil.getBlockCellValue(sheet, defaultFromCellRowIndex,
                defaultToCellRowIndex, defaultFromCellColIndex, defaultToCellColIndex);
        CellStyle[][] blockCellStyles = ReportsUtil.getBlockCellStyle(sheet, defaultFromCellRowIndex,
                defaultToCellRowIndex, defaultFromCellColIndex, defaultToCellColIndex);
        CellType[][] blockCellTypes = ReportsUtil.getBlockCellType(sheet, defaultFromCellRowIndex,
                defaultToCellRowIndex, defaultFromCellColIndex, defaultToCellColIndex);
        float[] rowHeight = ReportsUtil.getRowHeight(sheet, defaultFromCellRowIndex, defaultToCellRowIndex);

        // ???
        CellRangeAddress[] margedCells = ReportsUtil.getMargedCells(sheet, defaultFromCellRowIndex,
                defaultToCellRowIndex, defaultFromCellColIndex, defaultToCellColIndex);

        // ?
        final int defaultBlockHeight = defaultToCellRowIndex - defaultFromCellRowIndex + 1;
        final int defaultBlockWidth = defaultToCellColIndex - defaultFromCellColIndex + 1;
        int rowlen = defaultBlockHeight;
        int collen = defaultBlockWidth;

        // ??(fromCell??, 1?????)
        int blockStartRowIndex = defaultFromCellRowIndex;
        int blockStartColIndex = defaultFromCellColIndex;
        int blockEndRowIndex = defaultToCellRowIndex;
        int blockEndColIndex = defaultToCellColIndex;
        int maxblockEndRowIndex = blockEndRowIndex;

        parsedReportInfo.setDefaultRowIndex(defaultToCellRowIndex);
        parsedReportInfo.setDefaultColumnIndex(defaultToCellColIndex);

        for (int repeatCount = 0; repeatCount < repeatNum; repeatCount++) {
            // ????
            if (repeatCount > 0) {
                blockStartRowIndex = blockEndRowIndex + 1;
                blockEndRowIndex = blockStartRowIndex + rowlen - 1;

                CellRangeAddress rangeAddress = new CellRangeAddress(blockStartRowIndex, blockEndRowIndex,
                        blockStartColIndex, PoiUtil.getLastColNum(sheet));
                PoiUtil.insertRangeDown(sheet, rangeAddress);

                if (log.isDebugEnabled()) {
                    log.debug("");
                    log.debug(" : " + blockStartRowIndex + ":" + (blockStartRowIndex + rowlen - 1)
                            + ":" + blockStartColIndex + ":" + PoiUtil.getLastColNum(sheet));
                }

                // ???
                // ???????????
                // ??????
                int targetRowNum = maxblockEndRowIndex - (defaultFromCellRowIndex - 1);
                for (CellRangeAddress address : margedCells) {
                    // ??? + BR?? - BR?
                    // ??? + BR?? - BR?
                    // ???(BR????????)
                    // ???(BR????????)
                    int firstRowNum = address.getFirstRow() + targetRowNum;
                    int lastRowNum = address.getLastRow() + targetRowNum;
                    int firstColumnNum = address.getFirstColumn();
                    int lastColumnNum = address.getLastColumn();

                    CellRangeAddress copyAddress = new CellRangeAddress(firstRowNum, lastRowNum, firstColumnNum,
                            lastColumnNum);
                    sheet.addMergedRegion(copyAddress);
                }

            }

            if (log.isDebugEnabled()) {
                log.debug("repeatCount = " + repeatCount);
                log.debug("blockStartRowIndex = " + blockStartRowIndex);
                log.debug("blockStartColIndex = " + blockStartColIndex);
            }

            // ??
            if (log.isDebugEnabled()) {
                log.debug("?????? =" + repeatCount);
            }

            for (int rowIdx = 0; rowIdx < defaultBlockHeight; rowIdx++) {
                // ?
                Row row = sheet.getRow(blockStartRowIndex + rowIdx);
                // ??null???????????????
                if (row == null && !ReportsUtil.isEmptyRow(blockCellTypes[rowIdx], blockCellValues[rowIdx],
                        blockCellStyles[rowIdx])) {
                    row = sheet.createRow(blockStartRowIndex + rowIdx);
                }

                if (row != null) {
                    // ???
                    row.setHeightInPoints(rowHeight[rowIdx]);

                    // ?
                    for (int colIdx = 0; colIdx < defaultBlockWidth; colIdx++) {
                        // ?
                        Cell cell = row.getCell(blockStartColIndex + colIdx);
                        // ??
                        CellType cellType = blockCellTypes[rowIdx][colIdx];
                        // ??
                        Object cellValue = blockCellValues[rowIdx][colIdx];
                        // ??
                        CellStyle cellStyle = blockCellStyles[rowIdx][colIdx];
                        // ?????????????????
                        if (cell == null && !ReportsUtil.isEmptyCell(cellType, cellValue, cellStyle)) {
                            cell = row.createCell(blockStartColIndex + colIdx);
                        }

                        // 
                        if (cell != null) {
                            // ?
                            cell.setCellType(cellType);
                            // ??
                            PoiUtil.setCellValue(cell, cellValue);
                            // ??
                            if (cellStyle == null) {
                                log.info("Cell Style at [" + rowIdx + "," + colIdx
                                        + "] is not available. Skipping setCellValue()");
                            } else {
                                cell.setCellStyle(cellStyle);
                            }
                            log.debug("row=" + (blockStartRowIndex + rowIdx) + " col"
                                    + (blockStartColIndex + colIdx) + ">>>>>>"
                                    + blockCellValues[rowIdx][colIdx]);
                        }
                    }
                }
            }

            int currentBlockHeight = rowlen;
            int currentBlockWidth = collen;
            // ?????????
            int plusRowNum = 0;
            // ?????????
            int plusColNum = 0;
            collen = defaultBlockWidth;
            // 
            // ???
            for (int targetRow = blockStartRowIndex; targetRow < blockStartRowIndex + rowlen
                    + plusRowNum; targetRow++) {
                if (finalBlockRowIndex < targetRow) {
                    finalBlockRowIndex = targetRow;
                }
                if (sheet.getRow(targetRow) == null) {
                    if (log.isDebugEnabled()) {
                        log.debug("row=" + targetRow + " : row is not available. continued...");
                    }
                    continue;
                }

                for (int targetCol = blockStartColIndex; targetCol <= blockStartColIndex + collen + plusColNum
                        - 1; targetCol++) {
                    if (finalBlockColIndex < targetCol) {
                        finalBlockColIndex = targetCol;
                    }
                    Cell targetCell = sheet.getRow(targetRow).getCell(targetCol);
                    if (targetCell == null) {
                        if (log.isDebugEnabled()) {
                            log.debug("row=" + targetRow + " col=" + targetCol
                                    + " : cell is not available. continued...");
                        }
                        continue;
                    }

                    // ??
                    TagParser<?> parser = reportsParserInfo.getMatchTagParser(sheet, targetCell);
                    if (parser == null) {
                        if (log.isDebugEnabled()) {
                            log.debug("row=" + targetRow + " col=" + targetCol
                                    + " parser is not available. continued...");
                        }
                        continue;
                    }

                    String targetCellTag = targetCell.getStringCellValue();
                    if (log.isDebugEnabled()) {
                        log.debug("##########  row=" + targetRow + " col=" + targetCol
                                + " =" + targetCellTag + " ##########");
                    }

                    // 
                    ParsedReportInfo result = (ParsedReportInfo) parser.parse(sheet, targetCell,
                            reportsParserInfo.createChildParserInfo((ParamInfo) paramInfos[repeatCount]));
                    resultList.add(result.getParsedObject());

                    // ???
                    plusRowNum += result.getRowIndex() - result.getDefaultRowIndex();

                    // ???
                    plusColNum += result.getColumnIndex() - result.getDefaultColumnIndex();

                    int additionalHeight = result.getRowIndex() - result.getDefaultRowIndex();
                    int additionalWidth = result.getColumnIndex() - result.getDefaultColumnIndex();

                    // ??????????
                    currentBlockHeight = currentBlockHeight + additionalHeight;
                    currentBlockWidth = currentBlockWidth + additionalWidth;

                    // ???
                    if (parser instanceof SingleParamParser) {
                        if (unduplicableParamMap.containsKey(targetCellTag)) {
                            if (unduplicableParamMap.get(targetCellTag).equals(result.getParsedObject())) {
                                PoiUtil.setCellValue(targetCell, "");

                            } else {
                                unduplicableParamMap.put(targetCellTag, result.getParsedObject());
                            }
                        }
                    }

                    // ??????????????
                    if (defaultFromCellColIndex != result.getDefaultColumnIndex()
                            && result.getRowIndex() > result.getDefaultRowIndex()
                            && blockStartColIndex < targetCol) {
                        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
                            && result.getRowIndex() > result.getDefaultRowIndex()) {
                        CellRangeAddress rearRangeAddress = new CellRangeAddress(blockEndRowIndex + 1,
                                blockEndRowIndex + (result.getRowIndex() - result.getDefaultRowIndex()),
                                result.getDefaultColumnIndex() + 1, PoiUtil.getLastColNum(sheet));
                        PoiUtil.insertRangeDown(sheet, rearRangeAddress);
                        if (log.isDebugEnabled()) {
                            log.debug("******");
                            log.debug("2 : " + (blockEndRowIndex + 1) + ":"
                                    + (blockEndRowIndex + (result.getRowIndex() - result.getDefaultRowIndex()))
                                    + ":" + (result.getDefaultColumnIndex() + 1) + ":"
                                    + PoiUtil.getLastColNum(sheet));
                        }
                    }

                    blockEndRowIndex += result.getRowIndex() - result.getDefaultRowIndex();

                    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);
                        }
                    }

                    // ?
                }
                // ??????
                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;

                // ?
            }
            // ???????
            int lastColNum = PoiUtil.getLastColNum(sheet);
            if ((maxblockEndRowIndex + 1) <= blockEndRowIndex && (blockEndColIndex + 1) <= lastColNum) {
                CellRangeAddress rearRangeAddress = new CellRangeAddress(maxblockEndRowIndex + 1,
                        blockEndRowIndex, blockEndColIndex + 1, lastColNum);
                PoiUtil.insertRangeDown(sheet, rearRangeAddress);
                if (log.isDebugEnabled()) {
                    log.debug("2 : " + (maxblockEndRowIndex + 1) + ":" + blockEndRowIndex + ":"
                            + (blockEndColIndex + 1) + ":" + lastColNum);
                }
            }
            maxblockEndRowIndex = blockEndRowIndex;
        }

        // 
        if (removeTag) {
            tagCell.setCellType(CellType.BLANK);
        }

        // ??
        parsedReportInfo.setColumnIndex(finalBlockColIndex);
        parsedReportInfo.setRowIndex(finalBlockRowIndex);
        parsedReportInfo.setParsedObject(resultList);
        parsedReportInfo.setDefaultRowIndex(defaultToCellRowIndex);
        parsedReportInfo.setDefaultColumnIndex(defaultToCellColIndex);

        if (log.isDebugEnabled()) {
            log.debug("finalBlockRowIndex= " + finalBlockRowIndex);
            log.debug("finalBlockColIndex=" + finalBlockColIndex);
        }

        return parsedReportInfo;

    } catch (Exception e) {
        throw new ParseException(tagCell, e);
    }

}

From source file:org.bbreak.excella.reports.tag.BreakParamParser.java

License:Open Source License

/**
 * @see org.bbreak.excella.reports.tag.ReportsTagParser#parse(org.apache.poi.ss.usermodel.Sheet, org.apache.poi.ss.usermodel.Cell, java.lang.Object)
 *///from  ww  w. j a  va2 s. c  o  m
@Override
public ParsedReportInfo parse(Sheet sheet, Cell tagCell, Object data) throws ParseException {
    // ??????
    ParsedReportInfo parsedReportInfo = new ParsedReportInfo();
    parsedReportInfo.setRowIndex(tagCell.getRowIndex());
    parsedReportInfo.setColumnIndex(tagCell.getColumnIndex());
    parsedReportInfo.setDefaultRowIndex(tagCell.getRowIndex());
    parsedReportInfo.setDefaultColumnIndex(tagCell.getColumnIndex());
    return parsedReportInfo;
}

From source file:org.bbreak.excella.reports.tag.ColRepeatParamParser.java

License:Open Source License

@Override
public ParsedReportInfo parse(Sheet sheet, Cell tagCell, Object data) throws ParseException {

    Map<String, String> paramDef = TagUtil.getParams(tagCell.getStringCellValue());

    // ?/*from   w ww . ja  v  a2 s . c om*/
    checkParam(paramDef, tagCell);

    String tag = tagCell.getStringCellValue();
    ReportsParserInfo info = (ReportsParserInfo) data;
    ParamInfo paramInfo = info.getParamInfo();
    ParsedReportInfo parsedReportInfo = new ParsedReportInfo();

    // ??
    Object[] paramValues = null;
    try {
        // ???
        String replaceParam = paramDef.get(PARAM_VALUE);

        // ?
        Integer repeatNum = null;
        if (paramDef.containsKey(PARAM_REPEAT_NUM)) {
            repeatNum = Integer.valueOf(paramDef.get(PARAM_REPEAT_NUM));
        }
        // ??
        Integer minRepeatNum = null;
        if (paramDef.containsKey(PARAM_MIN_REPEAT_NUM)) {
            minRepeatNum = Integer.valueOf(paramDef.get(PARAM_MIN_REPEAT_NUM));
        }

        // ?
        boolean sheetLink = false;
        if (paramDef.containsKey(PARAM_SHEET_LINK)) {
            sheetLink = Boolean.valueOf(paramDef.get(PARAM_SHEET_LINK));
        }

        // 
        String propertyName = null;
        if (paramDef.containsKey(PARAM_PROPERTY)) {
            propertyName = paramDef.get(PARAM_PROPERTY);
        }

        // ???
        boolean hideDuplicate = false;
        if (paramDef.containsKey(PARAM_DUPLICATE)) {
            hideDuplicate = Boolean.valueOf(paramDef.get(PARAM_DUPLICATE));
        }

        // 
        if (ReportsUtil.VALUE_SHEET_NAMES.equals(replaceParam)) {
            // ??
            paramValues = ReportsUtil.getSheetNames(info.getReportBook()).toArray();
        } else if (ReportsUtil.VALUE_SHEET_VALUES.equals(replaceParam)) {
            // 
            paramValues = ReportsUtil
                    .getSheetValues(info.getReportBook(), propertyName, info.getReportParsers()).toArray();
        } else {
            // ???
            if (paramInfo != null) {
                paramValues = getParamData(paramInfo, replaceParam);
            }
        }

        if (paramValues == null || paramValues.length == 0) {
            // ?
            paramValues = new Object[] { null };
        }

        // ?
        if (hideDuplicate && paramValues.length > 1) {
            List<Object> paramValuesList = new ArrayList<Object>();
            for (int i = 0; i <= paramValues.length - 1; i++) {
                // ?????
                if (!paramValuesList.contains(paramValues[i])) {
                    paramValuesList.add(paramValues[i]);
                } else {
                    paramValuesList.add(null);
                }
            }
            paramValues = paramValuesList.toArray();
        }

        // ?
        int shiftNum = paramValues.length;
        // ?
        int paramLength = paramValues.length;

        // ???????
        if (minRepeatNum != null && shiftNum < minRepeatNum) {
            Object[] tmpValues = new Object[minRepeatNum];
            System.arraycopy(paramValues, 0, tmpValues, 0, paramValues.length);
            paramValues = tmpValues;
            shiftNum = paramValues.length;
            paramLength = paramValues.length;
        }

        // ???
        int defaultFromCellRowIndex = tagCell.getRowIndex();
        // ???
        int defaultFromCellColIndex = tagCell.getColumnIndex();

        // ??
        int unitColSize = 1;

        // ???
        List<CellRangeAddress> maegedAddresses = new ArrayList<CellRangeAddress>();
        for (int i = 0; i < sheet.getNumMergedRegions(); i++) {
            CellRangeAddress targetAddress = sheet.getMergedRegion(i);
            maegedAddresses.add(targetAddress);
        }

        // ???
        if (maegedAddresses.size() > 0) {
            // ?????????????
            for (CellRangeAddress curMergedAdress : maegedAddresses) {
                if (defaultFromCellColIndex == curMergedAdress.getFirstColumn()
                        && defaultFromCellRowIndex == curMergedAdress.getFirstRow()) {
                    // ????????????
                    // ??????
                    unitColSize = curMergedAdress.getLastColumn() - curMergedAdress.getFirstColumn() + 1;

                    // ??????
                    shiftNum = shiftNum * unitColSize;
                }
            }
        }

        // ?
        if (repeatNum != null && repeatNum < shiftNum) {
            // ??????
            // ????????????????
            // ?(repeatNum)??(unitColSize)??
            shiftNum = repeatNum * unitColSize;

            // ??????
            paramLength = repeatNum;
        }

        // ???
        // ?????
        tagCell = new CellClone(tagCell);
        List<Cell> cellList = new ArrayList<Cell>();
        int defaultToOverCellColIndex = tagCell.getColumnIndex() + unitColSize;
        for (int i = defaultFromCellColIndex; i < defaultToOverCellColIndex; i++) {
            Row targetCellRow = sheet.getRow(tagCell.getRowIndex());
            cellList.add(new CellClone(targetCellRow.getCell(i)));
        }

        // ?            
        if (shiftNum > 1) {
            // ?(????????)
            int shiftColSize = tagCell.getColumnIndex() + shiftNum - unitColSize - 1;
            // ???
            CellRangeAddress rangeAddress = new CellRangeAddress(tagCell.getRowIndex(), tagCell.getRowIndex(),
                    tagCell.getColumnIndex(), shiftColSize);
            PoiUtil.insertRangeRight(sheet, rangeAddress);
            // 
            int tagCellWidth = sheet.getColumnWidth(tagCell.getColumnIndex());
            for (int i = tagCell.getColumnIndex() + 1; i <= shiftColSize; i++) {
                int colWidth = sheet.getColumnWidth(i);
                if (colWidth < tagCellWidth) {
                    // ??  ???????
                    // ??????
                    sheet.setColumnWidth(i, tagCellWidth);
                }
            }
        }

        // ???
        Workbook workbook = sheet.getWorkbook();
        String sheetName = workbook.getSheetName(workbook.getSheetIndex(sheet));
        // ??
        List<String> sheetNames = ReportsUtil.getSheetNames(info.getReportBook());
        // ?
        List<Object> resultValues = new ArrayList<Object>();
        // ??(beforeValue)
        Object beforeValue = null;

        // ?
        int valueIndex = -1;
        // ?????
        for (int colIndex = 0; colIndex < shiftNum; colIndex++) {
            // ??
            Row row = sheet.getRow(tagCell.getRowIndex());
            if (row == null) {
                // ????
                // ?
                // (??)??????????
                // ??null???(RowCreate?)???????????
                row = sheet.createRow(tagCell.getRowIndex());
            }
            // ??
            Cell cell = row.getCell(tagCell.getColumnIndex() + colIndex);
            if (cell == null) {
                cell = row.createCell(tagCell.getColumnIndex() + colIndex);
            }
            // ????(null)
            Object value = null;

            // ??????
            // ??0???(?????????)???????
            int cellIndex = colIndex % unitColSize;

            // ?????????
            boolean skipCol = false;
            if (cellIndex != 0) {
                skipCol = true;
            } else {
                valueIndex++;
            }

            // 
            // ?
            PoiUtil.copyCell(cellList.get(cellIndex), cell);

            // ?
            Object currentValue = paramValues[valueIndex];
            // ??=true???????????
            boolean duplicateValue = false;
            if (beforeValue != null && currentValue != null && beforeValue.equals(currentValue)) {
                // ???
                duplicateValue = true;
            }
            if (!skipCol && !(hideDuplicate && duplicateValue)) {
                // ??=true
                // ??????????????
                value = currentValue;
            }
            if (log.isDebugEnabled()) {
                log.debug("[??=" + sheetName + ",=(" + cell.getRowIndex() + ","
                        + cell.getColumnIndex() + ")]  " + tag + "  " + value);
            }
            PoiUtil.setCellValue(cell, value);
            resultValues.add(value);

            // ?
            if (sheetLink) {
                if (!skipCol && valueIndex < sheetNames.size()) {
                    PoiUtil.setHyperlink(cell, HyperlinkType.DOCUMENT,
                            "'" + sheetNames.get(valueIndex) + "'!A1");
                    if (log.isDebugEnabled()) {
                        log.debug("[??=" + sheetName + ",=(" + cell.getRowIndex() + ","
                                + cell.getColumnIndex() + ")]  Hyperlink  " + "'"
                                + sheetNames.get(valueIndex) + "'!A1");
                    }
                }
            }

            // ??
            // ??????????????????
            if (!skipCol && unitColSize > 1 && paramLength > valueIndex + 1) {
                CellRangeAddress rangeAddress = new CellRangeAddress(cell.getRowIndex(), cell.getRowIndex(),
                        cell.getColumnIndex(), cell.getColumnIndex() + unitColSize - 1);
                sheet.addMergedRegion(rangeAddress);

                // ????????????
                beforeValue = value;
            }

            // ???????
            if (unitColSize == 1) {
                beforeValue = value;
            }

        }

        parsedReportInfo.setDefaultRowIndex(tagCell.getRowIndex());
        // ??
        parsedReportInfo.setDefaultColumnIndex(tagCell.getColumnIndex() + unitColSize - 1);
        parsedReportInfo.setRowIndex(tagCell.getRowIndex());
        parsedReportInfo.setColumnIndex(tagCell.getColumnIndex() + shiftNum - 1);
        parsedReportInfo.setParsedObject(resultValues);
        if (log.isDebugEnabled()) {
            log.debug(parsedReportInfo);
        }
        return parsedReportInfo;

    } catch (Exception e) {
        throw new ParseException(tagCell, e);
    }

}

From source file:org.bbreak.excella.reports.tag.ImageParamParser.java

License:Open Source License

@Override
public ParsedReportInfo parse(Sheet sheet, Cell tagCell, Object data) throws ParseException {

    Map<String, String> paramDef = TagUtil.getParams(tagCell.getStringCellValue());

    // ?//from  ww  w.ja  va2s.  c  o  m
    if (hasComments(sheet)) {
        throw new ParseException(
                "?[" + sheet.getWorkbook().getSheetName(sheet.getWorkbook().getSheetIndex(sheet))
                        + "]");
    }

    // ?
    checkParam(paramDef, tagCell);

    ReportsParserInfo reportsParserInfo = (ReportsParserInfo) data;

    ParamInfo paramInfo = reportsParserInfo.getParamInfo();

    String paramValue = null;

    if (paramInfo != null) {
        // ?
        String replaceParam = paramDef.get(PARAM_VALUE);

        paramValue = getParamData(paramInfo, replaceParam);

        // ??
        Integer dx1 = null;
        if (paramDef.containsKey(PARAM_WIDTH)) {
            dx1 = Integer.valueOf(paramDef.get(PARAM_WIDTH));
        }

        // ???
        Integer dy1 = null;
        if (paramDef.containsKey(PARAM_HEIGHT)) {
            dy1 = Integer.valueOf(paramDef.get(PARAM_HEIGHT));
        }

        // ???
        Double scale = 1.0;
        if (paramDef.containsKey(PARAM_SCALE)) {
            scale = Double.valueOf(paramDef.get(PARAM_SCALE));
        }

        // ??????
        if (ReportsUtil.getMergedAddress(sheet, tagCell.getRowIndex(), tagCell.getColumnIndex()) != null) {
            CellStyle cellStyle = tagCell.getCellStyle();
            tagCell.setCellType(CellType.BLANK);
            tagCell.setCellStyle(cellStyle);
        } else {
            tagCell = new CellClone(tagCell);
            PoiUtil.clearCell(sheet, new CellRangeAddress(tagCell.getRowIndex(), tagCell.getRowIndex(),
                    tagCell.getColumnIndex(), tagCell.getColumnIndex()));
        }

        if (log.isDebugEnabled()) {
            Workbook workbook = sheet.getWorkbook();
            String sheetName = workbook.getSheetName(workbook.getSheetIndex(sheet));

            log.debug("[??=" + sheetName + ",=(" + tagCell.getRowIndex() + ","
                    + tagCell.getColumnIndex() + ")]  " + tagCell.getStringCellValue() + "  " + paramValue);
        }

        if (paramValue != null) {
            replaceImageValue(sheet, tagCell, paramValue, dx1, dy1, scale);
        }

    }

    // ????
    ParsedReportInfo parsedReportInfo = new ParsedReportInfo();
    parsedReportInfo.setParsedObject(paramValue);
    parsedReportInfo.setRowIndex(tagCell.getRowIndex());
    parsedReportInfo.setColumnIndex(tagCell.getColumnIndex());
    parsedReportInfo.setDefaultRowIndex(tagCell.getRowIndex());
    parsedReportInfo.setDefaultColumnIndex(tagCell.getColumnIndex());

    return parsedReportInfo;
}