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

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

Introduction

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

Prototype

int getActiveSheetIndex();

Source Link

Document

Convenience method to get the active sheet.

Usage

From source file:cn.edu.zjnu.acm.judge.util.excel.ExcelUtil.java

License:Apache License

private static <T> List<T> parse(Workbook workbook, FormulaEvaluator evaluator, Class<T> type, Locale locale) {
    MetaInfo metaInfo = MetaInfo.forType(type, locale);
    Sheet sheet = workbook.getSheetAt(workbook.getActiveSheetIndex());
    Iterator<Row> rows = sheet.rowIterator();
    if (!rows.hasNext()) {
        return Collections.emptyList();
    }/*www .j ava2 s . c  o  m*/
    Row firstRow = rows.next();
    Map<Integer, String> columnIndexToFieldName = Maps.newHashMapWithExpectedSize(metaInfo.size());
    for (Iterator<Cell> it = firstRow.cellIterator(); it.hasNext();) {
        Cell cell = it.next();
        JsonElement jsonElement = parseAsJsonElement(cell, evaluator);
        if (jsonElement != null) {
            Field field = metaInfo.getField(jsonElement.getAsString());
            if (field != null) {
                String name = field.getName();
                int index = cell.getColumnIndex();
                columnIndexToFieldName.put(index, name);
            }
        }
    }
    if (columnIndexToFieldName.isEmpty()) {
        return Collections.emptyList();
    }
    List<T> result = new ArrayList<>(sheet.getLastRowNum() - sheet.getFirstRowNum());
    while (rows.hasNext()) {
        result.add(parseRow(evaluator, rows.next(), columnIndexToFieldName, type));
    }
    return result;
}

From source file:cn.hanbell.war.control.InvmasImportManagedBean.java

@Override
public void handleFileUploadWhenNew(FileUploadEvent event) {
    super.handleFileUploadWhenNew(event);
    if (this.fileName != null) {
        InvmasImport e;//from ww w . j av  a  2s .  co m
        if (addedList != null) {
            addedList.clear();
        }
        try {
            InputStream is = new FileInputStream(getAppResPath() + "/" + fileName);
            Workbook wb = WorkbookFactory.create(is);
            Sheet sheet = wb.getSheetAt(wb.getActiveSheetIndex());
            Cell c;
            for (Row r : sheet) {
                if (r.getRowNum() == 0) {
                    continue;
                }
                e = new InvmasImport();
                c = r.getCell(0);
                e.setItcls(BaseLib.convertExcelCell(String.class, c).trim());
                c = r.getCell(1);
                e.setItnbr(BaseLib.convertExcelCell(String.class, c).trim());
                c = r.getCell(2);
                e.setItdsc(BaseLib.convertExcelCell(String.class, c).trim());
                c = r.getCell(3);
                e.setSpdsc(BaseLib.convertExcelCell(String.class, c).trim());
                c = r.getCell(4);
                e.setEitdsc(BaseLib.convertExcelCell(String.class, c).trim());
                c = r.getCell(5);
                e.setEspdsc(BaseLib.convertExcelCell(String.class, c).trim());
                c = r.getCell(6);
                e.setKind(BaseLib.convertExcelCell(String.class, c).trim());
                c = r.getCell(7);
                e.setUnmsr1(BaseLib.convertExcelCell(String.class, c).trim());
                c = r.getCell(8);
                e.setUsed(BaseLib.convertExcelCell(String.class, c).trim());
                e.setStatus("N");
                e.setCreator(userManagedBean.getCurrentUser().getUsername());
                e.setCredateToNow();
                addedList.add(e);
            }
            doAdd = true;
            showInfoMsg("Info", "?");
        } catch (IOException | InvalidFormatException ex) {
            showErrorMsg("Info", ",??");
            Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, ex);
        }
    }
}

From source file:com.vaadin.addon.spreadsheet.CellSelectionManager.java

License:Open Source License

/**
 * Reports the correct cell selection value (formula/data) and selection.
 * This method is called when the cell selection has changed via the address
 * field./*from  www  .  j  a  v  a 2 s  . c om*/
 * 
 * @param rowIndex
 *            Index of row, 1-based
 * @param columnIndex
 *            Index of column, 1-based
 */
