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.liteoc.control.admin.SpreadSheetTableRepeating.java

License:LGPL

public String getValue(HSSFCell cell) {
    String val = null;
    int cellType = 0;
    if (cell == null) {
        cellType = HSSFCell.CELL_TYPE_BLANK;
    } else {/*from w  w  w .ja v  a2 s .com*/
        cellType = cell.getCellType();
    }

    switch (cellType) {
    case HSSFCell.CELL_TYPE_BLANK:
        val = "";
        break;
    case HSSFCell.CELL_TYPE_NUMERIC:
        // YW << Modify code so that floating number alone can be used for
        // CRF version. Before it must use, e.g. v1.1
        // Meanwhile modification has been done for read PHI cell and
        // Required cell
        val = cell.getNumericCellValue() + "";
        // >> YW
        // buf.append("<td><font class=\"bodytext\">" +
        // cell.getNumericCellValue()
        // + "</font></td>");
        // added to check for whole numbers, tbh 6/5/07
        double dphi = cell.getNumericCellValue();
        if ((dphi - (int) dphi) * 1000 == 0) {
            val = (int) dphi + "";
        }
        // logger.info("found a numeric cell after transfer: "+val);
        break;
    case HSSFCell.CELL_TYPE_STRING:
        val = cell.getStringCellValue();
        if (val.matches("'")) {
            // logger.info("Found single quote! "+val);
            val.replaceAll("'", "''");
        }
        // buf.append("<td><font class=\"bodytext\">" +
        // cell.getStringCellValue()
        // + "</font></td>");
        break;
    case HSSFCell.CELL_TYPE_BOOLEAN:
        boolean val2 = cell.getBooleanCellValue();
        if (val2) {
            val = "true";
        } else {
            val = "false";
        }
    default:
        val = "";
        // buf.append("<td></td>");
    }

    return val.trim();
}

From source file:com.lition.service.impl.OwnedServiceImpl.java

private String getValue(HSSFCell cell) {
    String cellValue = "";
    switch (cell.getCellType()) {
    case HSSFCell.CELL_TYPE_NUMERIC:
        double d = cell.getNumericCellValue();
        int intd = (int) d;
        cellValue = String.valueOf(intd);
        break;/*from w  w  w  . j a  va  2s. c  o  m*/
    case HSSFCell.CELL_TYPE_STRING:
        cellValue = cell.getStringCellValue();
    default:
        break;
    }
    return cellValue;
}

From source file:com.lushapp.common.excel.ExcelUtil.java

License:Apache License

/**
 * ? Excel// w  ww.jav  a  2  s .c  om
 * 
 * @param excel_name
 * @return
 * @throws Exception
 */
public static List<String[]> readExcel(String excel_name) throws Exception {
    // 
    List<String[]> list = new ArrayList<String[]>();

    HSSFWorkbook hssfworkbook = new HSSFWorkbook(new FileInputStream(excel_name));

    // ??i? getNumberOfSheets
    HSSFSheet hssfsheet = hssfworkbook.getSheetAt(0);

    // ??,j getPhysicalNumberOfRows
    for (int j = 0; j < hssfsheet.getPhysicalNumberOfRows(); j++) {
        HSSFRow hssfrow = hssfsheet.getRow(j);
        if (hssfrow != null) {
            int col = hssfrow.getPhysicalNumberOfCells();
            // ??
            String[] arrayString = new String[col];
            for (int i = 0; i < col; i++) {
                HSSFCell cell = hssfrow.getCell(i);
                if (cell == null) {
                    arrayString[i] = "";
                } else if (cell.getCellType() == 0) {
                    // arrayString[i] = new Double(cell.getNumericCellValue()).toString();
                    if (HSSFCell.CELL_TYPE_NUMERIC == cell.getCellType()) {
                        if (HSSFDateUtil.isCellDateFormatted(cell)) {
                            Date d = cell.getDateCellValue();
                            //                      DateFormat formater = new SimpleDateFormat("yyyy-MM-dd");    
                            DateFormat formater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                            arrayString[i] = formater.format(d);
                        } else {
                            arrayString[i] = new BigDecimal(cell.getNumericCellValue()).longValue() + "";
                        }
                    }
                } else {// EXCEL?
                    arrayString[i] = cell.getStringCellValue().trim();
                }
            }
            list.add(arrayString);
        }
    }
    return list;
}

From source file:com.lushapp.common.excel.ExcelUtil.java

License:Apache License

/**
 * ? Excel//from   w ww  .j  a va2  s  .  c om
 * 
 * @param excel_name
 * @return
 * @throws Exception
 */
