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:com.vaadin.addon.spreadsheet.SpreadsheetFactory.java

License:Open Source License

/**
 * Reloads the Spreadsheet component using the given Workbook as data
 * source.//w w  w  .  j  av a2 s. co  m
 *
 * @param spreadsheet
 *            Target Spreadsheet
 * @param workbook
 *            Source Workbook
 */
static void reloadSpreadsheetComponent(Spreadsheet spreadsheet, final Workbook workbook) {
    Workbook oldWorkbook = spreadsheet.getWorkbook();
    if (oldWorkbook != null) {
        spreadsheet.clearSheetServerSide();
        if (oldWorkbook instanceof SXSSFWorkbook) {
            ((SXSSFWorkbook) oldWorkbook).dispose();
        }
    }
    final Sheet sheet = workbook.getSheetAt(workbook.getActiveSheetIndex());
    spreadsheet.setInternalWorkbook(workbook);
    reloadSpreadsheetData(spreadsheet, sheet);
    loadWorkbookStyles(spreadsheet);
}

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

@Override
public void onPaste(String text) {
    Workbook workbook = spreadsheet.getWorkbook();
    Sheet activesheet = workbook.getSheetAt(workbook.getActiveSheetIndex());

    CellReference selectedCellReference = spreadsheet.getSelectedCellReference();

    String[] lines;/*from w  w  w.jav  a2 s  .  c o  m*/
    if (text.indexOf("\r\n") > -1) {
        lines = text.split("\r\n");
    } else if (text.indexOf("\n") > -1) {
        lines = text.split("\n");
    } else {
        lines = text.split("\r");
    }

    int pasteHeight = lines.length;
    int pasteWidth = 1;
    for (String line : lines) {
        String[] tokens = splitOnTab(line);
        pasteWidth = Math.max(pasteWidth, tokens.length);
    }

    int rowIndex = selectedCellReference.getRow();
    int colIndex = selectedCellReference.getCol();

    // Check for protected cells at target
    for (int i = 0; i < pasteHeight; i++) {
        Row row = activesheet.getRow(rowIndex + i);
        if (row != null) {
            for (int j = 0; j < pasteWidth; j++) {
                Cell cell = row.getCell(colIndex + j);
                if (spreadsheet.isCellLocked(cell)) {
                    protectedCellWriteAttempted();
                    return;
                }
            }
        }
    }

    CellValueCommand command = new CellValueCommand(spreadsheet);
    CellRangeAddress affectedRange = new CellRangeAddress(rowIndex, rowIndex + pasteHeight - 1, colIndex,
            colIndex + pasteWidth - 1);
    command.captureCellRangeValues(affectedRange);

    for (int i = 0; i < pasteHeight; i++) {
        String line = lines[i];
        Row row = activesheet.getRow(rowIndex + i);
        if (row == null) {
            row = activesheet.createRow(rowIndex + i);
        }
        String[] tokens = splitOnTab(line);
        for (int j = 0; j < pasteWidth; j++) {
            Cell cell = row.getCell(colIndex + j);
            if (cell == null) {
                cell = row.createCell(colIndex + j);
            }
            if (j < tokens.length) {
                String cellContent = tokens[j];
                Double numVal = SpreadsheetUtil.parseNumber(cell, cellContent, spreadsheet.getLocale());
                if (numVal != null) {
                    cell.setCellType(Cell.CELL_TYPE_NUMERIC);
                    cell.setCellValue(numVal);
                } else {
                    cell.setCellValue(cellContent);
                }
            } else {
                cell.setCellType(Cell.CELL_TYPE_BLANK);
                spreadsheet.markCellAsDeleted(cell, true);
            }

            spreadsheet.getCellValueManager().markCellForUpdate(cell);
            spreadsheet.getCellValueManager().getFormulaEvaluator().notifyUpdateCell(cell);
        }
    }

    spreadsheet.getSpreadsheetHistoryManager().addCommand(command);
    spreadsheet.updateMarkedCells();
    // re-set selection to copied area
    spreadsheet.setSelectionRange(rowIndex, colIndex, rowIndex + pasteHeight - 1, colIndex + pasteWidth - 1);

    fireCellValueChangeEvent(affectedRange);
}

From source file:dk.cubing.liveresults.uploader.parser.WcaParser.java

License:Open Source License

@Override
protected List<Event> parseEvents(Workbook workBook) throws IllegalStateException {
    evaluator = workBook.getCreationHelper().createFormulaEvaluator();
    List<Event> events = new CopyOnWriteArrayList<Event>();
    for (int i = 0; i < workBook.getNumberOfSheets(); i++) {
        Sheet sheet = workBook.getSheetAt(i);
        if (isValidResultSheet(sheet)) {
            log.debug("Parsing: {}", sheet.getSheetName());
            Row firstRow = sheet.getRow(4); // first row with event results
            if (firstRow != null) {
                Cell cell = firstRow.getCell(1); // first cell with event results
                if (cell != null && cell.getCellType() != Cell.CELL_TYPE_BLANK) { // only parse sheet with content
                    Event event = parseEventDetails(sheet);
                    event.setLive(workBook.getActiveSheetIndex() == i);
                    List<Result> results = new CopyOnWriteArrayList<Result>();
                    for (int j = 4; j < sheet.getLastRowNum(); j++) {
                        Row row = sheet.getRow(j);
                        if (row != null) {
                            Result result = parseResultRow(row, event);
                            if (result.getFirstname() != null && result.getSurname() != null) {
                                results.add(result);
                            }//  www.  ja v a2s  .  c  o  m
                        }
                    }
                    if (!results.isEmpty()) {
                        event.setResults(results);
                        events.add(event);
                    }
                }
            }
        }
    }
    return events;
}

From source file:org.joeffice.spreadsheet.SpreadsheetComponent.java

License:Apache License

public void load(Workbook workbook) {
    this.workbook = workbook;
    int numberOfSheets = workbook.getNumberOfSheets();
    for (int i = 0; i < numberOfSheets; i++) {
        Sheet sheet = workbook.getSheetAt(i);
        String sheetName = workbook.getSheetName(i);
        JPanel sheetPanel = new SheetComponent(sheet, this);
        addTab(sheetName, sheetPanel);/*from   w  w  w . j a v  a  2s .co m*/
    }
    setSelectedIndex(workbook.getActiveSheetIndex());
    formulaEvaluator = workbook.getCreationHelper().createFormulaEvaluator();
}

From source file:org.tiefaces.components.websheet.utility.CellUtility.java

License:MIT License

/**
 * Gets the poi cell with row col from current page.
 *
 * @param rowIndex//w  ww. j a  va2  s .  c  om
 *            the row index
 * @param colIndex
 *            the col index
 * @param wb
 *            workbook.
 * @return the poi cell with row col from current page
 */
public static Cell getPoiCellWithRowColFromCurrentPage(final int rowIndex, final int colIndex,
        final Workbook wb) {
    if (wb != null) {
        return getPoiCellFromSheet(rowIndex, colIndex, wb.getSheetAt(wb.getActiveSheetIndex()));
    }
    return null;
}