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

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

Introduction

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

Prototype

Sheet getSheet();

Source Link

Document

Returns the sheet this cell belongs to

Usage

From source file:com.ssy.havefun.f3d.F3DDaoImpl.java

public String getCellValue(Cell cell) {
    String ret = "";
    switch (cell.getCellType()) {
    case Cell.CELL_TYPE_BLANK:
        ret = "";
        break;/*  w w  w.jav  a  2s .  c  o  m*/
    case Cell.CELL_TYPE_BOOLEAN:
        ret = String.valueOf(cell.getBooleanCellValue());
        break;
    case Cell.CELL_TYPE_ERROR:
        ret = null;
        break;
    case Cell.CELL_TYPE_FORMULA:
        Workbook wb = cell.getSheet().getWorkbook();
        CreationHelper crateHelper = wb.getCreationHelper();
        FormulaEvaluator evaluator = crateHelper.createFormulaEvaluator();
        ret = getCellValue(evaluator.evaluateInCell(cell));
        break;
    case Cell.CELL_TYPE_NUMERIC:
        if (DateUtil.isCellDateFormatted(cell)) {
            //                Date theDate = cell.getDateCellValue();  
            //                ret = simpleDateFormat.format(theDate);  
        } else {
            ret = NumberToTextConverter.toText(cell.getNumericCellValue());
        }
        break;
    case Cell.CELL_TYPE_STRING:
        ret = cell.getRichStringCellValue().getString();
        break;
    default:
        ret = null;
    }

    return ret; //?trim  
}

From source file:com.vaadin.addon.spreadsheet.action.SpreadsheetAction.java

License:Open Source License

/**
 * Returns the locking status of the given cell.
 * //from  ww  w  .  j a  v a 2 s  . co  m
 * @param cell
 *            Target cell
 * @return true if the given cell is locked
 */
protected boolean isCellLocked(Cell cell) {
    return cell.getSheet().getProtect() && cell.getCellStyle().getLocked();
}

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

License:Open Source License

private void handleIsDisplayZeroPreference(Cell cell, CellData cellData) {
    boolean isCellNumeric = cell.getCellType() == Cell.CELL_TYPE_NUMERIC;
    boolean isCellFormula = cell.getCellType() == Cell.CELL_TYPE_FORMULA;
    boolean isApplicableCellType = isCellNumeric || isCellFormula;

    boolean displayZeroAsBlank = !cell.getSheet().isDisplayZeros();
    boolean valueIsZero = ZERO_AS_STRING.equals(cellData.value);

    if (isApplicableCellType && displayZeroAsBlank && valueIsZero) {
        cellData.value = "";
    }//w w w .j  ava2 s  .  co  m
}

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

License:Open Source License

private void navigateTo(Cell cell, Spreadsheet spreadsheet, String address) {
    if (address.contains("!")) { // has sheet name -> change
        String currentSheetName = cell.getSheet().getSheetName();
        String sheetName = address.substring(0, address.indexOf("!"));
        if (!currentSheetName.equals(sheetName)) {
            int sheetPOIIndex = getSheetIndex(cell, sheetName);
            spreadsheet.setActiveSheetWithPOIIndex(sheetPOIIndex);
        }/*from  www  . j  a v a2s  .c  om*/
        spreadsheet.initialSheetSelection = address;
        spreadsheet.getCellSelectionManager().onSheetAddressChanged(address, true);
    } else {
        // change selection to cell within the same sheet
        spreadsheet.getCellSelectionManager().onSheetAddressChanged(address, false);
    }
}

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

License:Open Source License

private int getSheetIndex(Cell cell, String sheetName) {
    // if name contains only numbers or contains spaces it's enclosed in
    // single quotes
    if (sheetName.charAt(0) == '\'' && sheetName.charAt(sheetName.length() - 1) == '\'') {
        sheetName = sheetName.substring(1, sheetName.length() - 1);
    }//from  www  .  ja v  a  2 s.  co  m
    return cell.getSheet().getWorkbook().getSheetIndex(sheetName);

}

