Example usage for org.apache.poi.ss.usermodel Workbook getSheetIndex

List of usage examples for org.apache.poi.ss.usermodel Workbook getSheetIndex

Introduction

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

Prototype

int getSheetIndex(Sheet sheet);

Source Link

Document

Returns the index of the given sheet

Usage

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

License:Open Source License

/**
 * ????/*w ww  .j a va  2  s .  c o  m*/
 * 
 * @param sheet 
 * @return ??
 */
public static String getSheetName(Sheet sheet) {
    Workbook workbook = sheet.getWorkbook();
    int sheetIndex = workbook.getSheetIndex(sheet);
    return workbook.getSheetName(sheetIndex);
}

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

License:Open Source License

/**
 * ?// w  ww  . ja v  a2 s .c  om
 * 
 * @param fromSheet 
 * @param rangeAddress 
 * @param toSheet 
 * @param toRowNum 
 * @param toColumnNum 
 * @param clearFromRange 
 */
public static void copyRange(Sheet fromSheet, CellRangeAddress rangeAddress, Sheet toSheet, int toRowNum,
        int toColumnNum, boolean clearFromRange) {

    if (fromSheet == null || rangeAddress == null || toSheet == null) {
        return;
    }

    int fromRowIndex = rangeAddress.getFirstRow();
    int fromColumnIndex = rangeAddress.getFirstColumn();

    int rowNumOffset = toRowNum - fromRowIndex;
    int columnNumOffset = toColumnNum - fromColumnIndex;

    // 
    CellRangeAddress toAddress = new CellRangeAddress(rangeAddress.getFirstRow() + rowNumOffset,
            rangeAddress.getLastRow() + rowNumOffset, rangeAddress.getFirstColumn() + columnNumOffset,
            rangeAddress.getLastColumn() + columnNumOffset);

    Workbook fromWorkbook = fromSheet.getWorkbook();
    Sheet baseSheet = fromSheet;

    Sheet tmpSheet = null;
    // ?????
    if (fromSheet.equals(toSheet) && crossRangeAddress(rangeAddress, toAddress)) {
        // ?
        tmpSheet = fromWorkbook.getSheet(TMP_SHEET_NAME);
        if (tmpSheet == null) {
            tmpSheet = fromWorkbook.createSheet(TMP_SHEET_NAME);
        }
        baseSheet = tmpSheet;

        int lastColNum = getLastColNum(fromSheet);
        for (int i = 0; i <= lastColNum; i++) {
            tmpSheet.setColumnWidth(i, fromSheet.getColumnWidth(i));
        }

        copyRange(fromSheet, rangeAddress, tmpSheet, rangeAddress.getFirstRow(), rangeAddress.getFirstColumn(),
                false);

        // ?
        if (clearFromRange) {
            clearRange(fromSheet, rangeAddress);
        }
    }

    // ????
    Set<CellRangeAddress> targetCellSet = getMergedAddress(baseSheet, rangeAddress);
    // ???
    clearRange(toSheet, toAddress);

    // ???
    for (CellRangeAddress mergeAddress : targetCellSet) {

        toSheet.addMergedRegion(new CellRangeAddress(mergeAddress.getFirstRow() + rowNumOffset,
                mergeAddress.getLastRow() + rowNumOffset, mergeAddress.getFirstColumn() + columnNumOffset,
                mergeAddress.getLastColumn() + columnNumOffset));

    }

    for (int i = rangeAddress.getFirstRow(); i <= rangeAddress.getLastRow(); i++) {
        // 
        Row fromRow = baseSheet.getRow(i);
        if (fromRow == null) {
            continue;
        }
        Row row = toSheet.getRow(i + rowNumOffset);
        if (row == null) {
            row = toSheet.createRow(i + rowNumOffset);
            row.setHeight((short) 0);
        }

        // ??????
        int fromRowHeight = fromRow.getHeight();
        int toRowHeight = row.getHeight();
        if (toRowHeight < fromRowHeight) {
            row.setHeight(fromRow.getHeight());
        }

        ColumnHelper columnHelper = null;
        if (toSheet instanceof XSSFSheet) {
            XSSFSheet xssfSheet = (XSSFSheet) toSheet.getWorkbook()
                    .getSheetAt(toSheet.getWorkbook().getSheetIndex(toSheet));
            CTWorksheet ctWorksheet = xssfSheet.getCTWorksheet();
            columnHelper = new ColumnHelper(ctWorksheet);
        }

        for (int j = rangeAddress.getFirstColumn(); j <= rangeAddress.getLastColumn(); j++) {
            Cell fromCell = fromRow.getCell(j);
            if (fromCell == null) {
                continue;
            }
            int maxColumn = SpreadsheetVersion.EXCEL97.getMaxColumns();
            if (toSheet instanceof XSSFSheet) {
                maxColumn = SpreadsheetVersion.EXCEL2007.getMaxColumns();
            }
            if (j + columnNumOffset >= maxColumn) {
                break;
            }
            Cell cell = row.getCell(j + columnNumOffset);
            if (cell == null) {
                cell = row.createCell(j + columnNumOffset);
                if (toSheet instanceof XSSFSheet) {
                    // XSSF??????????
                    CTCol col = columnHelper.getColumn(cell.getColumnIndex(), false);
                    if (col == null || !col.isSetWidth()) {
                        toSheet.setColumnWidth(cell.getColumnIndex(), baseSheet.getColumnWidth(j));
                    }
                }
            }

            // ?
            copyCell(fromCell, cell);

            // ??????
            int fromColumnWidth = baseSheet.getColumnWidth(j);
            int toColumnWidth = toSheet.getColumnWidth(j + columnNumOffset);

            if (toColumnWidth < fromColumnWidth) {
                toSheet.setColumnWidth(j + columnNumOffset, baseSheet.getColumnWidth(j));
            }
        }
    }

    if (tmpSheet != null) {
        // 
        fromWorkbook.removeSheetAt(fromWorkbook.getSheetIndex(tmpSheet));
    } else if (clearFromRange) {
        // ????
        clearRange(fromSheet, rangeAddress);
    }

}

From source file:org.bbreak.excella.reports.processor.ReportProcessor.java

License:Open Source License

/**
 * ????/*from  w  ww  . j ava  2 s .  c o  m*/
 *
 * @param reportBook ??
 * @throws IOException ???????
 * @throws ParseException ??????
 * @throws ExportException ?????
 */
private void processBook(ReportBook reportBook) throws Exception {

    if (reportBook == null) {
        return;
    }

    Workbook workbook = getTemplateWorkbook(reportBook);

    for (ReportProcessListener listener : listeners) {
        listener.preBookParse(workbook, reportBook);
    }

    checkReportBook(reportBook);

    // 
    Set<String> delSheetNames = expandTemplate(workbook, reportBook);

    // ?
    BookController controller = new BookController(workbook);

    // Parser?
    for (ReportsTagParser<?> tagParser : parsers.values()) {
        controller.addTagParser(tagParser);
    }
    // ?
    controller.addSheetParseListener(new RemoveAdapter());
    controller.addSheetParseListener(new BreakAdapter());
    for (ReportProcessListener listener : listeners) {
        controller.addSheetParseListener(listener);
    }

    // Exporter?
    for (ConvertConfiguration configuration : reportBook.getConfigurations()) {
        if (configuration == null) {
            continue;
        }
        for (ReportBookExporter reportExporter : exporters.values()) {
            if (configuration.getFormatType().equals(reportExporter.getFormatType())) {
                reportExporter.setConfiguration(configuration);
                reportExporter.setFilePath(reportBook.getOutputFileName() + reportExporter.getExtention());
                controller.addBookExporter(reportExporter);
            }
        }
    }

    ReportsParserInfo reportsParserInfo = new ReportsParserInfo();
    reportsParserInfo.setReportParsers(new ArrayList<ReportsTagParser<?>>(parsers.values()));
    reportsParserInfo.setReportBook(reportBook);

    BookData bookData = controller.getBookData();
    bookData.clear();
    for (String sheetName : controller.getSheetNames()) {
        if (sheetName.startsWith(BookController.COMMENT_PREFIX)) {
            continue;
        }
        ReportSheet reportSheet = ReportsUtil.getReportSheet(sheetName, reportBook);
        if (reportSheet != null) {

            reportsParserInfo.setParamInfo(reportSheet.getParamInfo());

            // ??
            SheetData sheetData = controller.parseSheet(sheetName, reportsParserInfo);
            // ??
            controller.getBookData().putSheetData(sheetName, sheetData);
        }
    }

    // ???
    for (String delSheetName : delSheetNames) {
        int delSheetIndex = workbook.getSheetIndex(delSheetName);
        if (delSheetIndex != -1) {
            workbook.removeSheetAt(delSheetIndex);
        }
    }

    // ?????
    for (ReportProcessListener listener : listeners) {
        listener.postBookParse(workbook, reportBook);
    }

    // ??
    for (BookExporter exporter : controller.getExporter()) {
        if (exporter != null) {
            exporter.setup();
            try {
                exporter.export(workbook, bookData);
            } finally {
                exporter.tearDown();
            }
        }
    }
}

From source file:org.bbreak.excella.reports.processor.ReportProcessor.java

