/** * @return cell type */ @Override public CellType getCellType() { return _cell.getCellType(); } /**
/** * Return the cell type. Tables in an array formula return * {@link CellType#FORMULA} for all cells, even though the formula is only defined * in the OOXML file for the top left cell of the array. * <p> * NOTE: POI does not support data table formulas. * Cells in a data table appear to POI as plain cells typed from their cached value. * * @return the cell type * @since POI 3.15 beta 3 * @deprecated use <code>getCellType</code> instead */ @Deprecated @Removal(version = "4.2") @Override public CellType getCellTypeEnum() { return getCellType(); }
/** * Returns the cell at the given (0 based) index, with the specified {@link org.apache.poi.ss.usermodel.Row.MissingCellPolicy} * * @return the cell at the given (0 based) index * @throws IllegalArgumentException if cellnum < 0 or the specified MissingCellPolicy is invalid */ @Override public XSSFCell getCell(int cellnum, MissingCellPolicy policy) { if(cellnum < 0) { throw new IllegalArgumentException("Cell index must be >= 0"); } // Performance optimization for bug 57840: explicit boxing is slightly faster than auto-unboxing, though may use more memory final Integer colI = Integer.valueOf(cellnum); // NOSONAR XSSFCell cell = _cells.get(colI); switch (policy) { case RETURN_NULL_AND_BLANK: return cell; case RETURN_BLANK_AS_NULL: boolean isBlank = (cell != null && cell.getCellType() == CellType.BLANK); return (isBlank) ? null : cell; case CREATE_NULL_AS_BLANK: return (cell == null) ? createCell(cellnum, CellType.BLANK) : cell; default: throw new IllegalArgumentException("Illegal policy " + policy); } }
/** * Get the value of the cell as a boolean. * <p> * For strings, numbers, and errors, we throw an exception. For blank cells we return a false. * </p> * @return the value of the cell as a boolean * @throws IllegalStateException if the cell type returned by {@link #getCellType()} * is not {@link CellType#BOOLEAN}, {@link CellType#BLANK} or {@link CellType#FORMULA} */ @Override public boolean getBooleanCellValue() { CellType cellType = getCellType(); switch(cellType) { case BLANK: return false; case BOOLEAN: return _cell.isSetV() && TRUE_AS_STRING.equals(_cell.getV()); case FORMULA: //YK: should throw an exception if requesting boolean value from a non-boolean formula return _cell.isSetV() && TRUE_AS_STRING.equals(_cell.getV()); default: throw typeMismatch(CellType.BOOLEAN, cellType, false); } }
CellType cellType = getCellType(); switch(cellType) { case BLANK:
CellType cellType = getCellType();
private String convertCellValueToString() { CellType cellType = getCellType();
switch (getCellType()) { case NUMERIC: if (DateUtil.isCellDateFormatted(this)) { return ErrorEval.getText(getErrorCellValue()); default: return "Unknown Cell Type: " + getCellType();
/** * Get the value of the cell as a date. * <p> * For strings we throw an exception. For blank cells we return a null. * </p> * @return the value of the cell as a date * @throws IllegalStateException if the cell type returned by {@link #getCellType()} is {@link CellType#STRING} * @exception NumberFormatException if the cell value isn't a parsable <code>double</code>. * @see DataFormatter for formatting this date into a string similar to how excel does. */ @Override public Date getDateCellValue() { if (getCellType() == CellType.BLANK) { return null; } double value = getNumericCellValue(); boolean date1904 = getSheet().getWorkbook().isDate1904(); return DateUtil.getJavaDate(value, date1904); }
private void mapCellOnNode(XSSFCell cell, Node node) { switch (cell.getCellType()) {
CellType cellType = getCellType(); switch (cellType){ case FORMULA:
/** * package/hierarchy use only - reuse an existing evaluation workbook if available for caching * * @param fpb evaluation workbook for reuse, if available, or null to create a new one as needed * @return a formula for the cell * @throws IllegalStateException if the cell type returned by {@link #getCellType()} is not {@link CellType#FORMULA} */ protected String getCellFormula(BaseXSSFEvaluationWorkbook fpb) { CellType cellType = getCellType(); if(cellType != CellType.FORMULA) { throw typeMismatch(CellType.FORMULA, cellType, false); } CTCellFormula f = _cell.getF(); if (isPartOfArrayFormulaGroup()) { /* In an excel generated array formula, the formula property might be set, but the string is empty in slave cells */ if (f == null || f.getStringValue().isEmpty()) { XSSFCell cell = getSheet().getFirstCellInArrayFormula(this); return cell.getCellFormula(fpb); } } if (f == null) { return null; } else if (f.getT() == STCellFormulaType.SHARED) { return convertSharedFormula((int)f.getSi(), fpb == null ? XSSFEvaluationWorkbook.create(getSheet().getWorkbook()) : fpb); } else { return f.getStringValue(); } }
private static String getValue(XSSFCell xssFCell) { String str = null; if(xssFCell == null){ return str; } if (xssFCell.getCellType() == XSSFCell.CELL_TYPE_BOOLEAN) { str = String.valueOf(xssFCell.getBooleanCellValue()); } else if (xssFCell.getCellType() == XSSFCell.CELL_TYPE_NUMERIC) { str = String.valueOf(new DecimalFormat("#").format(xssFCell.getNumericCellValue())); } else { str = String.valueOf(xssFCell.getStringCellValue()); } return StringUtils.trim(str); }
/** * when a cell with a 'master' shared formula is removed, the next cell in the range becomes the master * @param cell The cell that is removed * @param evalWb BaseXSSFEvaluationWorkbook in use, if one exists */ protected void onDeleteFormula(XSSFCell cell, BaseXSSFEvaluationWorkbook evalWb){ CTCellFormula f = cell.getCTCell().getF(); if (f != null && f.getT() == STCellFormulaType.SHARED && f.isSetRef() && f.getStringValue() != null) { CellRangeAddress ref = CellRangeAddress.valueOf(f.getRef()); if(ref.getNumberOfCells() > 1){ DONE: for(int i = cell.getRowIndex(); i <= ref.getLastRow(); i++){ XSSFRow row = getRow(i); if(row != null) for(int j = cell.getColumnIndex(); j <= ref.getLastColumn(); j++){ XSSFCell nextCell = row.getCell(j); if(nextCell != null && nextCell != cell && nextCell.getCellType() == CellType.FORMULA){ CTCellFormula nextF = nextCell.getCTCell().getF(); nextF.setStringValue(nextCell.getCellFormula(evalWb)); CellRangeAddress nextRef = new CellRangeAddress( nextCell.getRowIndex(), ref.getLastRow(), nextCell.getColumnIndex(), ref.getLastColumn()); nextF.setRef(nextRef.formatAsString()); sharedFormulas.put((int)nextF.getSi(), nextF); break DONE; } } } } } }
CellType prevType = getCellType();
CellType cellType = getCellType(); XSSFRichTextString rt; switch (cellType) {
for (final Cell c : srcRow){ final XSSFCell srcCell = (XSSFCell)c; final XSSFCell destCell = createCell(srcCell.getColumnIndex(), srcCell.getCellType()); destCell.copyCellFrom(srcCell, policy);
/** * @return cell type */ @Override public CellType getCellType() { return _cell.getCellType(); } /**
public static boolean isExpression( XSSFCell cell ) { if(cell.getCellType() != Cell.CELL_TYPE_STRING){ return false; } String value = cell.getStringCellValue(); if(value == null){ return false; }else { return value.contains("${"); } } }
XSSFWorkbook workbook = new XSSFWorkbook(); XSSFSheet sheet = workbook.createSheet(); XSSFFormulaEvaluator evaluator = new XSSFFormulaEvaluator(workbook); XSSFCell cellvalue = sheet.createRow(0).createCell(0); sheet.getRow(0).createCell(1).setCellValue(2); sheet.getRow(0).createCell(2).setCellValue(5); cellvalue.setCellFormula("B1+C1"); if (cellvalue.getCellType() == XSSFCell.CELL_TYPE_FORMULA) { evaluator.evaluateFormulaCell(cellvalue); } System.out.println(cellvalue.getNumericCellValue());