Example usage for org.apache.poi.hssf.usermodel HSSFCell getNumericCellValue

List of usage examples for org.apache.poi.hssf.usermodel HSSFCell getNumericCellValue

Introduction

In this page you can find the example usage for org.apache.poi.hssf.usermodel HSSFCell getNumericCellValue.

Prototype

public double getNumericCellValue() 

Source Link

Document

Get the value of the cell as a number.

Usage

From source file:com.cn.util.ExcelImport.java

/**
* ?2003excel/*from w  w w  .  jav  a 2  s .  c om*/
* @param file
* @return
*/
private static List<List<Object>> read2003Excel(InputStream inputStream) throws IOException {
    List<List<Object>> dataList = new ArrayList<>();
    HSSFWorkbook wb = new HSSFWorkbook(inputStream);
    HSSFSheet sheet = wb.getSheetAt(0);
    HSSFRow row = null;
    HSSFCell cell = null;
    Object val = null;
    DecimalFormat df = new DecimalFormat("0");// ?
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// ?

    for (int i = sheet.getFirstRowNum(); i < sheet.getPhysicalNumberOfRows(); i++) {
        row = sheet.getRow(i);
        if (row == null) {
            continue;
        }
        List<Object> objList = new ArrayList<>();
        for (int j = row.getFirstCellNum(); j < row.getLastCellNum(); j++) {
            cell = row.getCell(j);
            if (cell == null) {
                val = null;
                objList.add(val);
                continue;
            }
            switch (cell.getCellType()) {
            case HSSFCell.CELL_TYPE_STRING:
                val = cell.getStringCellValue();
                break;
            case HSSFCell.CELL_TYPE_NUMERIC:
                if ("@".equals(cell.getCellStyle().getDataFormatString())) {
                    val = df.format(cell.getNumericCellValue());
                } else if ("General".equals(cell.getCellStyle().getDataFormatString())) {
                    val = df.format(cell.getNumericCellValue());
                } else {
                    val = sdf.format(HSSFDateUtil.getJavaDate(cell.getNumericCellValue()));
                }
                break;
            case HSSFCell.CELL_TYPE_BOOLEAN:
                val = cell.getBooleanCellValue();
                break;
            case HSSFCell.CELL_TYPE_BLANK:
                val = "";
                break;
            default:
                val = cell.toString();
                break;
            }
            objList.add(val);
        }
        dataList.add(objList);
    }
    return dataList;
}

From source file:com.concursive.connect.web.modules.plans.utils.AssignmentExcelImporter.java

License:Open Source License

/**
 * Gets the value attribute of the AssignmentImporter class
 *
 * @param cell Description of the Parameter
 * @return The value value//w w w.ja  va 2s  .co m
 */
private static String getValue(HSSFCell cell) {
    if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
        return String.valueOf(cell.getNumericCellValue());
    }
    if (cell.getCellType() == HSSFCell.CELL_TYPE_BOOLEAN) {
        return String.valueOf(cell.getBooleanCellValue());
    }
    if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
        return cell.getStringCellValue().trim();
    }
    if (cell.getCellType() == HSSFCell.CELL_TYPE_BLANK) {
        return "";
    }
    if (System.getProperty("DEBUG") != null) {
        System.out.println("AssignmentExcelImporter-> NONE: " + cell.getCellType());
    }
    try {
        return cell.getStringCellValue().trim();
    } catch (Exception e) {
    }
    try {
        return String.valueOf(cell.getNumericCellValue());
    } catch (Exception e) {
    }
    try {
        return String.valueOf(cell.getBooleanCellValue());
    } catch (Exception e) {
    }
    return null;
}

From source file:com.demo.common.extreme.view.XlsView.java

License:Apache License

private void fixWidthAndPopulate(HSSFCell cell, double numeric, String value) {
    int valWidth = 0;

    if (numeric != NON_NUMERIC) {
        cell.setCellValue(numeric);/*from ww  w.j ava2s . co m*/
        valWidth = (cell.getNumericCellValue() + "$,.").length() * WIDTH_MULT;
    } else {
        cell.setCellValue(value);
        valWidth = (cell.getStringCellValue() + "").length() * WIDTH_MULT;

        if (valWidth < (WIDTH_MULT * MIN_CHARS)) {
            valWidth = WIDTH_MULT * MIN_CHARS;
        }
    }

    if (valWidth > sheet.getColumnWidth(cell.getCellNum())) {
        sheet.setColumnWidth(cell.getCellNum(), (short) valWidth);
    }
}

From source file:com.dtrules.compiler.excel.util.ImportRuleSets.java

License:Apache License