License:Open Source License

/**
 * ???????/* ww w  .  j a  v  a2 s . c o  m*/
 *
 * @param workbook 
 * @param reportBook 
 * @return ?????
 */
private Set<String> expandTemplate(Workbook workbook, ReportBook reportBook) {

    Set<String> delSheetNames = new TreeSet<String>(Collections.reverseOrder());
    Set<String> useSheetNames = new HashSet<String>();

    // ????
    for (ReportSheet reportSheet : reportBook.getReportSheets()) {

        if (reportSheet != null) {
            if (reportSheet.getSheetName().equals(reportSheet.getTemplateName())) {
                // ?????
                int lastSheetIndex = workbook.getNumberOfSheets() - 1;
                workbook.setSheetOrder(reportSheet.getSheetName(), lastSheetIndex);
                useSheetNames.add(reportSheet.getTemplateName());
            } else {
                int tempIdx = workbook.getSheetIndex(reportSheet.getTemplateName());
                Sheet sheet = workbook.cloneSheet(tempIdx);
                ReportsUtil.copyPrintSetup(workbook, tempIdx, sheet);
                workbook.setSheetName(workbook.getSheetIndex(sheet), reportSheet.getSheetName());
                delSheetNames.add(reportSheet.getTemplateName());
            }
        }
    }

    // ?
    for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
        Sheet sheet = workbook.getSheetAt(i);
        if (!isOutputSheet(sheet, reportBook)) {
            delSheetNames.add(sheet.getSheetName());
        }
    }

    delSheetNames.removeAll(useSheetNames);

    // ?????

    return delSheetNames;

}

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

License:Open Source License