private void handleCellAddressChange(int rowIndex, int colIndex, boolean initialSelection) {
    if (rowIndex >= spreadsheet.getState().rows) {
        rowIndex = spreadsheet.getState().rows;
    }
    if (colIndex >= spreadsheet.getState().cols) {
        colIndex = spreadsheet.getState().cols;
    }
    MergedRegion region = MergedRegionUtil.findIncreasingSelection(spreadsheet.getMergedRegionContainer(),
            rowIndex, rowIndex, colIndex, colIndex);
    if (region.col1 != region.col2 || region.row1 != region.row2) {
        handleCellRangeSelection(
                new CellRangeAddress(region.row1 - 1, region.row2 - 1, region.col1 - 1, region.col2 - 1));
    } else {
        rowIndex = region.row1;
        colIndex = region.col1;
        Workbook workbook = spreadsheet.getWorkbook();
        final Row row = workbook.getSheetAt(workbook.getActiveSheetIndex()).getRow(rowIndex - 1);
        if (row != null) {
            final Cell cell = row.getCell(colIndex - 1);
            if (cell != null) {
                String value = "";
                boolean formula = cell.getCellType() == Cell.CELL_TYPE_FORMULA;
                if (!spreadsheet.isCellHidden(cell)) {
                    if (formula) {
                        value = cell.getCellFormula();
                    } else {
                        value = spreadsheet.getCellValue(cell);
                    }
                }
                spreadsheet.getRpcProxy().showSelectedCell(colIndex, rowIndex, value, formula,
                        spreadsheet.isCellLocked(cell), initialSelection);
            } else {
                spreadsheet.getRpcProxy().showSelectedCell(colIndex, rowIndex, "", false,
                        spreadsheet.isCellLocked(cell), initialSelection);
            }
        } else {
            spreadsheet.getRpcProxy().showSelectedCell(colIndex, rowIndex, "", false,
                    spreadsheet.isActiveSheetProtected(), initialSelection);
        }
    }
}

From source file:com.vaadin.addon.spreadsheet.CellSelectionManager.java

License:Open Source License

/**
 * Reports the selected cell formula value, if any. This method is called
 * when the cell value has changed via sheet cell selection change.
 * //w ww .  j a v a  2  s .c o m
 * This method can also be used when the selected cell has NOT changed but
 * the value it displays on the formula field might have changed and needs
 * to be updated.
 * 
 * @param rowIndex
 *            1-based
 * @param columnIndex
 *            1-based
 */
private void handleCellSelection(int rowIndex, int columnIndex) {
    Workbook workbook = spreadsheet.getWorkbook();
    final Row row = workbook.getSheetAt(workbook.getActiveSheetIndex()).getRow(rowIndex - 1);
    if (row != null) {
        final Cell cell = row.getCell(columnIndex - 1);
        if (cell != null) {
            String value = "";
            boolean formula = cell.getCellType() == Cell.CELL_TYPE_FORMULA;
            if (!spreadsheet.isCellHidden(cell)) {
                if (formula) {
                    value = cell.getCellFormula();
                } else {
                    value = spreadsheet.getCellValue(cell);
                }
            }
            spreadsheet.getRpcProxy().showCellValue(value, columnIndex, rowIndex, formula,
                    spreadsheet.isCellLocked(cell));
        } else {
            spreadsheet.getRpcProxy().showCellValue("", columnIndex, rowIndex, false,
                    spreadsheet.isCellLocked(cell));
        }
    } else {
        spreadsheet.getRpcProxy().showCellValue("", columnIndex, rowIndex, false,
                spreadsheet.isActiveSheetProtected());
    }
}

From source file:com.vaadin.addon.spreadsheet.CellSelectionManager.java

License:Open Source License

/**
 * Handles the new cell range that was given in the address field, returns
 * the range and new selected cell formula/value (if any)
 * /*from  ww  w . ja v  a2s. co m*/
 * @param cra
 *            Range of cells to select
 */
