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

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

Introduction

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

Prototype

void removeSheetAt(int index);

Source Link

Document

Removes sheet at the given index

Usage

From source file:info.informationsea.java.excel2csv.Utilities.java

License:Open Source License

public static Sheet createUniqueNameSheetForWorkbook(Workbook workbook, String sheetName, boolean overwrite) {
    if (overwrite) {
        int index = workbook.getSheetIndex(workbook.getSheet(sheetName));
        if (index >= 0)
            workbook.removeSheetAt(index);
        return workbook.createSheet(sheetName);
    }/* w  w  w .  j  a v a2 s. com*/

    String realSheetName = sheetName;
    int index = 1;
    Sheet sheet;
    while (true) {
        try {
            sheet = workbook.createSheet(realSheetName);
            break;
        } catch (IllegalArgumentException e) {
            realSheetName = sheetName + "-" + index++;
            if (index > 20) {
                throw e;
            }
        }
    }
    return sheet;
}

From source file:net.sf.excelutils.ExcelUtils.java

License:Apache License

/**
 * parse Workbook/*from   w  ww  .j a v a2s .  co m*/
 * 
 * @param context
 * @param wb
 * @param sheetIndex
 * @throws ExcelException
 */
public static void parseWorkbook(Object context, Workbook wb, int sheetIndex) throws ExcelException {
    try {
        Sheet sheet = wb.getSheetAt(sheetIndex);
        if (null != sheet) {
            parseSheet(context, wb, sheet);
            // set print area
            WorkbookUtils.setPrintArea(wb, sheetIndex);
        }

        int i = 0;
        while (i++ < sheetIndex) {
            wb.removeSheetAt(0);
        }

        i = 1;
        while (i < wb.getNumberOfSheets()) {
            wb.removeSheetAt(i);
        }
    } catch (Exception e) {
        throw new ExcelException("parseWorkbook error: ", e);
    }
}

From source file:org.alanwilliamson.openbd.plugin.spreadsheet.tags.cfSpreadSheetWrite.java

License:Open Source License

protected void writeQueryToSheet(cfQueryResultData queryData, cfSpreadSheetData spreadsheet, String sheetName)
        throws dataNotSupportedException {
    Workbook workbook = spreadsheet.getWorkBook();

    if (workbook.getSheet(sheetName) != null)
        workbook.removeSheetAt(workbook.getSheetIndex(sheetName));

    Sheet sheet = workbook.createSheet(sheetName);

    //WRITE THE SHEET: 1st row to be the columns
    String[] columnList = queryData.getColumnList();
    Row row = sheet.createRow(0);/*www. jav a  2 s  .  c  om*/
    Cell cell;
    for (int c = 0; c < columnList.length; c++) {
        cell = row.createCell(c, Cell.CELL_TYPE_STRING);
        cell.setCellValue(columnList[c]);
    }

    //WRITE THE SHEET: Write out all the rows
    int rowsToInsert = queryData.getSize();
    for (int x = 0; x < rowsToInsert; x++) {
        row = sheet.createRow(x + 1);

        for (int c = 0; c < columnList.length; c++) {
            cell = row.createCell(c);

            cfData value = queryData.getCell(x + 1, c + 1);

            if (value.getDataType() == cfData.CFNUMBERDATA) {
                cell.setCellValue(value.getDouble());
                cell.setCellType(Cell.CELL_TYPE_NUMERIC);
            } else if (value.getDataType() == cfData.CFDATEDATA) {
                cell.setCellValue(new Date(value.getDateLong()));
            } else if (value.getDataType() == cfData.CFBOOLEANDATA) {
                cell.setCellValue(value.getBoolean());
                cell.setCellType(Cell.CELL_TYPE_BOOLEAN);
            } else {
                cell.setCellValue(value.getString());
                cell.setCellType(Cell.CELL_TYPE_STRING);
            }
        }

    }
}

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

License:Open Source License

/**
 * ?/*from w  ww . j av  a2  s.c o m*/
 * 
 * @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.exporter.ReportBookExporter.java