/**
 * // ww w  . ja  va 2  s .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.BlockColRepeatParamParserTest.java

License:Open Source License

@Test
public void testParseSheetCellObject() throws ParseException {
    Workbook workbook = null;

    ReportBook reportBook = new ReportBook("", "test", new ConvertConfiguration[] {});
    ReportSheet reportSheet1 = new ReportSheet("sheet1", "Sheet1");
    reportBook.addReportSheet(reportSheet1);
    ReportSheet reportSheet2 = new ReportSheet("sheet1", "Sheet2");
    reportBook.addReportSheet(reportSheet2);

    ReportSheet[] reportSheets = new ReportSheet[] { reportSheet1, reportSheet2 };

    // -----------------------
    // //from ww  w.  j ava2s. com
    // -----------------------

    //?BC1
    ParamInfo inBlockInfo1 = new ParamInfo();
    inBlockInfo1.addParam(ColRepeatParamParser.DEFAULT_TAG, "A",
            new Object[] { "inBC1-C-A1", "inBC1-C-A2", "inBC1-C-A3" });
    inBlockInfo1.addParam(ColRepeatParamParser.DEFAULT_TAG, "B", new Object[] { "inBC1-C-B1", "inBC1-C-B1" });
    inBlockInfo1.addParam(ColRepeatParamParser.DEFAULT_TAG, "C", new Object[] { "inBC1-C-C1" });

    inBlockInfo1.addParam(RowRepeatParamParser.DEFAULT_TAG, "A", new Object[] { "inBC1-R-A1" });

    inBlockInfo1.addParam(SingleParamParser.DEFAULT_TAG, "D", "inBC1-S-DDD");

    //?BC2
    ParamInfo inBlockInfo2 = new ParamInfo();
    inBlockInfo2.addParam(ColRepeatParamParser.DEFAULT_TAG, "A", new Object[] { "inBC2-C-A1" });
    inBlockInfo2.addParam(ColRepeatParamParser.DEFAULT_TAG, "B", new Object[] { "inBC2-C-B1", "inBC2-C-B2" });
    inBlockInfo2.addParam(ColRepeatParamParser.DEFAULT_TAG, "C",
            new Object[] { "inBC2-C-C1", "inBC2-C-C2", "inBC2-C-C3" });

    inBlockInfo2.addParam(RowRepeatParamParser.DEFAULT_TAG, "A",
            new Object[] { "inBC2-R-A1", "inBC2-R-A2", "inBC2-R-A3" });

    inBlockInfo2.addParam(SingleParamParser.DEFAULT_TAG, "D", "inBC2-S-DDD");

    //?BC3
    ParamInfo inBlockInfo3 = new ParamInfo();
    inBlockInfo3.addParam(ColRepeatParamParser.DEFAULT_TAG, "A",
            new Object[] { "inBC3-C-A1", "inBC3-C-A2", "inBC3-C-A3" });
    inBlockInfo3.addParam(ColRepeatParamParser.DEFAULT_TAG, "B",
            new Object[] { "inBC3-C-B1", "inBC3-C-B2", "inBC3-C-B3", "inBC3-C-B4" });
    inBlockInfo3.addParam(ColRepeatParamParser.DEFAULT_TAG, "C",
            new Object[] { "inBC3-C-C1", "inBC3-C-C2", "inBC3-C-C3", "inBC3-C-C4", "inBC3-C-C5" });

    inBlockInfo3.addParam(RowRepeatParamParser.DEFAULT_TAG, "A",
            new Object[] { "inBC3-R-A1", "inBC3-R-A2", "inBC3-R-A3", "inBC3-R-A4", "inBC3-R-A5" });

    inBlockInfo3.addParam(SingleParamParser.DEFAULT_TAG, "D", "inBC3-S-DDD");

    //?BC4
    ParamInfo inBlockInfo4 = new ParamInfo();
    inBlockInfo4.addParam(ColRepeatParamParser.DEFAULT_TAG, "A",
            new Object[] { "inBC4-C-A1", "inBC4-C-A2", "inBC4-C-A3", "inBC4-C-A4", "inBC4-C-A5" });
    inBlockInfo4.addParam(ColRepeatParamParser.DEFAULT_TAG, "B",
            new Object[] { "inBC4-C-B1", "inBC4-C-B2", "inBC4-C-B3", "inBC4-C-B4" });
    inBlockInfo4.addParam(ColRepeatParamParser.DEFAULT_TAG, "C",
            new Object[] { "inBC4-C-C1", "inBC4-C-C2", "inBC4-C-C3" });

    inBlockInfo4.addParam(RowRepeatParamParser.DEFAULT_TAG, "A", new Object[] { "inBC4-R-A1", "inBC4-R-A2" });

    inBlockInfo4.addParam(SingleParamParser.DEFAULT_TAG, "D", "inBC4-S-DDD");

    //BC1
    ParamInfo blockInfo1 = new ParamInfo();
    blockInfo1.addParam(ColRepeatParamParser.DEFAULT_TAG, "A",
            new Object[] { "BC1-C-A1", "BC1-C-A2", "BC1-C-A3", "BC1-C-A4", "BC1-C-A5" });
    blockInfo1.addParam(ColRepeatParamParser.DEFAULT_TAG, "B",
            new Object[] { "BC1-C-B1", "BC1-C-B2", "BC1-C-B3", "BC1-C-B4" });
    blockInfo1.addParam(ColRepeatParamParser.DEFAULT_TAG, "C",
            new Object[] { "BC1-C-C1", "BC1-C-C2", "BC1-C-C3" });

    blockInfo1.addParam(RowRepeatParamParser.DEFAULT_TAG, "A",
            new Object[] { "BC1-R-A1", "BC1-R-A2", "BC1-R-A3", "BC1-R-A4", "BC1-R-A5" });
    blockInfo1.addParam(RowRepeatParamParser.DEFAULT_TAG, "B",
            new Object[] { "BC1-R-B1", "BC1-R-B2", "BC1-R-B3", "BC1-R-B4" });
    blockInfo1.addParam(RowRepeatParamParser.DEFAULT_TAG, "C",
            new Object[] { "BC1-R-C1", "BC1-R-C2", "BC1-R-C3" });

    blockInfo1.addParam(SingleParamParser.DEFAULT_TAG, "D", "BC1-S-DDD");
    blockInfo1.addParam(SingleParamParser.DEFAULT_TAG, "D2", "BC1-S-DDD2");

    blockInfo1.addParam(BlockColRepeatParamParser.DEFAULT_TAG, "inBC1",
            new Object[] { inBlockInfo1, inBlockInfo2 });
    blockInfo1.addParam(BlockRowRepeatParamParser.DEFAULT_TAG, "inBC1",
            new Object[] { inBlockInfo1, inBlockInfo2 });

    //BC2
    ParamInfo blockInfo2 = new ParamInfo();
    blockInfo2.addParam(ColRepeatParamParser.DEFAULT_TAG, "A",
            new Object[] { "BC2-C-A1", "BC2-C-A2", "BC2-C-A3", "BC2-C-A4" });
    blockInfo2.addParam(ColRepeatParamParser.DEFAULT_TAG, "B",
            new Object[] { "BC2-C-B1", "BC2-C-B2", "BC2-C-B3", "BC2-C-B4", "BC2-C-B5" });
    blockInfo2.addParam(ColRepeatParamParser.DEFAULT_TAG, "C",
            new Object[] { "BC2-C-C1", "BC2-C-C2", "BC2-C-C3" });

    blockInfo2.addParam(RowRepeatParamParser.DEFAULT_TAG, "A",
            new Object[] { "BC2-R-A1", "BC2-R-A2", "BC2-R-A3", "BC2-R-A4" });
    blockInfo2.addParam(RowRepeatParamParser.DEFAULT_TAG, "B",
            new Object[] { "BC2-R-B1", "BC2-R-B2", "BC2-R-B3", "BC2-R-B4", "BC2-R-B5" });
    blockInfo2.addParam(RowRepeatParamParser.DEFAULT_TAG, "C",
            new Object[] { "BC2-R-C1", "BC2-R-C2", "BC2-R-C3" });

    blockInfo2.addParam(SingleParamParser.DEFAULT_TAG, "D", "BC2-S-DDD");
    blockInfo2.addParam(SingleParamParser.DEFAULT_TAG, "D2", "BC2-S-DDD2");

    blockInfo2.addParam(BlockColRepeatParamParser.DEFAULT_TAG, "inBC1",
            new Object[] { inBlockInfo3, inBlockInfo4 });
    blockInfo2.addParam(BlockRowRepeatParamParser.DEFAULT_TAG, "inBC1",
            new Object[] { inBlockInfo3, inBlockInfo4 });

    //BC3
    ParamInfo blockInfo3 = new ParamInfo();
    blockInfo3.addParam(SingleParamParser.DEFAULT_TAG, "D", "BC2-S-DDD");
    blockInfo3.addParam(SingleParamParser.DEFAULT_TAG, "D2", "BC2-S-DDD2");

    //BC4
    ParamInfo blockInfo4 = new ParamInfo();
    blockInfo4.addParam(SingleParamParser.DEFAULT_TAG, "D", "BC2-S-DDD");
    blockInfo4.addParam(SingleParamParser.DEFAULT_TAG, "D2", "BC2-S-DDD2");

    //BC5
    ParamInfo blockInfo5 = new ParamInfo();
    blockInfo5.addParam(SingleParamParser.DEFAULT_TAG, "D", "BC5-S-DDD");
    blockInfo5.addParam(SingleParamParser.DEFAULT_TAG, "D2", "BC5-S-DDD2");

    for (int i = 0; i < reportSheets.length; i++) {
        ParamInfo info = reportSheets[i].getParamInfo();
        if (i == 1) {
            info.addParam(BlockColRepeatParamParser.DEFAULT_TAG, "BC1",
                    new Object[] { blockInfo1, blockInfo2, blockInfo3, blockInfo4, blockInfo5 });
        } else {
            info.addParam(BlockColRepeatParamParser.DEFAULT_TAG, "BC1",
                    new Object[] { blockInfo1, blockInfo2 });
        }
    }

    BlockColRepeatParamParser parser = new BlockColRepeatParamParser();
    ReportsParserInfo reportsParserInfo = new ReportsParserInfo();
    reportsParserInfo.setReportParsers(
            new ArrayList<ReportsTagParser<?>>(ReportCreateHelper.createDefaultParsers().values()));
    reportsParserInfo.setReportBook(reportBook);
    reportsParserInfo.setParamInfo(reportSheets[0].getParamInfo());

    // ??
    List<ParsedReportInfo> results = null;
    CellObject[] expectBeCells = null;
    CellObject[] expectAfCells = null;

    // -----------------------
    // []??
    // BC-C
    // -----------------------
    workbook = getWorkbook();
    Sheet sheet1 = workbook.getSheetAt(0);
    results = parseSheet(parser, sheet1, reportsParserInfo);
    expectBeCells = new CellObject[] { new CellObject(3, 3) };
    expectAfCells = new CellObject[] { new CellObject(3, 14) };
    checkResult(expectBeCells, expectAfCells, results);

    // ??
    if (version.equals("2007")) {
        int index = workbook.getSheetIndex(PoiUtil.TMP_SHEET_NAME);
        if (index > 0) {
            workbook.removeSheetAt(index);
        }
    }

    checkSheet("Sheet1", sheet1, true);

    // -----------------------
    // []??
    // BC-C2
    // -----------------------
    workbook = getWorkbook();
    Sheet sheet2 = workbook.getSheetAt(1);

    results = parseSheet(parser, sheet2, reportsParserInfo);

    expectBeCells = new CellObject[] { new CellObject(3, 3) };
    expectAfCells = new CellObject[] { new CellObject(3, 17) };
    checkResult(expectBeCells, expectAfCells, results);

    // ??
    if (version.equals("2007")) {
        int index = workbook.getSheetIndex(PoiUtil.TMP_SHEET_NAME);
        if (index > 0) {
            workbook.removeSheetAt(index);
        }
    }

    checkSheet("Sheet2", sheet2, true);

    // -----------------------
    // []??
    // BC-R
    // -----------------------
    workbook = getWorkbook();
    Sheet sheet3 = workbook.getSheetAt(2);

    results = parseSheet(parser, sheet3, reportsParserInfo);

    expectBeCells = new CellObject[] { new CellObject(3, 3) };
    expectAfCells = new CellObject[] { new CellObject(10, 6) };
    checkResult(expectBeCells, expectAfCells, results);

    // ??
    if (version.equals("2007")) {
        int index = workbook.getSheetIndex(PoiUtil.TMP_SHEET_NAME);
        if (index > 0) {
            workbook.removeSheetAt(index);
        }
    }

    checkSheet("Sheet3", sheet3, true);

    // -----------------------
    // []??
    // BC-R2
    // -----------------------
    workbook = getWorkbook();
    Sheet sheet4 = workbook.getSheetAt(3);

    results = parseSheet(parser, sheet4, reportsParserInfo);

    expectBeCells = new CellObject[] { new CellObject(3, 3) };
    expectAfCells = new CellObject[] { new CellObject(12, 6) };
    checkResult(expectBeCells, expectAfCells, results);

    checkSheet("Sheet4", sheet4, true);

    // -----------------------
    // []??
    // BC-CR
    // -----------------------
    workbook = getWorkbook();
    Sheet sheet5 = workbook.getSheetAt(4);

    results = parseSheet(parser, sheet5, reportsParserInfo);

    expectBeCells = new CellObject[] { new CellObject(4, 3) };
    expectAfCells = new CellObject[] { new CellObject(8, 14) };
    checkResult(expectBeCells, expectAfCells, results);

    // ??
    if (version.equals("2007")) {
        int index = workbook.getSheetIndex(PoiUtil.TMP_SHEET_NAME);
        if (index > 0) {
            workbook.removeSheetAt(index);
        }
    }

    checkSheet("Sheet5", sheet5, true);

    // -----------------------
    // []??
    // BC-BC
    // -----------------------
    workbook = getWorkbook();
    Sheet sheet6 = workbook.getSheetAt(5);

    results = parseSheet(parser, sheet6, reportsParserInfo);

    expectBeCells = new CellObject[] { new CellObject(8, 5), new CellObject(-1, -1), new CellObject(-1, -1) };
    expectAfCells = new CellObject[] { new CellObject(12, 28), new CellObject(-1, -1), new CellObject(-1, -1) };

    // ??
    if (version.equals("2007")) {
        int index = workbook.getSheetIndex(PoiUtil.TMP_SHEET_NAME);
        if (index > 0) {
            workbook.removeSheetAt(index);
        }
    }

    checkResult(expectBeCells, expectAfCells, results);

    checkSheet("Sheet6", sheet6, true);

    // -----------------------
    // []??
    // BC-BR
    // -----------------------
    workbook = getWorkbook();
    Sheet sheet7 = workbook.getSheetAt(6);

    results = parseSheet(parser, sheet7, reportsParserInfo);

    expectBeCells = new CellObject[] { new CellObject(8, 5) };
    expectAfCells = new CellObject[] { new CellObject(17, 18) };
    checkResult(expectBeCells, expectAfCells, results);

    // ??
    if (version.equals("2007")) {
        int index = workbook.getSheetIndex(PoiUtil.TMP_SHEET_NAME);
        if (index > 0) {
            workbook.removeSheetAt(index);
        }
    }

    checkSheet("sheet7", sheet7, true);

    // -----------------------
    // []
    // ??
    // ?
    // -----------------------
    ReportsParserInfo reportsParserInfo8 = new ReportsParserInfo();
    reportsParserInfo8.setReportParsers(
            new ArrayList<ReportsTagParser<?>>(ReportCreateHelper.createDefaultParsers().values()));
    reportsParserInfo8.setReportBook(reportBook);
    reportsParserInfo8.setParamInfo(reportSheets[1].getParamInfo());

    workbook = getWorkbook();
    Sheet sheet8 = workbook.getSheetAt(7);

    results = parseSheet(parser, sheet8, reportsParserInfo8);

    expectBeCells = new CellObject[] { new CellObject(3, 3) };
    expectAfCells = new CellObject[] { new CellObject(3, 12) };
    checkResult(expectBeCells, expectAfCells, results);

    // ??
    if (version.equals("2007")) {
        int index = workbook.getSheetIndex(PoiUtil.TMP_SHEET_NAME);
        if (index > 0) {
            workbook.removeSheetAt(index);
        }
    }

    checkSheet("sheet8", sheet8, true);

    // -----------------------
    // []?
    // ??fromCell???toCell??
    // -----------------------
    workbook = getWorkbook();
    Sheet sheet9 = workbook.getSheetAt(8);
    try {
        results = parseSheet(parser, sheet9, reportsParserInfo);
        fail("fromCell?????????");
    } catch (ParseException e) {
    }
    checkSheet("sheet9", sheet9, true);

    workbook = getWorkbook();
    Sheet sheet10 = workbook.getSheetAt(9);
    try {
        results = parseSheet(parser, sheet10, reportsParserInfo);
        fail("toCell?????????");
    } catch (ParseException e) {
    }
    checkSheet("sheet10", sheet10, true);

    // -----------------------
    // []?
    // ?fromCell?toCell
    // -----------------------
    workbook = getWorkbook();
    Sheet sheet11 = workbook.getSheetAt(10);

    try {
        results = parseSheet(parser, sheet11, reportsParserInfo);
        fail("fromCell???????????");
    } catch (ParseException e) {
    }
    checkSheet("sheet11", sheet11, true);

    workbook = getWorkbook();
    Sheet sheet12 = workbook.getSheetAt(11);

    try {
        results = parseSheet(parser, sheet12, reportsParserInfo);
        fail("toCell???????????");
    } catch (ParseException e) {
    }
    checkSheet("sheet12", sheet12, true);

    // -----------------------
    // []?
    // ?fromCell?toCell
    // -----------------------
    workbook = getWorkbook();
    Sheet sheet13 = workbook.getSheetAt(12);

    try {
        results = parseSheet(parser, sheet13, reportsParserInfo);
        fail("fromCell??????????");
    } catch (ParseException e) {
    }
    checkSheet("sheet13", sheet13, true);

    workbook = getWorkbook();
    Sheet sheet14 = workbook.getSheetAt(13);

    try {
        results = parseSheet(parser, sheet14, reportsParserInfo);
        fail("toCell??????????");
    } catch (ParseException e) {
    }
    checkSheet("sheet14", sheet14, true);

    // -----------------------
    // []?
    // ?fromCell > toCell
    // -----------------------
    workbook = getWorkbook();
    Sheet sheet15 = workbook.getSheetAt(14);

    try {
        results = parseSheet(parser, sheet15, reportsParserInfo);
        fail("fromCell > toCell?????????");
    } catch (ParseException e) {
    }
    checkSheet("sheet15", sheet15, true);

    workbook = getWorkbook();
    Sheet sheet17 = workbook.getSheetAt(16);

    try {
        results = parseSheet(parser, sheet17, reportsParserInfo);
        fail("fromCell > toCell?????????");
    } catch (ParseException e) {
    }
    checkSheet("sheet17", sheet17, true);

    // -----------------------
    // []?
    // ?repeatNum
    // -----------------------
    workbook = getWorkbook();
    Sheet sheet16 = workbook.getSheetAt(15);

    try {
        results = parseSheet(parser, sheet16, reportsParserInfo);
        fail("repeatNum??????????");
    } catch (ParseException e) {
    }
    checkSheet("sheet16", sheet16, true);

    workbook = getWorkbook();
    Sheet sheet18 = workbook.getSheetAt(17);

    try {
        results = parseSheet(parser, sheet18, reportsParserInfo);
        fail("repeatNum??????????");
    } catch (ParseException e) {
    }
    checkSheet("sheet18", sheet18, true);

    // ----------------------------------
    // []?
    //   (toCell)?
    //       ??????
    //
    //   (toCell)?
    //       ?????????????
    //       ????
    //
    //   ??????
    // ----------------------------------
    workbook = getWorkbook();
    Sheet sheet19 = workbook.getSheetAt(18);

    results = parseSheet(parser, sheet19, reportsParserInfo);

    expectBeCells = new CellObject[] { new CellObject(6, 3) };
    expectAfCells = new CellObject[] { new CellObject(6, 6) };
    checkResult(expectBeCells, expectAfCells, results);

    // ??
    if (version.equals("2007")) {
        int index = workbook.getSheetIndex(PoiUtil.TMP_SHEET_NAME);
        if (index > 0) {
            workbook.removeSheetAt(index);
        }
    }

    checkSheet("sheet19", sheet19, true);

    // ----------------------------------
    // []?
    //   ?????
    // ----------------------------------
    workbook = getWorkbook();
    Sheet sheet20 = workbook.getSheetAt(19);

    results = parseSheet(parser, sheet20, reportsParserInfo);

    expectBeCells = new CellObject[] { new CellObject(11, 7) };
    expectAfCells = new CellObject[] { new CellObject(19, 27) };
    checkResult(expectBeCells, expectAfCells, results);

    // ??
    if (version.equals("2007")) {
        int index = workbook.getSheetIndex(PoiUtil.TMP_SHEET_NAME);
        if (index > 0) {
            workbook.removeSheetAt(index);
        }
    }

    checkSheet("sheet20", sheet20, true);

    // ----------------------------------
    // []?
    //   R???
    //   ????????
    //   ????
    // ----------------------------------
    workbook = getWorkbook();
    Sheet sheet21 = workbook.getSheetAt(20);

    try {
        results = parseSheet(parser, sheet21, reportsParserInfo);
        fail("???????");
    } catch (ParseException e) {
        // org.bbreak.excella.core.util.PoiUtil#getMergedAddress( Sheet sheet, CellRangeAddress rangeAddress)
        // ?throw????????
        // 
        //   BR?BC???????????
        //   ParseException??????????
        //   getCause?instanceof????getMessage?????
        assertTrue(e.getMessage().contains("IllegalArgumentException"));
        assertTrue(e.getMessage().contains("There are crossing merged regions in the range."));
    }

    // ??
    if (version.equals("2007")) {
        int index = workbook.getSheetIndex(PoiUtil.TMP_SHEET_NAME);
        if (index > 0) {
            workbook.removeSheetAt(index);
        }
    }

    checkSheet("sheet21", sheet21, true);

    // ----------------------------------
    // []?
    //   C???
    //   ????????
    //   ????
    // ----------------------------------
    workbook = getWorkbook();
    Sheet sheet22 = workbook.getSheetAt(21);

    try {
        results = parseSheet(parser, sheet22, reportsParserInfo);
        fail("???????");
    } catch (ParseException e) {
        // org.bbreak.excella.core.util.PoiUtil#getMergedAddress( Sheet sheet, CellRangeAddress rangeAddress)
        // ?throw????????
        // 
        //   BR?BC???????????
        //   ParseException??????????
        //   getCause?instanceof????getMessage?????
        assertTrue(e.getMessage().contains("IllegalArgumentException"));
        assertTrue(e.getMessage().contains("There are crossing merged regions in the range."));
    }

    // ??
    if (version.equals("2007")) {
        int index = workbook.getSheetIndex(PoiUtil.TMP_SHEET_NAME);
        if (index > 0) {
            workbook.removeSheetAt(index);
        }
    }

    checkSheet("sheet22", sheet22, true);

    // -----------------------
    // []
    // (=paramInfo)
    // -----------------------
    ReportsParserInfo reportsParserInfo23 = new ReportsParserInfo();
    reportsParserInfo23.setReportParsers(
            new ArrayList<ReportsTagParser<?>>(ReportCreateHelper.createDefaultParsers().values()));
    reportsParserInfo23.setReportBook(reportBook);
    reportsParserInfo23.setParamInfo(reportSheets[0].getParamInfo());

    workbook = getWorkbook();
    Sheet sheet23 = workbook.getSheetAt(22);

    results = parseSheet(parser, sheet23, reportsParserInfo23);

    expectBeCells = new CellObject[] { new CellObject(3, 3) };
    expectAfCells = new CellObject[] { new CellObject(3, 6) };
    checkResult(expectBeCells, expectAfCells, results);

    // ??
    if (version.equals("2007")) {
        int index = workbook.getSheetIndex(PoiUtil.TMP_SHEET_NAME);
        if (index > 0) {
            workbook.removeSheetAt(index);
        }
    }

    checkSheet("sheet23", sheet23, true);

    // -----------------------
    // []
    // (=paramInfo+1)
    // -----------------------
    ReportsParserInfo reportsParserInfo24 = new ReportsParserInfo();
    reportsParserInfo24.setReportParsers(
            new ArrayList<ReportsTagParser<?>>(ReportCreateHelper.createDefaultParsers().values()));
    reportsParserInfo24.setReportBook(reportBook);
    reportsParserInfo24.setParamInfo(reportSheets[0].getParamInfo());

    workbook = getWorkbook();
    Sheet sheet24 = workbook.getSheetAt(23);

    results = parseSheet(parser, sheet24, reportsParserInfo24);

    expectBeCells = new CellObject[] { new CellObject(3, 3) };
    expectAfCells = new CellObject[] { new CellObject(3, 9) };
    checkResult(expectBeCells, expectAfCells, results);

    // ??
    if (version.equals("2007")) {
        int index = workbook.getSheetIndex(PoiUtil.TMP_SHEET_NAME);
        if (index > 0) {
            workbook.removeSheetAt(index);
        }
    }

    checkSheet("sheet24", sheet24, true);
}

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

License:Open Source License

@Test
public void testParseSheetCellObject() throws ParseException {
    Workbook workbook = null;

    ReportBook reportBook = new ReportBook("", "test", new ConvertConfiguration[] {});
    ReportSheet reportSheet1 = new ReportSheet("sheet1", "Sheet1");
    reportBook.addReportSheet(reportSheet1);
    ReportSheet reportSheet2 = new ReportSheet("sheet1", "Sheet2");
    reportBook.addReportSheet(reportSheet2);

    ReportSheet[] reportSheets = new ReportSheet[] { reportSheet1, reportSheet2 };

    // -----------------------
    // /*from   w  ww  .  ja  va  2s  . c o  m*/
    // -----------------------

    //?1
    ParamInfo inBlockInfo1 = new ParamInfo();
    inBlockInfo1.addParam(ColRepeatParamParser.DEFAULT_TAG, "A",
            new Object[] { "inBR1-C-A1", "inBR1-C-A2", "inBR1-C-A3" });
    inBlockInfo1.addParam(ColRepeatParamParser.DEFAULT_TAG, "B", new Object[] { "inBR1-C-B1", "inBR1-C-B1" });
    inBlockInfo1.addParam(ColRepeatParamParser.DEFAULT_TAG, "C", new Object[] { "inBR1-C-C1" });

    inBlockInfo1.addParam(RowRepeatParamParser.DEFAULT_TAG, "A", new Object[] { "inBR1-R-A1" });

    inBlockInfo1.addParam(SingleParamParser.DEFAULT_TAG, "D", "inBR1-S-DDD");

    //?2
    ParamInfo inBlockInfo2 = new ParamInfo();
    inBlockInfo2.addParam(ColRepeatParamParser.DEFAULT_TAG, "A", new Object[] { "inBR2-C-A1" });
    inBlockInfo2.addParam(ColRepeatParamParser.DEFAULT_TAG, "B", new Object[] { "inBR2-C-B1", "inBR2-C-B2" });
    inBlockInfo2.addParam(ColRepeatParamParser.DEFAULT_TAG, "C",
            new Object[] { "inBR2-C-C1", "inBR2-C-C2", "inBR2-C-C3" });

    inBlockInfo2.addParam(RowRepeatParamParser.DEFAULT_TAG, "A",
            new Object[] { "inBR2-R-A1", "inBR2-R-A2", "inBR2-R-A3" });

    inBlockInfo2.addParam(SingleParamParser.DEFAULT_TAG, "D", "inBR2-S-DDD");

    //?3
    ParamInfo inBlockInfo3 = new ParamInfo();
    inBlockInfo3.addParam(ColRepeatParamParser.DEFAULT_TAG, "A",
            new Object[] { "inBR3-C-A1", "inBR3-C-A2", "inBR3-C-A3" });
    inBlockInfo3.addParam(ColRepeatParamParser.DEFAULT_TAG, "B",
            new Object[] { "inBR3-C-B1", "inBR3-C-B2", "inBR3-C-B3", "inBR3-C-B4" });
    inBlockInfo3.addParam(ColRepeatParamParser.DEFAULT_TAG, "C",
            new Object[] { "inBR3-C-C1", "inBR3-C-C2", "inBR3-C-C3", "inBR3-C-C4", "inBR3-C-C5" });

    inBlockInfo3.addParam(RowRepeatParamParser.DEFAULT_TAG, "A",
            new Object[] { "inBR3-R-A1", "inBR3-R-A2", "inBR3-R-A3", "inBR3-R-A4", "inBR3-R-A5" });

    inBlockInfo3.addParam(SingleParamParser.DEFAULT_TAG, "D", "inBR3-S-DDD");

    //?4
    ParamInfo inBlockInfo4 = new ParamInfo();
    inBlockInfo4.addParam(ColRepeatParamParser.DEFAULT_TAG, "A",
            new Object[] { "inBR4-C-A1", "inBR4-C-A2", "inBR4-C-A3", "inBR4-C-A4", "inBR4-C-A5" });
    inBlockInfo4.addParam(ColRepeatParamParser.DEFAULT_TAG, "B",
            new Object[] { "inBR4-C-B1", "inBR4-C-B2", "inBR4-C-B3", "inBR4-C-B4" });
    inBlockInfo4.addParam(ColRepeatParamParser.DEFAULT_TAG, "C",
            new Object[] { "inBR4-C-C1", "inBR4-C-C2", "inBR4-C-C3" });

    inBlockInfo4.addParam(RowRepeatParamParser.DEFAULT_TAG, "A", new Object[] { "inBR4-R-A1", "inBR4-R-A2" });

    inBlockInfo4.addParam(SingleParamParser.DEFAULT_TAG, "D", "inBR4-S-DDD");

    //BR1
    ParamInfo blockInfo1 = new ParamInfo();
    blockInfo1.addParam(ColRepeatParamParser.DEFAULT_TAG, "A",
            new Object[] { "BR1-C-A1", "BR1-C-A2", "BR1-C-A3", "BR1-C-A4", "BR1-C-A5" });
    blockInfo1.addParam(ColRepeatParamParser.DEFAULT_TAG, "B",
            new Object[] { "BR1-C-B1", "BR1-C-B2", "BR1-C-B3", "BR1-C-B4" });
    blockInfo1.addParam(ColRepeatParamParser.DEFAULT_TAG, "C",
            new Object[] { "BR1-C-C1", "BR1-C-C2", "BR1-C-C3" });

    blockInfo1.addParam(RowRepeatParamParser.DEFAULT_TAG, "A",
            new Object[] { "BR1-R-A1", "BR1-R-A2", "BR1-R-A3", "BR1-R-A4", "BR1-R-A5" });
    blockInfo1.addParam(RowRepeatParamParser.DEFAULT_TAG, "B",
            new Object[] { "BR1-R-B1", "BR1-R-B2", "BR1-R-B3", "BR1-R-B4" });
    blockInfo1.addParam(RowRepeatParamParser.DEFAULT_TAG, "C",
            new Object[] { "BR1-R-C1", "BR1-R-C2", "BR1-R-C3" });

    blockInfo1.addParam(SingleParamParser.DEFAULT_TAG, "D", "BR1-S-DDD");
    blockInfo1.addParam(SingleParamParser.DEFAULT_TAG, "D2", "BR1-S-DDD2");

    blockInfo1.addParam(BlockColRepeatParamParser.DEFAULT_TAG, "inBR1",
            new Object[] { inBlockInfo1, inBlockInfo2 });
    blockInfo1.addParam(BlockRowRepeatParamParser.DEFAULT_TAG, "inBR1",
            new Object[] { inBlockInfo1, inBlockInfo2 });

    //BC2
    ParamInfo blockInfo2 = new ParamInfo();
    blockInfo2.addParam(ColRepeatParamParser.DEFAULT_TAG, "A",
            new Object[] { "BR2-C-A1", "BR2-C-A2", "BR2-C-A3", "BR2-C-A4" });
    blockInfo2.addParam(ColRepeatParamParser.DEFAULT_TAG, "B",
            new Object[] { "BR2-C-B1", "BR2-C-B2", "BR2-C-B3", "BR2-C-B4", "BR2-C-B5" });
    blockInfo2.addParam(ColRepeatParamParser.DEFAULT_TAG, "C",
            new Object[] { "BR2-C-C1", "BR2-C-C2", "BR2-C-C3" });

    blockInfo2.addParam(RowRepeatParamParser.DEFAULT_TAG, "A",
            new Object[] { "BR2-R-A1", "BR2-R-A2", "BR2-R-A3", "BR2-R-A4" });
    blockInfo2.addParam(RowRepeatParamParser.DEFAULT_TAG, "B",
            new Object[] { "BR2-R-B1", "BR2-R-B2", "BR2-R-B3", "BR2-R-B4", "BR2-R-B5" });
    blockInfo2.addParam(RowRepeatParamParser.DEFAULT_TAG, "C",
            new Object[] { "BR2-R-C1", "BR2-R-C2", "BR2-R-C3" });

    blockInfo2.addParam(SingleParamParser.DEFAULT_TAG, "D", "BR2-S-DDD");
    blockInfo2.addParam(SingleParamParser.DEFAULT_TAG, "D2", "BR2-S-DDD2");

    blockInfo2.addParam(BlockColRepeatParamParser.DEFAULT_TAG, "inBR1",
            new Object[] { inBlockInfo3, inBlockInfo4 });
    blockInfo2.addParam(BlockRowRepeatParamParser.DEFAULT_TAG, "inBR1",
            new Object[] { inBlockInfo3, inBlockInfo4 });

    //BC3
    ParamInfo blockInfo3 = new ParamInfo();
    blockInfo3.addParam(SingleParamParser.DEFAULT_TAG, "D", "BR2-S-DDD");
    blockInfo3.addParam(SingleParamParser.DEFAULT_TAG, "D2", "BR2-S-DDD2");

    //BC4
    ParamInfo blockInfo4 = new ParamInfo();
    blockInfo4.addParam(SingleParamParser.DEFAULT_TAG, "D", "BR2-S-DDD");
    blockInfo4.addParam(SingleParamParser.DEFAULT_TAG, "D2", "BR2-S-DDD2");

    //BC5
    ParamInfo blockInfo5 = new ParamInfo();
    blockInfo5.addParam(SingleParamParser.DEFAULT_TAG, "D", "BR5-S-DDD");
    blockInfo5.addParam(SingleParamParser.DEFAULT_TAG, "D2", "BR5-S-DDD2");

    for (int i = 0; i < reportSheets.length; i++) {
        ParamInfo info = reportSheets[i].getParamInfo();
        if (i == 1) {
            info.addParam(BlockRowRepeatParamParser.DEFAULT_TAG, "BR1",
                    new Object[] { blockInfo1, blockInfo2, blockInfo3, blockInfo4, blockInfo5 });
        } else {
            info.addParam(BlockRowRepeatParamParser.DEFAULT_TAG, "BR1",
                    new Object[] { blockInfo1, blockInfo2 });
        }
    }

    BlockRowRepeatParamParser parser = new BlockRowRepeatParamParser();
    ReportsParserInfo reportsParserInfo = new ReportsParserInfo();
    reportsParserInfo.setReportParsers(
            new ArrayList<ReportsTagParser<?>>(ReportCreateHelper.createDefaultParsers().values()));
    reportsParserInfo.setReportBook(reportBook);
    reportsParserInfo.setParamInfo(reportSheets[0].getParamInfo());

    // ??
    List<ParsedReportInfo> results = null;
    CellObject[] expectBeCells = null;
    CellObject[] expectAfCells = null;

    // -----------------------
    // []??
    // BR-C
    // -----------------------
    workbook = getWorkbook();
    Sheet sheet1 = workbook.getSheetAt(0);
    results = parseSheet(parser, sheet1, reportsParserInfo);
    expectBeCells = new CellObject[] { new CellObject(3, 3) };
    expectAfCells = new CellObject[] { new CellObject(6, 7) };
    checkResult(expectBeCells, expectAfCells, results);

    // ??
    if (version.equals("2007")) {
        int index = workbook.getSheetIndex(PoiUtil.TMP_SHEET_NAME);
        if (index > 0) {
            workbook.removeSheetAt(index);
        }
    }

    checkSheet("Sheet1", sheet1, true);

    // -----------------------
    // []??
    // BR-C2
    // -----------------------
    workbook = getWorkbook();
    Sheet sheet2 = workbook.getSheetAt(1);

    results = parseSheet(parser, sheet2, reportsParserInfo);

    expectBeCells = new CellObject[] { new CellObject(3, 3) };
    expectAfCells = new CellObject[] { new CellObject(6, 9) };
    checkResult(expectBeCells, expectAfCells, results);

    // ??
    if (version.equals("2007")) {
        int index = workbook.getSheetIndex(PoiUtil.TMP_SHEET_NAME);
        if (index > 0) {
            workbook.removeSheetAt(index);
        }
    }

    checkSheet("Sheet2", sheet2, true);

    // -----------------------
    // []??
    // BR-R
    // -----------------------
    workbook = getWorkbook();
    Sheet sheet3 = workbook.getSheetAt(2);

    results = parseSheet(parser, sheet3, reportsParserInfo);

    expectBeCells = new CellObject[] { new CellObject(3, 3) };
    expectAfCells = new CellObject[] { new CellObject(20, 3) };
    checkResult(expectBeCells, expectAfCells, results);

    // ??
    if (version.equals("2007")) {
        int index = workbook.getSheetIndex(PoiUtil.TMP_SHEET_NAME);
        if (index > 0) {
            workbook.removeSheetAt(index);
        }
    }

    checkSheet("Sheet3", sheet3, true);

    // -----------------------
    // []??
    // BR-R2
    // -----------------------
    workbook = getWorkbook();
    Sheet sheet4 = workbook.getSheetAt(3);

    results = parseSheet(parser, sheet4, reportsParserInfo);

    expectBeCells = new CellObject[] { new CellObject(3, 3) };
    expectAfCells = new CellObject[] { new CellObject(24, 3) };
    checkResult(expectBeCells, expectAfCells, results);

    checkSheet("Sheet4", sheet4, true);

    // -----------------------
    // []??
    // BR-CR
    // -----------------------
    workbook = getWorkbook();
    Sheet sheet5 = workbook.getSheetAt(4);

    results = parseSheet(parser, sheet5, reportsParserInfo);

    expectBeCells = new CellObject[] { new CellObject(4, 3) };
    expectAfCells = new CellObject[] { new CellObject(15, 7) };
    checkResult(expectBeCells, expectAfCells, results);

    // ??
    if (version.equals("2007")) {
        int index = workbook.getSheetIndex(PoiUtil.TMP_SHEET_NAME);
        if (index > 0) {
            workbook.removeSheetAt(index);
        }
    }

    checkSheet("Sheet5", sheet5, true);

    // -----------------------
    // []??
    // BR-BR
    // -----------------------
    workbook = getWorkbook();
    Sheet sheet6 = workbook.getSheetAt(5);

    results = parseSheet(parser, sheet6, reportsParserInfo);

    expectBeCells = new CellObject[] { new CellObject(8, 5), new CellObject(-1, -1), new CellObject(-1, -1) };
    expectAfCells = new CellObject[] { new CellObject(31, 9), new CellObject(-1, -1), new CellObject(-1, -1) };

    // ??
    if (version.equals("2007")) {
        int index = workbook.getSheetIndex(PoiUtil.TMP_SHEET_NAME);
        if (index > 0) {
            workbook.removeSheetAt(index);
        }
    }

    checkResult(expectBeCells, expectAfCells, results);

    checkSheet("Sheet6", sheet6, true);

    // -----------------------
    // []??
    // BR-BC
    // -----------------------
    workbook = getWorkbook();
    Sheet sheet7 = workbook.getSheetAt(6);

    results = parseSheet(parser, sheet7, reportsParserInfo);

    expectBeCells = new CellObject[] { new CellObject(8, 5) };
    expectAfCells = new CellObject[] { new CellObject(22, 16) };
    checkResult(expectBeCells, expectAfCells, results);

    // ??
    if (version.equals("2007")) {
        int index = workbook.getSheetIndex(PoiUtil.TMP_SHEET_NAME);
        if (index > 0) {
            workbook.removeSheetAt(index);
        }
    }

    checkSheet("sheet7", sheet7, true);

    // -----------------------
    // []
    // ??
    // ?
    // -----------------------
    ReportsParserInfo reportsParserInfo8 = new ReportsParserInfo();
    reportsParserInfo8.setReportParsers(
            new ArrayList<ReportsTagParser<?>>(ReportCreateHelper.createDefaultParsers().values()));
    reportsParserInfo8.setReportBook(reportBook);
    reportsParserInfo8.setParamInfo(reportSheets[1].getParamInfo());

    workbook = getWorkbook();
    Sheet sheet8 = workbook.getSheetAt(7);

    results = parseSheet(parser, sheet8, reportsParserInfo8);

    expectBeCells = new CellObject[] { new CellObject(3, 3) };
    expectAfCells = new CellObject[] { new CellObject(12, 3) };
    checkResult(expectBeCells, expectAfCells, results);

    // ??
    if (version.equals("2007")) {
        int index = workbook.getSheetIndex(PoiUtil.TMP_SHEET_NAME);
        if (index > 0) {
            workbook.removeSheetAt(index);
        }
    }

    checkSheet("sheet8", sheet8, true);

    // -----------------------
    // []?
    // ??fromCell???toCell??
    // -----------------------
    workbook = getWorkbook();
    Sheet sheet9 = workbook.getSheetAt(8);
    try {
        results = parseSheet(parser, sheet9, reportsParserInfo);
        fail("fromCell?????????");
    } catch (ParseException e) {
    }
    checkSheet("sheet9", sheet9, true);

    workbook = getWorkbook();
    Sheet sheet10 = workbook.getSheetAt(9);
    try {
        results = parseSheet(parser, sheet10, reportsParserInfo);
        fail("toCell?????????");
    } catch (ParseException e) {
    }
    checkSheet("sheet10", sheet10, true);

    // -----------------------
    // []?
    // ?fromCell?toCell
    // -----------------------
    workbook = getWorkbook();
    Sheet sheet11 = workbook.getSheetAt(10);

    try {
        results = parseSheet(parser, sheet11, reportsParserInfo);
        fail("fromCell???????????");
    } catch (ParseException e) {
    }
    checkSheet("sheet11", sheet11, true);

    workbook = getWorkbook();
    Sheet sheet12 = workbook.getSheetAt(11);

    try {
        results = parseSheet(parser, sheet12, reportsParserInfo);
        fail("toCell???????????");
    } catch (ParseException e) {
    }
    checkSheet("sheet12", sheet12, true);

    // -----------------------
    // []?
    // ?fromCell?toCell
    // -----------------------
    workbook = getWorkbook();
    Sheet sheet13 = workbook.getSheetAt(12);

    try {
        results = parseSheet(parser, sheet13, reportsParserInfo);
        fail("fromCell??????????");
    } catch (ParseException e) {
    }
    checkSheet("sheet13", sheet13, true);

    workbook = getWorkbook();
    Sheet sheet14 = workbook.getSheetAt(13);

    try {
        results = parseSheet(parser, sheet14, reportsParserInfo);
        fail("toCell??????????");
    } catch (ParseException e) {
    }
    checkSheet("sheet14", sheet14, true);

    // -----------------------
    // []?
    // ?fromCell > toCell
    // -----------------------
    workbook = getWorkbook();
    Sheet sheet15 = workbook.getSheetAt(14);

    try {
        results = parseSheet(parser, sheet15, reportsParserInfo);
        fail("fromCell > toCell?????????");
    } catch (ParseException e) {
    }
    checkSheet("sheet15", sheet15, true);

    workbook = getWorkbook();
    Sheet sheet17 = workbook.getSheetAt(16);

    try {
        results = parseSheet(parser, sheet17, reportsParserInfo);
        fail("fromCell > toCell?????????");
    } catch (ParseException e) {
    }
    checkSheet("sheet17", sheet17, true);

    // -----------------------
    // []?
    // ?repeatNum
    // -----------------------
    workbook = getWorkbook();
    Sheet sheet16 = workbook.getSheetAt(15);

    try {
        results = parseSheet(parser, sheet16, reportsParserInfo);
        fail("repeatNum??????????");
    } catch (ParseException e) {
    }
    checkSheet("sheet16", sheet16, true);

    workbook = getWorkbook();
    Sheet sheet18 = workbook.getSheetAt(17);

    try {
        results = parseSheet(parser, sheet18, reportsParserInfo);
        fail("repeatNum??????????");
    } catch (ParseException e) {
    }
    checkSheet("sheet18", sheet18, true);

    // ----------------------------------
    // []?
    //   (toCell)?
    //       ??????
    //
    //   (toCell)?
    //       ?????????????
    //       ????
    //
    //   ??????
    // ----------------------------------
    workbook = getWorkbook();
    Sheet sheet19 = workbook.getSheetAt(18);

    results = parseSheet(parser, sheet19, reportsParserInfo);

    expectBeCells = new CellObject[] { new CellObject(6, 3) };
    expectAfCells = new CellObject[] { new CellObject(12, 3) };
    checkResult(expectBeCells, expectAfCells, results);

    // ??
    if (version.equals("2007")) {
        int index = workbook.getSheetIndex(PoiUtil.TMP_SHEET_NAME);
        if (index > 0) {
            workbook.removeSheetAt(index);
        }
    }

    checkSheet("sheet19", sheet19, true);

    // ----------------------------------
    // []?
    //   ?????
    // ----------------------------------
    workbook = getWorkbook();
    Sheet sheet20 = workbook.getSheetAt(19);

    results = parseSheet(parser, sheet20, reportsParserInfo);

    expectBeCells = new CellObject[] { new CellObject(11, 7) };
    expectAfCells = new CellObject[] { new CellObject(30, 15) };
    checkResult(expectBeCells, expectAfCells, results);

    // ??
    if (version.equals("2007")) {
        int index = workbook.getSheetIndex(PoiUtil.TMP_SHEET_NAME);
        if (index > 0) {
            workbook.removeSheetAt(index);
        }
    }

    checkSheet("sheet20", sheet20, true);

    // ----------------------------------
    // []?
    //   R???
    //   ????????
    //   ????
    // ----------------------------------
    workbook = getWorkbook();
    Sheet sheet21 = workbook.getSheetAt(20);

    try {
        results = parseSheet(parser, sheet21, reportsParserInfo);
        fail("???????");
    } catch (ParseException e) {
        // org.bbreak.excella.core.util.PoiUtil#getMergedAddress( Sheet sheet, CellRangeAddress rangeAddress)
        // ?throw????????
        // 
        //   BR?BC???????????
        //   ParseException??????????
        //   getCause?instanceof????getMessage?????
        assertTrue(e.getMessage().contains("IllegalArgumentException"));
        assertTrue(e.getMessage().contains("There are crossing merged regions in the range."));
    }

    // ??
    if (version.equals("2007")) {
        int index = workbook.getSheetIndex(PoiUtil.TMP_SHEET_NAME);
        if (index > 0) {
            workbook.removeSheetAt(index);
        }
    }

    checkSheet("sheet21", sheet21, true);

    // ----------------------------------
    // []?
    //   C???
    //   ????????
    //   ????
    // ----------------------------------
    workbook = getWorkbook();
    Sheet sheet22 = workbook.getSheetAt(21);

    try {
        results = parseSheet(parser, sheet22, reportsParserInfo);
        fail("???????");
    } catch (ParseException e) {
        // org.bbreak.excella.core.util.PoiUtil#getMergedAddress( Sheet sheet, CellRangeAddress rangeAddress)
        // ?throw????????
        // 
        //   BR?BC???????????
        //   ParseException??????????
        //   getCause?instanceof????getMessage?????
        assertTrue(e.getMessage().contains("IllegalArgumentException"));
        assertTrue(e.getMessage().contains("There are crossing merged regions in the range."));
    }

    // ??
    if (version.equals("2007")) {
        int index = workbook.getSheetIndex(PoiUtil.TMP_SHEET_NAME);
        if (index > 0) {
            workbook.removeSheetAt(index);
        }
    }

    checkSheet("sheet22", sheet22, true);

    // -----------------------
    // []
    // (=paramInfo)
    // -----------------------
    ReportsParserInfo reportsParserInfo23 = new ReportsParserInfo();
    reportsParserInfo23.setReportParsers(
            new ArrayList<ReportsTagParser<?>>(ReportCreateHelper.createDefaultParsers().values()));
    reportsParserInfo23.setReportBook(reportBook);
    reportsParserInfo23.setParamInfo(reportSheets[0].getParamInfo());

    workbook = getWorkbook();
    Sheet sheet23 = workbook.getSheetAt(22);

    results = parseSheet(parser, sheet23, reportsParserInfo23);

    expectBeCells = new CellObject[] { new CellObject(3, 4) };
    expectAfCells = new CellObject[] { new CellObject(6, 4) };
    checkResult(expectBeCells, expectAfCells, results);

    // ??
    if (version.equals("2007")) {
        int index = workbook.getSheetIndex(PoiUtil.TMP_SHEET_NAME);
        if (index > 0) {
            workbook.removeSheetAt(index);
        }
    }

    checkSheet("sheet23", sheet23, true);
    // -----------------------
    // []
    // (=paramInfo+1)
    // -----------------------
    ReportsParserInfo reportsParserInfo24 = new ReportsParserInfo();
    reportsParserInfo24.setReportParsers(
            new ArrayList<ReportsTagParser<?>>(ReportCreateHelper.createDefaultParsers().values()));
    reportsParserInfo24.setReportBook(reportBook);
    reportsParserInfo24.setParamInfo(reportSheets[0].getParamInfo());

    workbook = getWorkbook();
    Sheet sheet24 = workbook.getSheetAt(23);

    results = parseSheet(parser, sheet24, reportsParserInfo24);

    expectBeCells = new CellObject[] { new CellObject(3, 4) };
    expectAfCells = new CellObject[] { new CellObject(9, 4) };
    checkResult(expectBeCells, expectAfCells, results);

    // ??
    if (version.equals("2007")) {
        int index = workbook.getSheetIndex(PoiUtil.TMP_SHEET_NAME);
        if (index > 0) {
            workbook.removeSheetAt(index);
        }
    }

    checkSheet("sheet24", sheet24, true);
}

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

    // ?/*www .j a v a2s .  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.ColRepeatParamParserTest.java