protected void handleCellRangeSelection(CellRangeAddress cra) {
    int row1 = cra.getFirstRow();
    int row2 = cra.getLastRow();
    int col1 = cra.getFirstColumn();
    int col2 = cra.getLastColumn();
    Workbook workbook = spreadsheet.getWorkbook();
    final Row row = workbook.getSheetAt(workbook.getActiveSheetIndex()).getRow(row1);
    if (row != null) {
        final Cell cell = row.getCell(col1);
        if (cell != null) {
            String value = "";
            boolean formula = cell.getCellType() == Cell.CELL_TYPE_FORMULA;
            if (!spreadsheet.isCellHidden(cell)) {
                if (formula) {
                    value = cell.getCellFormula();
                } else {
                    value = spreadsheet.getCellValue(cell);
                }
            }
            spreadsheet.getRpcProxy().showSelectedCellRange(col1 + 1, col2 + 1, row1 + 1, row2 + 1, value,
                    formula, spreadsheet.isCellLocked(cell));
        } else {
            spreadsheet.getRpcProxy().showSelectedCellRange(col1 + 1, col2 + 1, row1 + 1, row2 + 1, "", false,
                    spreadsheet.isCellLocked(cell));
        }
    } else {
        spreadsheet.getRpcProxy().showSelectedCellRange(col1 + 1, col2 + 1, row1 + 1, row2 + 1, "", false,
                spreadsheet.isActiveSheetProtected());
    }
}

From source file:com.vaadin.addon.spreadsheet.CellSelectionManager.java

License:Open Source License

/**
 * Sets the given range and starting point as the current selection.
 * //from ww  w . j ava 2 s.  c  om
 * @param startingPoint
 *            Reference to starting point
 * @param cellsToSelect
 *            Selection area
 */
protected void handleCellRangeSelection(CellReference startingPoint, CellRangeAddress cellsToSelect,
        boolean scroll) {
    int row1 = cellsToSelect.getFirstRow();
    int row2 = cellsToSelect.getLastRow();
    int col1 = cellsToSelect.getFirstColumn();
    int col2 = cellsToSelect.getLastColumn();
    Workbook workbook = spreadsheet.getWorkbook();
    final Row row = workbook.getSheetAt(workbook.getActiveSheetIndex()).getRow(startingPoint.getRow());
    if (row != null) {
        final Cell cell = row.getCell(startingPoint.getCol());
        if (cell != null) {
            String value = "";
            boolean formula = cell.getCellType() == Cell.CELL_TYPE_FORMULA;
            if (!spreadsheet.isCellHidden(cell)) {
                if (formula) {
                    value = cell.getCellFormula();
                } else {
                    value = spreadsheet.getCellValue(cell);
                }
            }
            spreadsheet.getRpcProxy().setSelectedCellAndRange(startingPoint.getCol() + 1,
                    startingPoint.getRow() + 1, col1 + 1, col2 + 1, row1 + 1, row2 + 1, value, formula,
                    spreadsheet.isCellLocked(cell), scroll);
        } else {
            spreadsheet.getRpcProxy().setSelectedCellAndRange(startingPoint.getCol() + 1,
                    startingPoint.getRow() + 1, col1 + 1, col2 + 1, row1 + 1, row2 + 1, "", false,
                    spreadsheet.isCellLocked(cell), scroll);
        }
    } else {
        spreadsheet.getRpcProxy().setSelectedCellAndRange(startingPoint.getCol() + 1,
                startingPoint.getRow() + 1, col1 + 1, col2 + 1, row1 + 1, row2 + 1, "", false,
                spreadsheet.isActiveSheetProtected(), scroll);
    }
    selectedCellReference = startingPoint;
    cellRangeAddresses.clear();
    individualSelectedCells.clear();
    paintedCellRange = cellsToSelect;
    if (col1 != col2 || row1 != row2) {
        cellRangeAddresses.add(cellsToSelect);
    }
    ensureClientHasSelectionData();
    fireNewSelectionChangeEvent();
}

From source file:com.vaadin.addon.spreadsheet.CellSelectionShifter.java

License:Open Source License

