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

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

Introduction

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

Prototype

String getCellFormula();

Source Link

Document

Return a formula for the cell, for example, SUM(C4:E4)

Usage

From source file:com.ostrichemulators.semtool.poi.main.POIReader.java

License:Open Source License

/**
 * Always return a non-null string (will be "" for null cells).
 *
 * @param cell//from w  ww.  j  av  a2s .  c o m
 * @return
 */
private static String getString(Cell cell) {
    if (null == cell) {
        return "";
    }

    switch (cell.getCellType()) {
    case NUMERIC:
        return Double.toString(cell.getNumericCellValue());
    case BOOLEAN:
        return Boolean.toString(cell.getBooleanCellValue());
    case FORMULA:
        return cell.getCellFormula();
    default:
        return cell.getStringCellValue();
    }
}

From source file:com.pdf.GetPdf.java

public static void addXls(Document document, String url, String type) throws IOException, DocumentException {
    Iterator<Row> rowIterator;
    int colNo;//from   w  w w .  j a  v  a2s  . c  o m
    if (type.equals("xls")) {
        HSSFWorkbook excelWorkbook = new HSSFWorkbook(new URL(url).openStream());
        HSSFSheet my_worksheet = excelWorkbook.getSheetAt(0);
        rowIterator = my_worksheet.iterator();
        colNo = my_worksheet.getRow(0).getLastCellNum();
    } else {
        XSSFWorkbook excelWorkbook1 = new XSSFWorkbook(new URL(url).openStream());
        XSSFSheet my_worksheet = excelWorkbook1.getSheetAt(0);
        rowIterator = my_worksheet.iterator();
        colNo = my_worksheet.getRow(0).getLastCellNum();
    }
    PdfPTable my_table = new PdfPTable(colNo);
    PdfPCell table_cell = null;
    while (rowIterator.hasNext()) {
        Row row = rowIterator.next(); //Read Rows from Excel document       
        Iterator<Cell> cellIterator = row.cellIterator();//Read every column for every row that is READ
        while (cellIterator.hasNext()) {
            Cell cell = cellIterator.next(); //Fetch CELL
            if (cell.getCellType() == (Cell.CELL_TYPE_NUMERIC)) {
                table_cell = new PdfPCell(new Phrase(new Double(cell.getNumericCellValue()).toString()));
                System.out.println(cell.getNumericCellValue());
                my_table.addCell(table_cell);
            } else if (cell.getCellType() == (Cell.CELL_TYPE_STRING)) {
                table_cell = new PdfPCell(new Phrase(cell.getStringCellValue()));
                System.out.println(cell.getStringCellValue());
                my_table.addCell(table_cell);
            } else if (cell.getCellType() == (Cell.CELL_TYPE_FORMULA)) {
                table_cell = new PdfPCell(new Phrase(cell.getCellFormula()));
                my_table.addCell(table_cell);
            } else if (cell.getCellType() == (Cell.CELL_TYPE_BLANK)) {
                table_cell = new PdfPCell(new Phrase(""));
                my_table.addCell(table_cell);
            } else {
                table_cell = new PdfPCell(new Phrase(""));
                my_table.addCell(table_cell);
            }
        }
    }
    document.add(my_table);
}

From source file:com.plugin.excel.util.ExcelUtil.java

License:Apache License

/**
 * @param oldCell//from w  w  w .  j  a v a2s .c om
 * @param newCell
 * @param styleMap
 */
public static void copyCell(Cell oldCell, Cell newCell, Map<Integer, CellStyle> styleMap) {
    if (styleMap != null) {
        if (oldCell.getSheet().getWorkbook() == newCell.getSheet().getWorkbook()) {
            newCell.setCellStyle(oldCell.getCellStyle());
        } else {
            int stHashCode = oldCell.getCellStyle().hashCode();
            CellStyle newCellStyle = styleMap.get(stHashCode);
            if (newCellStyle == null) {
                newCellStyle = newCell.getSheet().getWorkbook().createCellStyle();
                newCellStyle.cloneStyleFrom(oldCell.getCellStyle());
                styleMap.put(stHashCode, newCellStyle);
            }
            newCell.setCellStyle(newCellStyle);
        }
    }
    switch (oldCell.getCellType()) {
    case Cell.CELL_TYPE_STRING:
        newCell.setCellValue(oldCell.getStringCellValue());
        break;
    case Cell.CELL_TYPE_NUMERIC:
        newCell.setCellValue(oldCell.getNumericCellValue());
        break;
    case Cell.CELL_TYPE_BLANK:
        newCell.setCellType(Cell.CELL_TYPE_BLANK);
        break;
    case Cell.CELL_TYPE_BOOLEAN:
        newCell.setCellValue(oldCell.getBooleanCellValue());
        break;
    case Cell.CELL_TYPE_ERROR:
        newCell.setCellErrorValue(oldCell.getErrorCellValue());
        break;
    case Cell.CELL_TYPE_FORMULA:
        newCell.setCellFormula(oldCell.getCellFormula());
        break;
    default:
        break;
    }

}

From source file:com.qihang.winter.poi.excel.imports.ExcelImportServer.java

License:Apache License

/**
 * ?key,?????/*from  www .  ja va 2s  .  c  o m*/
 *
 * @author Zerrion
 * @date 2013-11-21
 * @param cell
 * @return
 */
private String getKeyValue(Cell cell) {
    Object obj = null;
    switch (cell.getCellType()) {
    case Cell.CELL_TYPE_STRING:
        obj = cell.getStringCellValue();
        break;
    case Cell.CELL_TYPE_BOOLEAN:
        obj = cell.getBooleanCellValue();
        break;
    case Cell.CELL_TYPE_NUMERIC:
        obj = cell.getNumericCellValue();
        break;
    case Cell.CELL_TYPE_FORMULA:
        obj = cell.getCellFormula();
        break;
    }
    return obj == null ? null : obj.toString().trim();
}