License:Open Source License

/**
 * {@link org.bbreak.excella.reports.tag.ColRepeatParamParser#parse(org.apache.poi.ss.usermodel.Sheet, org.apache.poi.ss.usermodel.Cell, java.lang.Object)} ????
 */// w w w  .j ava 2s. c om
@Test
public void testParseSheetCellObject() {
    Workbook workbook = null;
    // -----------------------
    // []??
    // -----------------------
    workbook = getWorkbook();
    Sheet sheet1 = workbook.getSheetAt(0);

    ReportBook reportBook = new ReportBook("", "test", new ConvertConfiguration[] {});
    ReportSheet reportSheet1 = new ReportSheet("sheet1", "Sheet1");
    reportBook.addReportSheet(reportSheet1);
    ReportSheet reportSheet2 = new ReportSheet("sheet1", "Sheet2");
    reportBook.addReportSheet(reportSheet2);
    ReportSheet reportSheet3 = new ReportSheet("sheet1", "Sheet3");
    reportBook.addReportSheet(reportSheet3);

    ReportSheet[] reportSheets = new ReportSheet[] { reportSheet1, reportSheet2, reportSheet3 };

    for (ReportSheet reportSheet : reportSheets) {
        ParamInfo info = reportSheet.getParamInfo();
        info.addParam("$C[]", "A", new Object[] { "AA1", "AA1", "AA2", "AA2", "AA3" });
        info.addParam("$C[]", "B", new Object[] { "BB1", "BB1", "BB2" });
        info.addParam("$C[]", "C", new Object[] { "CC1", "CC2", "CC3", "CC4", "CC5" });
        info.addParam("$", "D", "DDD");
    }

    ColRepeatParamParser parser = new ColRepeatParamParser();
    ReportsParserInfo reportsParserInfo = new ReportsParserInfo();
    reportsParserInfo.setReportParsers(
            new ArrayList<ReportsTagParser<?>>(ReportCreateHelper.createDefaultParsers().values()));
    reportsParserInfo.setReportBook(reportBook);
    reportsParserInfo.setParamInfo(reportSheets[0].getParamInfo());

    // ??
    List<ParsedReportInfo> results = null;
    try {
        results = parseSheet(parser, sheet1, reportsParserInfo);
    } catch (ParseException e) {
        fail(e.toString());
    }

    CellObject[] expectBeCells = new CellObject[] { new CellObject(0, 0), new CellObject(2, 1) };
    CellObject[] expectAfCells = new CellObject[] { new CellObject(0, 4), new CellObject(2, 3) };
    checkResult(expectBeCells, expectAfCells, results);

    // ??
    if (version.equals("2007")) {
        int index = workbook.getSheetIndex(PoiUtil.TMP_SHEET_NAME);
        if (index > 0) {
            workbook.removeSheetAt(index);
        }
    }

    checkSheet("Sheet1", sheet1, true);
    // -----------------------
    // []
    // ??
    // ?
    // -----------------------
    workbook = getWorkbook();
    Sheet sheet2 = workbook.getSheetAt(1);
    // ??
    try {
        results = parseSheet(parser, sheet2, reportsParserInfo);
    } catch (ParseException e) {
        fail(e.toString());
    }

    expectBeCells = new CellObject[] { new CellObject(0, 0), new CellObject(2, 1), new CellObject(4, 2) };
    expectAfCells = new CellObject[] { new CellObject(0, 4), new CellObject(2, 3), new CellObject(4, 3) };
    checkResult(expectBeCells, expectAfCells, results);

    // ??
    if (version.equals("2007")) {
        int index = workbook.getSheetIndex(PoiUtil.TMP_SHEET_NAME);
        if (index > 0) {
            workbook.removeSheetAt(index);
        }
    }
    checkSheet("Sheet2", sheet2, true);

    // -----------------------
    // []
    // 
    // -----------------------
    workbook = getWorkbook();
    Sheet sheet3 = workbook.getSheetAt(2);
    // ??
    try {
        results = parseSheet(parser, sheet3, reportsParserInfo);
    } catch (ParseException e) {
        e.printStackTrace();
        fail(e.toString());
    }

    expectBeCells = new CellObject[] { new CellObject(0, 0), new CellObject(1, 0), new CellObject(2, 0),
            new CellObject(16, 0), new CellObject(17, 0) };
    expectAfCells = new CellObject[] { new CellObject(0, 2), new CellObject(1, 1), new CellObject(2, 4),
            new CellObject(16, 2), new CellObject(17, 2) };
    checkResult(expectBeCells, expectAfCells, results);

    // ??
    if (version.equals("2007")) {
        int index = workbook.getSheetIndex(PoiUtil.TMP_SHEET_NAME);
        if (index > 0) {
            workbook.removeSheetAt(index);
        }
    }
    checkSheet("Sheet3", sheet3, true);

    // -----------------------
    // []?
    // ???
    // ????????
    // -----------------------
    workbook = getWorkbook();
    Sheet sheet4 = workbook.getSheetAt(3);
    // ??
    try {
        results = parseSheet(parser, sheet4, reportsParserInfo);
    } catch (ParseException e) {
        fail(e.toString());
    }

    expectBeCells = new CellObject[] { new CellObject(0, 0), new CellObject(1, 0), new CellObject(2, 0) };
    expectAfCells = new CellObject[] { new CellObject(0, 0), new CellObject(1, 0), new CellObject(2, 0) };
    checkResult(expectBeCells, expectAfCells, results);

    // ??
    if (version.equals("2007")) {
        int index = workbook.getSheetIndex(PoiUtil.TMP_SHEET_NAME);
        if (index > 0) {
            workbook.removeSheetAt(index);
        }
    }
    checkSheet("Sheet4", sheet4, true);

    Sheet sheet5 = workbook.getSheetAt(4);
    // ??
    try {
        results = parseSheet(parser, sheet5, reportsParserInfo);
        fail("?????????????????");
    } catch (ParseException e) {
    }

    // -----------------------
    // []?
    // ?????
    // -----------------------
    workbook = getWorkbook();
    Sheet sheet6 = workbook.getSheetAt(5);
    // ??
    try {
        results = parseSheet(parser, sheet6, reportsParserInfo);
        fail();
    } catch (ParseException e) {
        assertTrue(e instanceof ParseException);
    }

    // ------------------------------------------------------------
    // []
    // ??????????
    //   PoiUtil.getMergedAddress??
    //   ?????????
    // ------------------------------------------------------------
    workbook = getWorkbook();
    Sheet sheet7 = workbook.getSheetAt(6);
    // ??
    try {
        results = parseSheet(parser, sheet7, reportsParserInfo);
        fail("???");
    } catch (ParseException e) {
        // org.bbreak.excella.core.util.PoiUtil#getMergedAddress( Sheet sheet, CellRangeAddress rangeAddress)
        // ?throw????????
        assertTrue(e.getCause() instanceof IllegalArgumentException);
        assertTrue(e.getMessage().contains("There are crossing merged regions in the range."));
    }

    // ------------------------------------------------------------
    // []
    // ?????????
    // ------------------------------------------------------------
    workbook = getWorkbook();
    Sheet sheet8 = workbook.getSheetAt(7);
    // ??
    try {
        results = parseSheet(parser, sheet8, reportsParserInfo);
    } catch (ParseException e) {
        e.printStackTrace();
        fail(e.toString());
    }

    expectBeCells = new CellObject[] { new CellObject(1, 2), new CellObject(1, 12), new CellObject(1, 16),
            new CellObject(4, 2), new CellObject(5, 3), new CellObject(5, 14) };
    expectAfCells = new CellObject[] { new CellObject(1, 10), new CellObject(1, 14), new CellObject(1, 24),
            new CellObject(4, 6), new CellObject(5, 11), new CellObject(5, 22) };
    checkResult(expectBeCells, expectAfCells, results);

    // ??
    if (version.equals("2007")) {
        int index = workbook.getSheetIndex(PoiUtil.TMP_SHEET_NAME);
        if (index > 0) {
            workbook.removeSheetAt(index);
        }
    }
    checkSheet("Sheet8", sheet8, true);

    // ------------------------------------------------------------
    // []
    // ?????????
    // ------------------------------------------------------------
    workbook = getWorkbook();
    Sheet sheet9 = workbook.getSheetAt(8);
    // ??
    try {
        results = parseSheet(parser, sheet9, reportsParserInfo);
    } catch (ParseException e) {
        e.printStackTrace();
        fail(e.toString());
    }

    expectBeCells = new CellObject[] { new CellObject(1, 3), new CellObject(1, 19), new CellObject(1, 26),
            new CellObject(4, 3), new CellObject(4, 13) };
    expectAfCells = new CellObject[] { new CellObject(1, 15), new CellObject(1, 22), new CellObject(1, 38),
            new CellObject(4, 9), new CellObject(4, 19) };
    checkResult(expectBeCells, expectAfCells, results);

    // ??
    if (version.equals("2007")) {
        int index = workbook.getSheetIndex(PoiUtil.TMP_SHEET_NAME);
        if (index > 0) {
            workbook.removeSheetAt(index);
        }
    }
    checkSheet("Sheet9", sheet9, true);

    // ------------------------------------------------------------
    // []
    // ??????????
    //   PoiUtil.getMergedAddress??
    //   ?????????
    // ------------------------------------------------------------
    workbook = getWorkbook();
    Sheet sheet10 = workbook.getSheetAt(9);
    // ??
    results = null;
    try {
        results = parseSheet(parser, sheet10, reportsParserInfo);
        fail("???");
    } catch (ParseException e) {
        // org.bbreak.excella.core.util.PoiUtil#getMergedAddress( Sheet sheet, CellRangeAddress rangeAddress)
        // ?throw????????
        assertTrue(e.getCause() instanceof IllegalArgumentException);
        assertTrue(e.getMessage().contains("There are crossing merged regions in the range."));
    }

    // ------------------------------------------------------------
    // []
    // 
    // ------------------------------------------------------------
    workbook = getWorkbook();
    Sheet sheet15 = workbook.getSheetAt(14);
    // ??
    results = null;
    try {
        results = parseSheet(parser, sheet15, reportsParserInfo);
    } catch (ParseException e) {
        e.printStackTrace();
        fail(e.toString());
    }

    // ?=?=-1,=-1(???),=-1(??????)
    expectBeCells = new CellObject[] { new CellObject(1, 1), new CellObject(2, 2), new CellObject(3, 4),
            new CellObject(4, 4) };
    expectAfCells = new CellObject[] { new CellObject(1, 5), new CellObject(2, 5), new CellObject(3, 10),
            new CellObject(4, 7) };
    checkResult(expectBeCells, expectAfCells, results);

    // ??
    if (version.equals("2007")) {
        int index = workbook.getSheetIndex(PoiUtil.TMP_SHEET_NAME);
        if (index > 0) {
            workbook.removeSheetAt(index);
        }
    }

    checkSheet("Sheet15", sheet15, true);
}

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.  j a v  a 2s . 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;
}