private void shiftRowsDownInSelection(int newLastRow) {
    CellRangeAddress paintedCellRange = spreadsheet.getCellSelectionManager().getSelectedCellRange();
    int r1 = paintedCellRange.getFirstRow() + 1;
    int r2 = paintedCellRange.getLastRow() + 1;
    int c1 = paintedCellRange.getFirstColumn() + 1;
    int c2 = paintedCellRange.getLastColumn() + 1;
    Workbook workbook = spreadsheet.getWorkbook();
    final Sheet activeSheet = workbook.getSheetAt(workbook.getActiveSheetIndex());
    for (int shiftedRowIndex = r1; shiftedRowIndex <= r2; shiftedRowIndex++) {
        final Row shiftedRow = activeSheet.getRow(shiftedRowIndex - 1);
        int newRowIndex = r2 + 1 + (shiftedRowIndex - r1);
        while (newRowIndex <= newLastRow) {
            if (shiftedRow != null) {
                Row newRow = activeSheet.getRow(newRowIndex - 1);
                if (newRow == null) {
                    newRow = activeSheet.createRow(newRowIndex - 1);
                }//from  w ww  .  j a va  2  s.c o  m
                for (int c = c1; c <= c2; c++) {
                    Double sequenceIncrement = getColumnSequenceIncrement(c, r1, r2);
                    Cell shiftedCell = shiftedRow.getCell(c - 1);
                    Cell newCell = newRow.getCell(c - 1);
                    if (shiftedCell != null) {
                        if (newCell == null) {
                            newCell = newRow.createCell(c - 1);
                        }
                        shiftCellValue(shiftedCell, newCell, false, sequenceIncrement);
                    } else if (newCell != null) {
                        // update style to 0
                        newCell.setCellStyle(null);
                        spreadsheet.getSpreadsheetStyleFactory().cellStyleUpdated(newCell, true);
                        newCell.setCellValue((String) null);
                        getCellValueManager().cellDeleted(newCell);
                    }
                }
            } else {
                getCellValueManager().removeCells(newRowIndex, c1, newRowIndex, c2, true);
            }
            newRowIndex += r2 - r1 + 1;
        }
    }
}

From source file:com.vaadin.addon.spreadsheet.CellSelectionShifter.java

License:Open Source License

private void shiftRowsUpInSelection(int newFirstRow) {
    CellRangeAddress paintedCellRange = spreadsheet.getCellSelectionManager().getSelectedCellRange();
    int r1 = paintedCellRange.getFirstRow() + 1;
    int r2 = paintedCellRange.getLastRow() + 1;
    int c1 = paintedCellRange.getFirstColumn() + 1;
    int c2 = paintedCellRange.getLastColumn() + 1;
    Workbook workbook = spreadsheet.getWorkbook();
    final Sheet activeSheet = workbook.getSheetAt(workbook.getActiveSheetIndex());
    for (int shiftedRowIndex = r1; shiftedRowIndex <= r2; shiftedRowIndex++) {
        final Row shiftedRow = activeSheet.getRow(shiftedRowIndex - 1);
        int newRowIndex = r1 - 1 - (shiftedRowIndex - r1);
        while (newRowIndex >= newFirstRow) {
            if (shiftedRow != null) {
                Row newRow = activeSheet.getRow(newRowIndex - 1);
                if (newRow == null) {
                    newRow = activeSheet.createRow(newRowIndex - 1);
                }//from w w w. j a  va 2s.  co m
                for (int c = c1; c <= c2; c++) {
                    Double sequenceIncrement = getColumnSequenceIncrement(c, r1, r2);
                    Cell shiftedCell = shiftedRow.getCell(c - 1);
                    Cell newCell = newRow.getCell(c - 1);
                    if (shiftedCell != null) {
                        if (newCell == null) {
                            newCell = newRow.createCell(c - 1);
                        }
                        shiftCellValue(shiftedCell, newCell, false, sequenceIncrement);
                    } else if (newCell != null) {
                        // update style to 0
                        newCell.setCellStyle(null);
                        spreadsheet.getSpreadsheetStyleFactory().cellStyleUpdated(newCell, true);
                        newCell.setCellValue((String) null);
                        getCellValueManager().cellDeleted(newCell);
                    }
                }
            } else {
                getCellValueManager().removeCells(newRowIndex, c1, newRowIndex, c2, true);
            }
            newRowIndex = newRowIndex - (r2 - r1) - 1;
        }
    }
}