From source file:com.qihang.winter.poi.util.PoiSheetUtility.java

License:Apache License

private static void cloneCell(Cell cNew, Cell cOld) {
    cNew.setCellComment(cOld.getCellComment());
    cNew.setCellStyle(cOld.getCellStyle());

    switch (cNew.getCellType()) {
    case Cell.CELL_TYPE_BOOLEAN: {
        cNew.setCellValue(cOld.getBooleanCellValue());
        break;/*  ww w  . j  a  v  a  2 s  .  c  o m*/
    }
    case Cell.CELL_TYPE_NUMERIC: {
        cNew.setCellValue(cOld.getNumericCellValue());
        break;
    }
    case Cell.CELL_TYPE_STRING: {
        cNew.setCellValue(cOld.getStringCellValue());
        break;
    }
    case Cell.CELL_TYPE_ERROR: {
        cNew.setCellValue(cOld.getErrorCellValue());
        break;
    }
    case Cell.CELL_TYPE_FORMULA: {
        cNew.setCellFormula(cOld.getCellFormula());
        break;
    }
    }

}

From source file:com.qualogy.qafe.service.DocumentServiceImpl.java

License:Apache License

String getCellValue(Cell cell) {
    String value = null;/*  w  ww  .j ava2 s. c o m*/
    switch (cell.getCellType()) {
    case Cell.CELL_TYPE_NUMERIC:
    case Cell.CELL_TYPE_STRING:
        value = cell.toString();
        break;
    // case Cell.CELL_TYPE_NUMERIC:
    // value = cell.getNumericCellValue() + "";
    // break;
    // case Cell.CELL_TYPE_STRING:
    // value = cell.getStringCellValue();
    // break;
    case Cell.CELL_TYPE_FORMULA:
        value = cell.getCellFormula();
        break;
    case Cell.CELL_TYPE_BLANK:
        ;
        break;
    case Cell.CELL_TYPE_BOOLEAN:
        value = cell.getBooleanCellValue() + "";
        break;
    case Cell.CELL_TYPE_ERROR:
        value = cell.getErrorCellValue() + "";
        break;
    }
    return value != null ? value.trim() : value;
}

From source file:com.radaee.excel.ToHtml.java

License:Apache License

private String getText(Cell cell) {
    switch (cell.getCellType()) {
    case Cell.CELL_TYPE_BLANK:
        return "";
    case Cell.CELL_TYPE_BOOLEAN:
        break;/*w w w. ja va 2s  . c  om*/
    case Cell.CELL_TYPE_FORMULA:
        return cell.getCellFormula();
    case Cell.CELL_TYPE_NUMERIC:
        String phone = Double.toString(cell.getNumericCellValue());

        String str[] = phone.split("[.]");
        phone = str[0];
        if (str[1].contains("E")) {
            String str1[] = str[1].split("[E]");
            phone += str1[0];
        }

        return phone;
    case Cell.CELL_TYPE_STRING:
        return cell.getStringCellValue();
    default:
        return "";
    }
    return null;
}

From source file:com.siberhus.tdfl.excel.DefaultExcelRowReader.java

License:Apache License

private Object getCellValue(Cell cell) {
    if (cell == null)
        return null;
    switch (cell.getCellType()) {
    case Cell.CELL_TYPE_STRING:
        RichTextString rts = cell.getRichStringCellValue();
        if (rts != null) {
            return rts.getString();
        }/*from   www  .  jav a  2 s.c  o  m*/
        return null;
    case Cell.CELL_TYPE_NUMERIC:
        String value = cell.toString();
        /*
         * In POI we cannot know which cell is date or number because both
         * cells have numeric type To fix this problem we need to call
         * toString if it's number cell we can parse it but if it's date
         * cell we cannot parse the value with number parser
         */
        try {
            return new BigDecimal(value);
        } catch (Exception e) {
            return cell.getDateCellValue();
        }
    case Cell.CELL_TYPE_BLANK:
        return null;
    case Cell.CELL_TYPE_BOOLEAN:
        return cell.getBooleanCellValue();
    case Cell.CELL_TYPE_FORMULA:
        return cell.getCellFormula();
    }
    return null;
}

From source file:com.tecacet.jflat.excel.PoiExcelReader.java

License:Apache License

private String getCellContentAsString(Cell cell) {
    switch (cell.getCellType()) {
    case Cell.CELL_TYPE_STRING:
        return cell.getRichStringCellValue().getString();
    case Cell.CELL_TYPE_NUMERIC:
        if (DateUtil.isCellDateFormatted(cell)) {
            return cell.getDateCellValue().toString();
        } else {/*from w  ww.ja v a 2s . c  om*/
            double d = cell.getNumericCellValue();
            // TODO find a flexible enough format for all numeric types
            return numberFormat.format(d);
            // return Double.toString(d);
        }
    case Cell.CELL_TYPE_BOOLEAN:
        boolean b = cell.getBooleanCellValue();
        return Boolean.toString(b);
    case Cell.CELL_TYPE_FORMULA:
        return cell.getCellFormula();
    case Cell.CELL_TYPE_BLANK:
        return "";
    case Cell.CELL_TYPE_ERROR:
        byte bt = cell.getErrorCellValue();
        return Byte.toString(bt);
    default:
        return cell.getStringCellValue();

    }
}

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 w w w  .  java  2  s. co  m*/
 * 
 * @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);
        }
    }
}