From source file:de.iteratec.iteraplan.businesslogic.exchange.elasticExcel.util.ExcelUtils.java

License:Open Source License

/**
 * Returns a CellReference containing the cell's sheet name, as opposed to the standard
 * {@link CellReference#CellReference(Cell)} constructor.
 * @param cell//from  ww  w  .j a v a  2 s .  co  m
 *          Cell to create a CellReference from
 * @return The CellReference including the sheet name
 */
public static CellReference getFullCellReference(Cell cell) {
    return new CellReference(cell.getSheet().getSheetName(), cell.getRowIndex(), cell.getColumnIndex(), true,
            true);
}

From source file:de.topicmapslab.jexc.eXql.grammar.expression.ValueExpression.java

License:Apache License

/**
 * Returns the cell value represent by the given token
 * //  ww  w  .  ja  va2  s.  com
 * @param cell
 *            the cell to extract the values from cell
 * @param token
 *            the token specifies the value to extract
 * @return the cell value
 * @throws JeXcException
 *             thrown if cell value token is unknown
 */
public Object getCellValue(final Cell cell, final String token) throws JeXcException {
    if (VALUE.equalsIgnoreCase(token) || VALUE_STRING.equalsIgnoreCase(token)) {
        switch (cell.getCellType()) {
        case Cell.CELL_TYPE_NUMERIC:
            Double d = cell.getNumericCellValue();
            Long l = d.longValue();
            /*
             * check if long value represents the same numeric value then
             * the double origin
             */
            if (d.doubleValue() == l.longValue()) {
                return String.valueOf(l);
            }
            return String.valueOf(d);
        case Cell.CELL_TYPE_BOOLEAN:
            return String.valueOf(cell.getBooleanCellValue());
        case Cell.CELL_TYPE_STRING:
        default:
            return cell.getStringCellValue();
        }
    } else if (VALUE_DATE.equalsIgnoreCase(token)) {
        return cell.getDateCellValue();
    } else if (VALUE_NUMERICAL.equalsIgnoreCase(token)) {
        return cell.getNumericCellValue();
    } else if (STYLE_FOREGROUND.equalsIgnoreCase(token)) {
        CellStyle style = cell.getCellStyle();
        return style == null ? NULL : style.getFillForegroundColor();
    } else if (STYLE_BACKGROUND.equalsIgnoreCase(token)) {
        CellStyle style = cell.getCellStyle();
        return style == null ? NULL : style.getFillBackgroundColor();
    } else if (BORDER_TOP.equalsIgnoreCase(token)) {
        CellStyle style = cell.getCellStyle();
        return style == null ? 0 : style.getBorderTop();
    } else if (BORDER_BOTTOM.equalsIgnoreCase(token)) {
        CellStyle style = cell.getCellStyle();
        return style == null ? 0 : style.getBorderBottom();
    } else if (BORDER_LEFT.equalsIgnoreCase(token)) {
        CellStyle style = cell.getCellStyle();
        return style == null ? 0 : style.getBorderLeft();
    } else if (BORDER_RIGHT.equalsIgnoreCase(token)) {
        CellStyle style = cell.getCellStyle();
        return style == null ? 0 : style.getBorderRight();
    } else if (ADDRESS.equalsIgnoreCase(token)) {
        StringBuilder builder = new StringBuilder();
        builder.append(cell.getSheet().getSheetName());
        builder.append(SLASH);
        builder.append(cell.getRow().getRowNum());
        builder.append(COLON);
        builder.append(cell.getColumnIndex());
        return builder.toString();
    } else if (HEIGHT.equalsIgnoreCase(token)) {
        CellRangeAddress address = XlsxCellUtils.getCellRange(cell);
        if (address != null) {
            return address.getLastRow() - address.getFirstRow() + 1;
        }
        return 1;
    } else if (ROW.equalsIgnoreCase(token)) {
        return cell.getRowIndex();
    } else if (COLUMN.equalsIgnoreCase(token)) {
        return cell.getColumnIndex();
    }
    throw new JeXcException("Unknown constant '" + token + "'!");
}