public static List<List<Object>> readExcelByList(String excel_name) throws Exception {
    // 
    List<List<Object>> list = new ArrayList<List<Object>>();

    HSSFWorkbook hssfworkbook = new HSSFWorkbook(new FileInputStream(excel_name));

    // ??i? getNumberOfSheets
    HSSFSheet hssfsheet = hssfworkbook.getSheetAt(0);

    // ??,j getPhysicalNumberOfRows
    for (int j = 0; j < hssfsheet.getPhysicalNumberOfRows(); j++) {
        HSSFRow hssfrow = hssfsheet.getRow(j);
        if (hssfrow != null) {
            int col = hssfrow.getPhysicalNumberOfCells();
            // ??
            List<Object> arrayString = new ArrayList<Object>();
            for (int i = 0; i < col; i++) {
                HSSFCell cell = hssfrow.getCell(i);
                if (cell == null) {
                    arrayString.add("");
                } else if (cell.getCellType() == 0) {
                    arrayString.add(new Double(cell.getNumericCellValue()).toString());
                } else {// EXCEL?
                    arrayString.add(cell.getStringCellValue().trim());
                }
            }
            list.add(arrayString);
        }
    }
    return list;
}

From source file:com.lushapp.common.excel.ExcelUtil.java

License:Apache License

/**
 * ? Excel/*from   ww w.j a v  a 2s  . co m*/
 * 
 * @param inputstream
 * @return
 * @throws Exception
 */
public static List<List<Object>> readExcelByInputStream(InputStream inputstream) throws Exception {
    // 
    List<List<Object>> list = new ArrayList<List<Object>>();

    HSSFWorkbook hssfworkbook = new HSSFWorkbook(inputstream);

    // ??i? getNumberOfSheets
    HSSFSheet hssfsheet = hssfworkbook.getSheetAt(0);

    // ??,j getPhysicalNumberOfRows

    // //System.out.println("excel "+hssfsheet.getPhysicalNumberOfRows());
    for (int j = 0; j < hssfsheet.getPhysicalNumberOfRows(); j++) {
        HSSFRow hssfrow = hssfsheet.getRow(j);
        if (hssfrow != null) {
            int col = hssfrow.getPhysicalNumberOfCells();
            // ??
            List<Object> arrayString = new ArrayList<Object>();
            for (int i = 0; i < col; i++) {
                HSSFCell cell = hssfrow.getCell(i);
                if (cell == null) {
                    arrayString.add("");
                } else if (cell.getCellType() == 0) {
                    arrayString.add(new Double(cell.getNumericCellValue()).toString());
                } else {// EXCEL?
                    arrayString.add(cell.getStringCellValue().trim());
                }
            }
            list.add(arrayString);
        }
    }
    return list;
}

From source file:com.mebigfatguy.yank.YankTask.java

License:Apache License

private List<Artifact> readArtifactList() throws IOException {
    BufferedInputStream bis = null;
    List<Artifact> artifacts = new ArrayList<Artifact>();

    try {//www. j av  a  2 s  . co  m
        bis = new BufferedInputStream(new FileInputStream(xlsFile));
        POIFSFileSystem poifs = new POIFSFileSystem(bis);
        HSSFWorkbook workBook = new HSSFWorkbook(poifs);

        HSSFSheet sheet = workBook.getSheetAt(0);

        Map<ColumnType, Integer> columnHeaders = getColumnInfo(sheet);
        Integer typeColumn = columnHeaders.get(ColumnType.TYPE_COLUMN);
        Integer classifierColumn = columnHeaders.get(ColumnType.CLASSIFIER_COLUMN);
        String groupId = "";
        String artifactId = "";
        String type = JAR;
        String version = "";
        String classifier = "";

        for (int i = sheet.getFirstRowNum() + 1; i <= sheet.getLastRowNum(); ++i) {
            HSSFRow row = sheet.getRow(i);
            if (row != null) {
                HSSFCell cell = row.getCell(columnHeaders.get(ColumnType.GROUP_COLUMN));
                if (cell != null) {
                    String gId = cell.getStringCellValue().trim();
                    if (!gId.isEmpty()) {
                        groupId = gId;
                    }
                }

                cell = row.getCell(columnHeaders.get(ColumnType.ARTIFACT_COLUMN));
                if (cell != null) {
                    String aId = cell.getStringCellValue().trim();
                    if (!aId.isEmpty()) {
                        artifactId = aId;
                    }
                }

                cell = row.getCell(columnHeaders.get(ColumnType.VERSION_COLUMN));
                if (cell != null) {
                    String v;
                    if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
                        v = String.valueOf(cell.getNumericCellValue());
                    } else {
                        v = cell.getStringCellValue().trim();
                    }
                    if (!v.isEmpty()) {
                        version = v;
                    }
                }

                cell = (typeColumn != null) ? row.getCell(typeColumn.intValue()) : null;
                if (cell != null) {
                    type = cell.getStringCellValue().trim();
                }

                cell = (classifierColumn != null) ? row.getCell(classifierColumn.intValue()) : null;
                if (cell != null) {
                    classifier = cell.getStringCellValue().trim();
                }

                if (groupId.isEmpty() || artifactId.isEmpty() || version.isEmpty()) {
                    if (groupId.isEmpty() || version.isEmpty()) {
                        getProject().log("Row " + row.getRowNum() + ": Invalid artifact specified: [groupId: "
                                + groupId + ", artifactId: " + artifactId + ", classifier: " + classifier
                                + ", version: " + version + "]");
                    }
                } else {
                    artifacts.add(new Artifact(groupId, artifactId, type, classifier, version));
                }
            }

            artifactId = "";
            classifier = "";
            type = JAR;
        }

        getProject().log(sheet.getLastRowNum() + " rows read from " + xlsFile, Project.MSG_VERBOSE);
    } finally {
        Closer.close(bis);
    }

    return artifacts;
}