License:Open Source License

public void export(Workbook book, BookData bookdata) throws ExportException {
    // ()/*from w w  w .  j  av a2 s.c  om*/
    SortedSet<Integer> deleteSheetIndexs = new TreeSet<Integer>(Collections.reverseOrder());
    for (int i = 0; i < book.getNumberOfSheets(); i++) {
        String sheetName = book.getSheetName(i);
        if (sheetName.startsWith(PoiUtil.TMP_SHEET_NAME)) {
            deleteSheetIndexs.add(i);
        }
    }
    for (int index : deleteSheetIndexs) {
        book.removeSheetAt(index);
    }

    if (configuration != null) {
        // 
        output(book, bookdata, configuration);
    }

}

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

License:Open Source License

/**
 * ????// w  w w  .  j av  a 2s  . 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.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  www  .j  ava  2 s.co  m*/
    // -----------------------

    //?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  w  w  .j a 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.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)} ????
 *//*from ww w . j  a  v  a 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.ImageParamParserTest.java

License:Open Source License

/**
 * {@link org.bbreak.excella.reports.tag.ImageParamParser#parse(org.apache.poi.ss.usermodel.Sheet, org.apache.poi.ss.usermodel.Cell, java.lang.Object)} ????
 *//*from w  w  w .j  a v a2s .  c  o m*/