From source file:de.topicmapslab.jexc.utility.XlsxCellUtils.java

License:Apache License

/**
 * Returns the cell range of the given cell
 * /* w ww  . ja va  2s  . co m*/
 * @param cell
 *            the cell
 * @return the cell range of merged region the cell is part of or
 *         <code>null</code>
 */
public static CellRangeAddress getCellRange(Cell cell) {
    Sheet s = cell.getSheet();
    for (int i = 0; i < s.getNumMergedRegions(); i++) {
        CellRangeAddress a = s.getMergedRegion(i);
        if (a.isInRange(cell.getRowIndex(), cell.getColumnIndex())) {
            return a;
        }
    }
    return null;
}

From source file:fi.thl.pivot.export.XlsxExporter.java

protected void setCellValue(Pivot pivot, Row row, PivotCell cell, int dataColumnNumber) {
    if (null == cell || null == pivot || null == pivot.getRows() || null == row) {
        return;/*from w ww. j  a v  a2s  . com*/
    }
    Cell c = row.createCell(dataColumnNumber + pivot.getRows().size());
    if (cell.isNumber()) {
        int d = cell.determineDecimals();
        double decimals = Math.pow(10, d);
        long value = Math.round(cell.getNumberValue() * decimals);
        c.setCellValue(value / decimals);
        c.setCellStyle(measureStyle(c.getSheet().getWorkbook(), d));
    } else {
        c.setCellValue(cell.getValue());
        c.setCellStyle(defaultStyle);
    }
}

From source file:guru.qas.martini.report.DefaultState.java

License:Apache License

public void updateLongestExecutions() {
    if (!longestExecutionCells.isEmpty()) {
        for (Cell cell : longestExecutionCells) {
            CellStyle original = cell.getCellStyle();
            Sheet sheet = cell.getSheet();
            Workbook workbook = sheet.getWorkbook();
            CellStyle newStyle = workbook.createCellStyle();
            newStyle.cloneStyleFrom(original);
            int originalFontIndex = original.getFontIndexAsInt();
            Font originalFont = workbook.getFontAt(originalFontIndex);

            Font font = workbook.createFont();
            font.setBold(true);/*from   w  ww . ja v  a 2 s  .c o  m*/
            font.setColor(IndexedColors.DARK_RED.getIndex());
            font.setFontHeight((short) Math.round(originalFont.getFontHeight() * 1.5));
            newStyle.setFont(font);
            cell.setCellStyle(newStyle);

            Row row = cell.getRow();
            short firstCellNum = row.getFirstCellNum();
            short lastCellNum = row.getLastCellNum();

            for (int i = firstCellNum; i < lastCellNum; i++) {
                Cell rowCell = row.getCell(i);
                original = rowCell.getCellStyle();
                CellStyle borderStyle = workbook.createCellStyle();
                borderStyle.cloneStyleFrom(original);
                borderStyle.setBorderTop(BorderStyle.MEDIUM);
                borderStyle.setBorderBottom(BorderStyle.MEDIUM);

                if (i == cell.getColumnIndex()) {
                    borderStyle.setBorderLeft(BorderStyle.MEDIUM);
                    borderStyle.setBorderRight(BorderStyle.MEDIUM);
                } else if (i == firstCellNum) {
                    borderStyle.setBorderLeft(BorderStyle.MEDIUM);
                } else if (i == lastCellNum - 1) {
                    borderStyle.setBorderRight(BorderStyle.MEDIUM);
                }
                rowCell.setCellStyle(borderStyle);
            }
        }
    }
}