From source file:com.vaadin.addon.spreadsheet.CellSelectionShifter.java

License:Open Source License

private void shiftColumnsRightInSelection(int newRightMostColumn) {
    CellRangeAddress paintedCellRange = spreadsheet.getCellSelectionManager().getSelectedCellRange();
    int r1 = paintedCellRange.getFirstRow() + 1;
    int r2 = paintedCellRange.getLastRow() + 1;
    int c1 = paintedCellRange.getFirstColumn() + 1;
    int c2 = paintedCellRange.getLastColumn() + 1;
    Workbook workbook = spreadsheet.getWorkbook();
    final Sheet activeSheet = workbook.getSheetAt(workbook.getActiveSheetIndex());
    for (int rIndex = r1; rIndex <= r2; rIndex++) {
        final Row row = activeSheet.getRow(rIndex - 1);
        if (row != null) {
            Double sequenceIncrement = getRowSequenceIncrement(rIndex, c1, c2);
            for (int shiftedCellIndex = c1; shiftedCellIndex <= c2; shiftedCellIndex++) {
                Cell shiftedCell = row.getCell(shiftedCellIndex - 1);
                int newCellIndex = c2 + 1 + (shiftedCellIndex - c1);
                while (newCellIndex <= newRightMostColumn) {
                    Cell newCell = row.getCell(newCellIndex - 1);
                    if (shiftedCell != null) {
                        if (newCell == null) {
                            newCell = row.createCell(newCellIndex - 1);
                        }/*from w  w w  .ja v a2  s .  co m*/
                        shiftCellValue(shiftedCell, newCell, false, sequenceIncrement);

                    } else if (newCell != null) {
                        newCell.setCellValue((String) null);
                        getCellValueManager().cellDeleted(newCell);
                        // update style to 0
                        newCell.setCellStyle(null);
                        spreadsheet.getSpreadsheetStyleFactory().cellStyleUpdated(newCell, true);
                    }
                    newCellIndex += (c2 - c1) + 1;
                }
            }
        }
    }
}

From source file:com.vaadin.addon.spreadsheet.CellSelectionShifter.java

License:Open Source License

private void shiftColumnsLeftInSelection(int newLeftMostColumn) {
    CellRangeAddress paintedCellRange = spreadsheet.getCellSelectionManager().getSelectedCellRange();
    int r1 = paintedCellRange.getFirstRow() + 1;
    int r2 = paintedCellRange.getLastRow() + 1;
    int c1 = paintedCellRange.getFirstColumn() + 1;
    int c2 = paintedCellRange.getLastColumn() + 1;
    Workbook workbook = spreadsheet.getWorkbook();
    final Sheet activeSheet = workbook.getSheetAt(workbook.getActiveSheetIndex());
    for (int rIndex = r1; rIndex <= r2; rIndex++) {
        final Row row = activeSheet.getRow(rIndex - 1);
        if (row != null) {
            Double sequenceIncrement = getRowSequenceIncrement(rIndex, c1, c2);
            for (int shiftedCellIndex = c1; shiftedCellIndex <= c2; shiftedCellIndex++) {
                Cell shiftedCell = row.getCell(shiftedCellIndex - 1);
                int newCellIndex = c1 - (shiftedCellIndex - c1) - 1;
                while (newCellIndex >= newLeftMostColumn) {
                    Cell newCell = row.getCell(newCellIndex - 1);
                    if (shiftedCell != null) {
                        if (newCell == null) {
                            newCell = row.createCell(newCellIndex - 1);
                        }//from   www . jav  a2 s  . c  o  m
                        shiftCellValue(shiftedCell, newCell, false, sequenceIncrement);
                    } else if (newCell != null) {
                        newCell.setCellValue((String) null);
                        getCellValueManager().cellDeleted(newCell);
                        // update style to 0
                        newCell.setCellStyle(null);
                        spreadsheet.getSpreadsheetStyleFactory().cellStyleUpdated(newCell, true);
                    }
                    newCellIndex = newCellIndex - (c2 - c1) - 1;
                }
            }
        }
    }
}