From source file:com.ms.commons.file.excel.ExcelParser.java

License:Open Source License

@SuppressWarnings({ "deprecation", "unused" })
public String[] splitLine() throws Exception {
    if (m_iCurrentRow == m_iNbRows)
        return null;

    HSSFRow row = m_sheet.getRow(m_iCurrentRow);
    if (row == null) {
        return null;
    } else {/*  ww  w . j a v a  2  s.c  om*/
        int cellIndex = 0;
        int noOfCells = row.getPhysicalNumberOfCells();
        short firstCellNum = row.getFirstCellNum();
        short lastCellNum = row.getLastCellNum();
        String[] values = new String[lastCellNum];

        if (firstCellNum >= 0 && lastCellNum >= 0) {
            for (short iCurrent = firstCellNum; iCurrent < lastCellNum; iCurrent++) {
                HSSFCell cell = (HSSFCell) row.getCell(iCurrent);
                if (cell == null) {
                    values[iCurrent] = StringUtils.EMPTY;
                    cellIndex++;
                    continue;
                } else {
                    switch (cell.getCellType()) {

                    case HSSFCell.CELL_TYPE_NUMERIC:
                        double value = cell.getNumericCellValue();
                        if (HSSFDateUtil.isCellDateFormatted(cell)) {
                            if (HSSFDateUtil.isValidExcelDate(value)) {
                                Date date = HSSFDateUtil.getJavaDate(value);
                                SimpleDateFormat dateFormat = new SimpleDateFormat(JAVA_TOSTRING);
                                values[iCurrent] = dateFormat.format(date);
                            } else {
                                throw new Exception("Invalid Date value found at row number " + row.getRowNum()
                                        + " and column number " + cell.getCellNum());
                            }
                        } else {
                            values[iCurrent] = value + StringUtils.EMPTY;
                        }
                        break;

                    case HSSFCell.CELL_TYPE_STRING:
                        values[iCurrent] = cell.getStringCellValue();
                        break;

                    case HSSFCell.CELL_TYPE_BLANK:
                        values[iCurrent] = null;
                        break;

                    default:
                        values[iCurrent] = null;
                    }
                }
            }
        }
        m_iCurrentRow++;
        return values;
    }
}

From source file:com.ms.commons.test.datareader.impl.ExcelReadUtil.java

License:Open Source License

/**
 * @param name column name of table in which this cell locates.
 *///from  ww w  .  j a va2  s . co  m
@SuppressWarnings("deprecation")
protected static MemoryField readCellValue(String name, HSSFCell cell) {

    MemoryField field = null;
    if (cell == null) {
        field = new MemoryField(name, MemoryFieldType.Null);
    } else {
        switch (cell.getCellType()) {
        case HSSFCell.CELL_TYPE_NUMERIC:
        case HSSFCell.CELL_TYPE_FORMULA:
            if (getCellValueForFormula(cell)) {
                field = new MemoryField(name, MemoryFieldType.Date, cell.getDateCellValue().toString());
            } else {
                field = new MemoryField(name, MemoryFieldType.Number, cell.getNumericCellValue());
            }
            break;

        case HSSFCell.CELL_TYPE_STRING:
            field = new MemoryField(name, MemoryFieldType.String, cell.getStringCellValue());
            break;

        case HSSFCell.CELL_TYPE_BLANK:
            field = new MemoryField(name, MemoryFieldType.Null);
            break;
        default:
            field = new MemoryField(name, MemoryFieldType.Unknow, cell.getStringCellValue());
            break;
        }
    }

    return field;
}

From source file:com.netxforge.netxstudio.data.importer.HSSFMetricValuesImporter.java

License:Open Source License

@Override
protected double getNumericCellValue(int row, int column) {
    HSSFCell cell = currentSheet.getRow(row).getCell(column);
    if (cell != null) {
        return cell.getNumericCellValue();
    } else {// ww  w .j av a2  s.  c om
        return 0.0;
    }
}

From source file:com.netxforge.netxstudio.data.importer.HSSFMetricValuesImporter.java

License:Open Source License

protected String getCellValue(HSSFCell cell) {
    String value = null;//from w ww  .jav a  2 s.  c  om
    // Get the value.
    int cellType = cell.getCellType();

    if (cellType == HSSFCell.CELL_TYPE_NUMERIC) {
        double numericCellValue = cell.getNumericCellValue();
        NumberFormat nf = NumberFormat.getInstance();
        nf.setMaximumFractionDigits(0);// set as you need
        value = nf.format(numericCellValue);

    } else if (cellType == HSSFCell.CELL_TYPE_STRING) {
        value = cell.getStringCellValue();
    }
    return value;
}