@Test
public void testParseSheetCellObject() {
    // -----------------------
    // []?
    // -----------------------
    Workbook workbook = getWorkbook();

    Sheet sheet1 = workbook.getSheetAt(0);

    ImageParamParser parser = new ImageParamParser();

    ReportsParserInfo reportsParserInfo = new ReportsParserInfo();
    reportsParserInfo.setParamInfo(createTestData(ImageParamParser.DEFAULT_TAG));

    try {
        parseSheet(parser, sheet1, reportsParserInfo);
    } catch (ParseException e) {
        fail(e.toString());
    }

    // ???
    Set<Integer> delSheetIndexs = new TreeSet<Integer>(Collections.reverseOrder());
    for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
        if (i != 0) {
            delSheetIndexs.add(i);
        }
    }
    for (Integer i : delSheetIndexs) {
        workbook.removeSheetAt(i);
    }

    // ???
    Workbook expectedWorkbook = getExpectedWorkbook();
    Sheet expectedSheet = expectedWorkbook.getSheet("Sheet1");

    try {
        // ?
        ReportsTestUtil.checkSheet(expectedSheet, sheet1, false);
    } catch (ReportsCheckException e) {
        fail(e.getCheckMessagesToString());

    } finally {
        // ?????????????
        String tmpDirPath = System.getProperty("user.dir") + "/work/test/"; // System.getProperty( "java.io.tmpdir");
        File file = new File(tmpDirPath);
        if (!file.exists()) {
            file.mkdirs();
        }

        try {
            String filepath = null;
            if (version.equals("2007")) {
                filepath = tmpDirPath + "ImageParamParserTest1.xlsx";
            } else {
                filepath = tmpDirPath + "ImageParamParserTest1.xls";
            }
            PoiUtil.writeBook(workbook, filepath);
            log.info("????????" + filepath);

        } catch (IOException e) {
            fail(e.toString());
        }
    }

    // -----------------------
    // []
    // -----------------------
    workbook = getWorkbook();

    Sheet sheet2 = workbook.getSheetAt(1);

    parser = new ImageParamParser("$Image");

    reportsParserInfo = new ReportsParserInfo();
    reportsParserInfo.setParamInfo(createTestData("$Image"));

    try {
        parseSheet(parser, sheet2, reportsParserInfo);
    } catch (ParseException e) {
        fail(e.toString());
    }

    // ???
    delSheetIndexs.clear();
    for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
        if (i != 1) {
            delSheetIndexs.add(i);
        }
    }
    for (Integer i : delSheetIndexs) {
        workbook.removeSheetAt(i);
    }

    // ???
    expectedWorkbook = getExpectedWorkbook();
    expectedSheet = expectedWorkbook.getSheet("Sheet2");

    try {
        // ?
        ReportsTestUtil.checkSheet(expectedSheet, sheet2, false);
    } catch (ReportsCheckException e) {
        fail(e.getCheckMessagesToString());
    } finally {
        // ?????????????
        String tmpDirPath = System.getProperty("user.dir") + "/work/test/"; // System.getProperty( "java.io.tmpdir");;
        File file = new File(tmpDirPath);
        if (!file.exists()) {
            file.mkdir();
        }
        try {
            String filepath = null;
            if (version.equals("2007")) {
                filepath = tmpDirPath + "ImageParamParserTest2.xlsx";
            } else {
                filepath = tmpDirPath + "ImageParamParserTest2.xls";
            }
            PoiUtil.writeBook(workbook, filepath);
            log.info("????????" + filepath);

        } catch (IOException e) {
            fail(e.toString());
        }
    }

    String filename = this.getClass().getSimpleName();
    if (version.equals("2007")) {
        filename = filename + "_err.xlsx";
    } else if (version.equals("2003")) {
        filename = filename + "_err.xls";
    }

    URL url = this.getClass().getResource(filename);
    String path = null;
    try {
        path = URLDecoder.decode(url.getFile(), "UTF-8");

    } catch (UnsupportedEncodingException e) {
        Assert.fail();
    }

    // -----------------------
    // []?
    // -----------------------
    workbook = getWorkbook(path);

    Sheet sheet3 = workbook.getSheetAt(2);

    parser = new ImageParamParser();

    reportsParserInfo = new ReportsParserInfo();
    reportsParserInfo.setParamInfo(createTestData(ImageParamParser.DEFAULT_TAG));

    try {
        parseSheet(parser, sheet3, reportsParserInfo);
        fail("?????????");
    } catch (ParseException e) {
    }

    // -----------------------
    // []????
    // -----------------------
    workbook = getWorkbook();

    Sheet sheet4 = workbook.getSheetAt(2);

    parser = new ImageParamParser();

    reportsParserInfo = new ReportsParserInfo();
    reportsParserInfo.setParamInfo(createTestData(ImageParamParser.DEFAULT_TAG));

    try {
        parseSheet(parser, sheet4, reportsParserInfo);
    } catch (ParseException e) {
        fail(e.toString());
    }

    // ???
    expectedWorkbook = getExpectedWorkbook();
    expectedSheet = expectedWorkbook.getSheet("Sheet4");

    try {
        // ?
        ReportsTestUtil.checkSheet(expectedSheet, sheet4, false);
    } catch (ReportsCheckException e) {
        fail(e.getCheckMessagesToString());
    }

    // ----------------------------------------------------------------------
    // []1 / ?? / ???
    // ----------------------------------------------------------------------
    workbook = getWorkbook();

    Sheet sheet5 = workbook.getSheetAt(INDEX_OF_SHEET5);

    parser = new ImageParamParser();

    reportsParserInfo = new ReportsParserInfo();
    reportsParserInfo.setParamInfo(createPluralTestData(ImageParamParser.DEFAULT_TAG));

    try {
        parseSheet(parser, sheet5, reportsParserInfo);
    } catch (ParseException e) {
        fail(e.toString());
    }

    // ???
    delSheetIndexs.clear();
    for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
        if (i != INDEX_OF_SHEET5) {
            delSheetIndexs.add(i);
        }
    }
    for (Integer i : delSheetIndexs) {
        workbook.removeSheetAt(i);
    }

    // ???
    expectedWorkbook = getExpectedWorkbook();
    expectedSheet = expectedWorkbook.getSheet("Sheet5");

    try {
        // ?
        ReportsTestUtil.checkSheet(expectedSheet, sheet5, false);
    } catch (ReportsCheckException e) {
        fail(e.getCheckMessagesToString());
    } finally {
        // ?????????????
        String tmpDirPath = System.getProperty("user.dir") + "/work/test/"; // System.getProperty( "java.io.tmpdir");
        File file = new File(tmpDirPath);
        if (!file.exists()) {
            file.mkdirs();
        }

        try {
            String filepath = null;
            if (version.equals("2007")) {
                filepath = tmpDirPath + "ImageParamParserTest3.xlsx";
            } else {
                filepath = tmpDirPath + "ImageParamParserTest3.xls";
            }
            PoiUtil.writeBook(workbook, filepath);
            log.info("????????" + filepath);

        } catch (IOException e) {
            fail(e.toString());
        }
    }

    // ----------------------------------------------------------------------
    // []1 / ?? / ?1
    // ----------------------------------------------------------------------

    workbook = getWorkbook();

    Sheet sheet6 = workbook.cloneSheet(INDEX_OF_SHEET5);

    parser = new ImageParamParser();

    reportsParserInfo = new ReportsParserInfo();
    reportsParserInfo.setParamInfo(createPluralTestData(ImageParamParser.DEFAULT_TAG));

    try {
        parseSheet(parser, sheet6, reportsParserInfo);
    } catch (ParseException e) {
        fail(e.toString());
    }

    // ???
    delSheetIndexs.clear();
    for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
        if (i != workbook.getNumberOfSheets() - 1) {
            delSheetIndexs.add(i);
        }
    }
    for (Integer i : delSheetIndexs) {
        workbook.removeSheetAt(i);
    }

    // ???
    expectedWorkbook = getExpectedWorkbook();
    expectedSheet = expectedWorkbook.cloneSheet(INDEX_OF_SHEET5);

    try {
        // ?
        ReportsTestUtil.checkSheet(expectedSheet, sheet6, false);
    } catch (ReportsCheckException e) {
        fail(e.getCheckMessagesToString());
    } finally {
        // ?????????????
        String tmpDirPath = System.getProperty("user.dir") + "/work/test/"; // System.getProperty( "java.io.tmpdir");
        File file = new File(tmpDirPath);
        if (!file.exists()) {
            file.mkdirs();
        }

        try {
            String filepath = null;
            if (version.equals("2007")) {
                filepath = tmpDirPath + "ImageParamParserTest4.xlsx";
            } else {
                filepath = tmpDirPath + "ImageParamParserTest4.xls";
            }
            PoiUtil.writeBook(workbook, filepath);
            log.info("????????" + filepath);

        } catch (IOException e) {
            fail(e.toString());
        }
    }

    // ----------------------------------------------------------------------
    // []  / ?? / ?
    // ----------------------------------------------------------------------

    workbook = getWorkbook();

    for (int i = 1; i <= PLURAL_COPY_FIRST_NUM_OF_SHEETS; i++) {

        Sheet sheet = workbook.cloneSheet(INDEX_OF_SHEET5);

        parser = new ImageParamParser();

        reportsParserInfo = new ReportsParserInfo();
        reportsParserInfo.setParamInfo(createPluralTestData(ImageParamParser.DEFAULT_TAG));

        try {
            parseSheet(parser, sheet, reportsParserInfo);
        } catch (ParseException e) {
            fail(e.toString());
        }
    }

    // ???
    delSheetIndexs.clear();
    for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
        if (i < workbook.getNumberOfSheets() - PLURAL_COPY_FIRST_NUM_OF_SHEETS) {
            delSheetIndexs.add(i);
        }
    }
    for (Integer i : delSheetIndexs) {
        workbook.removeSheetAt(i);
    }

    // ???
    expectedWorkbook = getExpectedWorkbook();
    for (int i = 1; i <= PLURAL_COPY_FIRST_NUM_OF_SHEETS; i++) {
        expectedSheet = expectedWorkbook.cloneSheet(INDEX_OF_SHEET5);
    }

    try {
        int startOfTargetSheet = expectedWorkbook.getNumberOfSheets() - PLURAL_COPY_FIRST_NUM_OF_SHEETS;

        for (int i = 0; i < PLURAL_COPY_FIRST_NUM_OF_SHEETS; i++) {
            // ?
            ReportsTestUtil.checkSheet(expectedWorkbook.getSheetAt(startOfTargetSheet + i),
                    workbook.getSheetAt(i), false);
        }
    } catch (ReportsCheckException e) {
        fail(e.getCheckMessagesToString());
    } finally {
        // ?????????????
        String tmpDirPath = System.getProperty("user.dir") + "/work/test/"; // System.getProperty( "java.io.tmpdir");
        File file = new File(tmpDirPath);
        if (!file.exists()) {
            file.mkdirs();
        }

        try {
            String filepath = null;
            if (version.equals("2007")) {
                filepath = tmpDirPath + "ImageParamParserTest5.xlsx";
            } else {
                filepath = tmpDirPath + "ImageParamParserTest5.xls";
            }
            PoiUtil.writeBook(workbook, filepath);
            log.info("????????" + filepath);

        } catch (IOException e) {
            fail(e.toString());
        }

    }

    // ----------------------------------------------------------------------
    // []  / ?? / (2)?
    // ----------------------------------------------------------------------

    workbook = getWorkbook();

    Sheet sheet = null;
    int totalNumOfCopies = PLURAL_COPY_FIRST_NUM_OF_SHEETS + PLURAL_COPY_SECOND_NUM_OF_SHEETS;
    for (int i = 1; i <= totalNumOfCopies; i++) {

        if (i <= PLURAL_COPY_FIRST_NUM_OF_SHEETS) {
            sheet = workbook.cloneSheet(INDEX_OF_SHEET5);
        } else {
            sheet = workbook.cloneSheet(INDEX_OF_SHEET6);
        }

        parser = new ImageParamParser();

        reportsParserInfo = new ReportsParserInfo();
        reportsParserInfo.setParamInfo(createPluralTestData(ImageParamParser.DEFAULT_TAG));

        try {
            parseSheet(parser, sheet, reportsParserInfo);
        } catch (ParseException e) {
            fail(e.toString());
        }
    }

    // ???
    delSheetIndexs.clear();
    for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
        if (i < workbook.getNumberOfSheets() - totalNumOfCopies) {
            delSheetIndexs.add(i);
        }
    }
    for (Integer i : delSheetIndexs) {
        workbook.removeSheetAt(i);
    }

    // ???
    expectedWorkbook = getExpectedWorkbook();
    for (int i = 1; i <= totalNumOfCopies; i++) {
        if (i <= PLURAL_COPY_FIRST_NUM_OF_SHEETS) {
            expectedSheet = expectedWorkbook.cloneSheet(INDEX_OF_SHEET5);
        } else {
            expectedSheet = expectedWorkbook.cloneSheet(INDEX_OF_SHEET6);
        }
    }

    try {
        int startOfTargetSheet = expectedWorkbook.getNumberOfSheets() - totalNumOfCopies;

        for (int i = 0; i < totalNumOfCopies; i++) {
            // ?
            ReportsTestUtil.checkSheet(expectedWorkbook.getSheetAt(startOfTargetSheet + i),
                    workbook.getSheetAt(i), false);
        }
    } catch (ReportsCheckException e) {
        fail(e.getCheckMessagesToString());
    } finally {
        // ?????????????
        String tmpDirPath = System.getProperty("user.dir") + "/work/test/"; // System.getProperty( "java.io.tmpdir");
        File file = new File(tmpDirPath);
        if (!file.exists()) {
            file.mkdirs();
        }

        try {
            String filepath = null;
            if (version.equals("2007")) {
                filepath = tmpDirPath + "ImageParamParserTest6.xlsx";
            } else {
                filepath = tmpDirPath + "ImageParamParserTest6.xls";
            }
            PoiUtil.writeBook(workbook, filepath);
            log.info("????????" + filepath);

        } catch (IOException e) {
            fail(e.toString());
        }
    }

}