private String getCellValue(HSSFSheet sheet, int row, int column) {
    if (row > sheet.getLastRowNum())
        return "";
    HSSFRow theRow = sheet.getRow(row);//  www.  j  ava2s.c  o m
    if (theRow == null)
        return "";
    HSSFCell cell = theRow.getCell(column);
    if (cell == null)
        return "";
    switch (cell.getCellType()) {
    case HSSFCell.CELL_TYPE_BLANK:
        return "";
    case HSSFCell.CELL_TYPE_BOOLEAN:
        return cell.getBooleanCellValue() ? "true" : "false";
    case HSSFCell.CELL_TYPE_NUMERIC: {
        Double v = cell.getNumericCellValue();
        if (v.doubleValue() == (v.longValue())) {
            return Long.toString(v.longValue());
        }
        return Double.toString(v);
    }
    case HSSFCell.CELL_TYPE_STRING:
        String v = cell.getRichStringCellValue().getString().trim();
        return v;

    default:
        return "";
    }
}

From source file:com.duroty.lucene.parser.MSExcelParser.java

License:Open Source License

/**
 * DOCUMENT ME!//  ww  w. jav  a  2  s  .c om
 *
 * @return DOCUMENT ME!
 *
 * @throws ParserException DOCUMENT ME!
 */
private String getContents() throws ParserException {
    String contents = "";

    try {
        POIFSFileSystem fs = new POIFSFileSystem(input);
        HSSFWorkbook workbook = new HSSFWorkbook(fs);
        StringBuffer buffer = new StringBuffer();

        for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
            HSSFSheet sheet = workbook.getSheetAt(i);

            Iterator rows = sheet.rowIterator();

            while (rows.hasNext()) {
                HSSFRow row = (HSSFRow) rows.next();

                Iterator cells = row.cellIterator();

                while (cells.hasNext()) {
                    HSSFCell cell = (HSSFCell) cells.next();

                    switch (cell.getCellType()) {
                    case HSSFCell.CELL_TYPE_NUMERIC:

                        String num = Double.toString(cell.getNumericCellValue()).trim();

                        if (num.length() > 0) {
                            buffer.append(num + " ");
                        }

                        break;

                    case HSSFCell.CELL_TYPE_STRING:

                        String text = cell.getStringCellValue().trim();

                        if (text.length() > 0) {
                            buffer.append(text + " ");
                        }

                        break;
                    }
                }

                buffer.append("\n");

                /*if (sleep > 0) {
                    try {
                        Thread.sleep(sleep);
                    } catch (Exception ex) {
                    }
                }*/
            }
        }

        contents = buffer.toString();
    } catch (Exception ex) {
        throw new ParserException(ex);
    }

    return contents;
}

From source file:com.elbeesee.poink.transreptor.HSSFCellToXML.java

License:Open Source License

public void onTransrept(INKFRequestContext aContext) throws Exception {
    IHSSFCellRepresentation aIHSSFCellRepresentation = (IHSSFCellRepresentation) aContext
            .sourcePrimary(IHSSFCellRepresentation.class);
    HSSFCell vCell = aIHSSFCellRepresentation.getCellReadOnly();
    String vSheetName = vCell.getSheet().getSheetName();

    StringBuilder vCellXML = new StringBuilder();

    vCellXML.append("<cell columnIndex=\"");
    vCellXML.append(vCell.getColumnIndex());
    vCellXML.append("\" rowIndex=\"");
    vCellXML.append(vCell.getRow().getRowNum());
    vCellXML.append("\" sheetIndex=\"");
    vCellXML.append(vCell.getSheet().getWorkbook().getSheetIndex(vSheetName));
    vCellXML.append("\">");

    int vCellType = vCell.getCellType();
    if (vCellType == Cell.CELL_TYPE_FORMULA) {
        vCellType = vCell.getCachedFormulaResultType();
    }//w  w  w.  ja v a2s.  c  om

    if (vCellType == Cell.CELL_TYPE_NUMERIC) {
        if (HSSFDateUtil.isCellDateFormatted(vCell)) {
            vCellXML.append(vCell.getDateCellValue());
        } else {
            vCellXML.append(vCell.getNumericCellValue());
        }
    } else if (vCellType == Cell.CELL_TYPE_STRING) {
        vCellXML.append(XMLUtils.escape(vCell.getStringCellValue()));
    } else if (vCellType == Cell.CELL_TYPE_BOOLEAN) {
        vCellXML.append(vCell.getBooleanCellValue());
    } else if (vCellType == Cell.CELL_TYPE_BLANK) {
    } else if (vCellType == Cell.CELL_TYPE_ERROR) {
        vCellXML.append(vCell.getErrorCellValue());
    }
    //

    vCellXML.append("</cell>");

    INKFResponse vResponse = aContext.createResponseFrom(vCellXML.toString());
    vResponse.setExpiry(INKFResponse.EXPIRY_DEPENDENT);
}

From source file:com.eryansky.core.excelTools.ExcelUtils.java

License:Apache License

public static void copyCell(HSSFWorkbook destwb, HSSFCell dest, HSSFWorkbook srcwb, HSSFCell src) {
    if (src == null) {
        dest.setCellType(HSSFCell.CELL_TYPE_BLANK);
        return;//from ww w .jav  a 2  s  . com
    }

    if (src.getCellComment() != null)
        dest.setCellComment(src.getCellComment());
    if (src.getCellStyle() != null) {
        HSSFCellStyle nstyle = findStyle(src.getCellStyle(), srcwb, destwb);
        if (nstyle == null) {
            nstyle = destwb.createCellStyle();
            copyCellStyle(destwb, nstyle, srcwb, src.getCellStyle());
        }
        dest.setCellStyle(nstyle);
    }
    dest.setCellType(src.getCellType());

    switch (src.getCellType()) {
    case HSSFCell.CELL_TYPE_BLANK:

        break;
    case HSSFCell.CELL_TYPE_BOOLEAN:
        dest.setCellValue(src.getBooleanCellValue());
        break;
    case HSSFCell.CELL_TYPE_FORMULA:
        dest.setCellFormula(src.getCellFormula());
        break;
    case HSSFCell.CELL_TYPE_ERROR:
        dest.setCellErrorValue(src.getErrorCellValue());
        break;
    case HSSFCell.CELL_TYPE_NUMERIC:
        dest.setCellValue(src.getNumericCellValue());
        break;
    default:
        dest.setCellValue(new HSSFRichTextString(src.getRichStringCellValue().getString()));
        break;
    }
}

From source file:com.esd.cs.common.HExcelSheetParser.java

License:Open Source License

private Object getCellString(HSSFCell cell) {
    Object result = null;//from  w ww  .  jav a2  s  .co m
    if (cell != null) {
        // ?Numeric:0,String:1,Formula:2,Blank:3,Boolean:4,Error:5
        int cellType = cell.getCellType();
        switch (cellType) {
        case HSSFCell.CELL_TYPE_STRING:
            result = cell.getRichStringCellValue().getString();
            break;
        case HSSFCell.CELL_TYPE_NUMERIC:
            result = cell.getNumericCellValue();
            break;
        case HSSFCell.CELL_TYPE_FORMULA:
            result = cell.getNumericCellValue();
            break;
        case HSSFCell.CELL_TYPE_BOOLEAN:
            result = cell.getBooleanCellValue();
            break;
        case HSSFCell.CELL_TYPE_BLANK:
            result = null;
            break;
        case HSSFCell.CELL_TYPE_ERROR:
            result = null;
            break;
        default:
            logger.info("");
            break;
        }
    }
    return result;
}

From source file:com.essa.main.ReadExcel.java

public static void main(String[] args) {
    try {/*from  w w w  .j  a  v a2  s .c om*/

        InputStream input = new BufferedInputStream(new FileInputStream("sample.xls"));
        POIFSFileSystem fs = new POIFSFileSystem(input);
        HSSFWorkbook wb = new HSSFWorkbook(fs);
        HSSFSheet sheet = wb.getSheetAt(0);

        Iterator rows = sheet.rowIterator();
        while (rows.hasNext()) {
            HSSFRow row = (HSSFRow) rows.next();
            System.out.println("\n");
            Iterator cells = row.cellIterator();
            while (cells.hasNext()) {

                HSSFCell cell = (HSSFCell) cells.next();
                if (HSSFCell.CELL_TYPE_NUMERIC == cell.getCellType())
                    System.out.print(cell.getNumericCellValue() + "     ");
                else if (HSSFCell.CELL_TYPE_STRING == cell.getCellType())
                    System.out.print(cell.getStringCellValue() + "     ");
                else if (HSSFCell.CELL_TYPE_BOOLEAN == cell.getCellType())
                    System.out.print(cell.getBooleanCellValue() + "     ");
                else if (HSSFCell.CELL_TYPE_BLANK == cell.getCellType())
                    System.out.print("BLANK     ");
                else
                    System.out.print("Unknown cell type");

            }

        }

    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

From source file:com.example.selenium.util.ApachePOIXLSReader.java

private static Object cellToObject(HSSFCell cell) {
    Object result;/*from w  ww  .j  a va  2s  .c  o m*/
    switch (cell.getCellType()) {

    case Cell.CELL_TYPE_NUMERIC:
        result = cell.getNumericCellValue();
        break;

    case Cell.CELL_TYPE_STRING:
        result = cell.getStringCellValue();
        break;

    case Cell.CELL_TYPE_BOOLEAN:
        result = cell.getBooleanCellValue();
        break;

    case Cell.CELL_TYPE_FORMULA:
        result = cell.getCellFormula();
        break;

    default:
        throw new RuntimeException("Unknown Cell Type");
    }

    return result;

}