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

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

Introduction

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

Prototype

public Date getDateCellValue() 

Source Link

Document

Get the value of the cell as a date.

Usage

From source file:org.seasar.dbflute.helper.io.xls.DfXlsReader.java

License:Apache License

public Object getValue(int columnIndex, HSSFCell cell, DfDataTable table) {
    if (cell == null) {
        if (isEmptyStringTarget(columnIndex, table)) {
            return "\"\""; // for preventing trimming later
        } else {/*www.j  a va 2s  .c  o m*/
            return null;
        }
    }
    switch (cell.getCellType()) {
    case HSSFCell.CELL_TYPE_NUMERIC:
        if (isCellDateFormatted(cell)) {
            return DfTypeUtil.toTimestamp(cell.getDateCellValue());
        }
        final double numericCellValue = cell.getNumericCellValue();
        if (isInt(numericCellValue)) {
            return new BigDecimal((int) numericCellValue);
        }
        return new BigDecimal(Double.toString(numericCellValue));
    case HSSFCell.CELL_TYPE_STRING:
        String s = cell.getRichStringCellValue().getString();
        if (s != null) {
            if (isNotTrimTarget(cell, table)) {
                if (s.length() != s.trim().length()) {
                    s = "\"" + s + "\""; // for preventing trimming later
                }
            } else {
                s = Srl.rtrim(s);
            }
        }
        if ("".equals(s)) {
            s = null;
        }
        if (isEmptyStringTarget(columnIndex, table) && s == null) {
            s = "\"\""; // for preventing trimming later
        }
        if (isCellBase64Formatted(cell)) {
            return DfTypeUtil.decodeAsBase64(s);
        }
        return s;
    case HSSFCell.CELL_TYPE_BOOLEAN:
        boolean b = cell.getBooleanCellValue();
        return Boolean.valueOf(b);
    default:
        if (isEmptyStringTarget(columnIndex, table)) {
            return "\"\"";
        } else {
            return null;
        }
    }
}

From source file:org.seasar.extension.dataset.impl.XlsReader.java

License:Apache License

/**
 * ????/*  www. j a va 2s . c  o  m*/
 * 
 * @param cell
 *            
 * @return ?
 */
public Object getValue(HSSFCell cell) {
    if (cell == null) {
        return null;
    }
    switch (cell.getCellType()) {
    case HSSFCell.CELL_TYPE_NUMERIC:
        if (isCellDateFormatted(cell)) {
            return TimestampConversionUtil.toTimestamp(cell.getDateCellValue());
        }
        final double numericCellValue = cell.getNumericCellValue();
        if (isInt(numericCellValue)) {
            return new BigDecimal((int) numericCellValue);
        }
        return new BigDecimal(Double.toString(numericCellValue));
    case HSSFCell.CELL_TYPE_STRING:
        String s = cell.getRichStringCellValue().getString();
        if (s != null) {
            s = StringUtil.rtrim(s);
            if (!trimString && s.length() > 1 && s.startsWith("\"") && s.endsWith("\"")) {
                s = s.substring(1, s.length() - 1);
            }
        }
        if ("".equals(s)) {
            s = null;
        }
        if (isCellBase64Formatted(cell)) {
            return Base64Util.decode(s);
        }
        return s;
    case HSSFCell.CELL_TYPE_BOOLEAN:
        boolean b = cell.getBooleanCellValue();
        return Boolean.valueOf(b);
    default:
        return null;
    }
}

From source file:org.xianairlines.action.staffs.StaffsImportAction.java

private Date parse(HSSFCell cell) {
    DateUtil du = new DateUtil();
    if (cell.getCellType() == 0)
        return cell.getDateCellValue();
    if (cell.getCellType() == 1)
        return du.parse(cell.getStringCellValue(), "yyyyMMdd");
    return null;//from ww  w  . j  a  v a  2s .  c o  m
}

From source file:payroll.jobs.RowParser.java

/**
 * Method to parse a row containing Job data.
 * @param inRow HSSFRow to be processed.
 *///from   w  w w .  j  a  va 2 s  . c  o m
public void processRow(HSSFRow inRow) {

    HSSFCell accountNumberCell = inRow.getCell(accountNumberIndex);
    accountNumber = accountNumberCell.getStringCellValue();
    HSSFCell workOrderNumberCell = inRow.getCell(workOrderIndex);
    workOrderNumber = workOrderNumberCell.getStringCellValue();
    HSSFCell dateCell = inRow.getCell(dateIndex);
    date = dateCell.getDateCellValue();
    HSSFCell techIDCell = inRow.getCell(techIDIndex);
    techID = techIDCell.getStringCellValue();
    HSSFCell customerNameCell = inRow.getCell(customerNameIndex);
    customerName = customerNameCell.getStringCellValue();

}

From source file:swift.selenium.WebHelper.java

License:Open Source License

/**
 * This method reads and returns data from each cell of a provided worksheet
 * /* w  w  w . j  a v  a2  s.co m*/
 * @param reqValue
 * @param reqSheet
 * @param rowIndex
 * @param inputHashTable
 * @return
 * @throws IOException
 */
@SuppressWarnings("null")
public static String getCellData(String reqValue, HSSFSheet reqSheet, int rowIndex,
        HashMap<String, Object> inputHashTable) throws IOException {
    HSSFCell reqCell = null;
    Object actualvalue = null;
    String req = "";
    DataFormatter fmt = new DataFormatter();

    if (inputHashTable.isEmpty() == true) {
        inputHashTable = getValueFromHashMap(reqSheet);
    }
    HSSFRow rowActual = reqSheet.getRow(rowIndex);
    if (inputHashTable.get(reqValue) == null) {

        TransactionMapping.report.setStrMessage("Column " + reqValue + " not Found. Please Check input Sheet");
        TransactionMapping.pauseFun("Column " + reqValue + " not Found. Please Check input Sheet");
    } else {
        actualvalue = inputHashTable.get(reqValue);//rowHeader.getCell(colIndex).toString();         
        if (actualvalue != null) {
            int colIndex = Integer.parseInt(actualvalue.toString());
            reqCell = rowActual.getCell(colIndex);
            //TM 27-04-2015: Updated the code for formula in cells
            if (reqCell == null) {
                System.out.println(reqValue + " is Null");
            } else {
                HSSFWorkbook wb = reqCell.getSheet().getWorkbook(); //TM-05/05/2015: Get workbook instance from the worksheet
                HSSFFormulaEvaluator.evaluateAllFormulaCells(wb); //TM-05/05/2015: To refresh all the formulas in the worksheet
                FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();

                CellValue cellValue = evaluator.evaluate(reqCell);
                int type = 0;
                if (cellValue != null) {
                    type = cellValue.getCellType();
                } else {
                    type = reqCell.getCellType();
                }

                switch (type) {
                case HSSFCell.CELL_TYPE_BLANK:
                    req = "";
                    break;
                case HSSFCell.CELL_TYPE_NUMERIC:
                    if (DateUtil.isCellDateFormatted(reqCell)) {
                        SimpleDateFormat form = new SimpleDateFormat(
                                Automation.configHashMap.get("DATEFORMAT").toString());
                        req = form.format(reqCell.getDateCellValue());
                    } else
                        req = fmt.formatCellValue(reqCell, evaluator);
                    break;
                case HSSFCell.CELL_TYPE_STRING:
                    req = reqCell.getStringCellValue();
                    break;
                case HSSFCell.CELL_TYPE_BOOLEAN:
                    req = Boolean.toString(reqCell.getBooleanCellValue());
                    break;
                case HSSFCell.CELL_TYPE_ERROR:
                    req = "error";
                    break;
                }
            }
        }

        else {
            req = reqCell.getStringCellValue();
            System.out.println("null");
        }
    }
    return req;
}

From source file:test.ExcelUtil.java

License:Apache License

/**
 * , /*ww  w.j  av a  2 s . c  o  m*/
 * 
 * @param cell
 * @return
 */
public static String cell2string(HSSFCell cell, HSSFFormulaEvaluator evaluator) {
    if (cell == null) {
        return null;
    }
    String str = null;
    final int cellType = cell.getCellType();

    switch (cellType) {
    case HSSFCell.CELL_TYPE_STRING:
        str = "" + cell.getRichStringCellValue().getString().trim();
        break;
    case HSSFCell.CELL_TYPE_NUMERIC:
        if (HSSFDateUtil.isCellDateFormatted(cell)) {
            str = "" + dateFormat.format(cell.getDateCellValue());
        } else {
            str = String.valueOf(cell.getNumericCellValue());

            //modify by cyyan 2008-09-23 19:17:28 
            //excelE, ;
            //E, 15, (15, )
            // 15, 0, 0
            //            str = "" + new BigDecimal(numberStr).setScale(15, BigDecimal.ROUND_HALF_UP);
            //            
            //            //modify yanchangyou 2008-09-26 18:01:43 
            //            // .0000* , 0, 
            //            if (str.indexOf('.') != -1) {
            //               str = str.replaceAll("(\\.)?0*$", "");
            //            }            

            /*
             * , 
             */
            //            if (str.indexOf('.') != -1) { //
            //               int index = str.length();
            //               for (int i = str.length()-1; i > -1; i--) {
            //                  if (str.charAt(i) == '0') {
            //                     index = i;
            //                  } else if (str.charAt(i) == '.'){
            //                     index = i;
            //                     break;
            //                  } else {
            //                     break;
            //                  }
            //               }
            //               str = str.substring(0, index);
            //            }
        }
        break;
    case HSSFCell.CELL_TYPE_BLANK:
        str = "";
        break;
    case HSSFCell.CELL_TYPE_BOOLEAN:
        str = "" + cell.getBooleanCellValue();
        break;
    case HSSFCell.CELL_TYPE_ERROR:
        str = "" + cell.getErrorCellValue();
        break;
    case HSSFCell.CELL_TYPE_FORMULA:
        if (evaluator == null) {
            str = "" + cell.getRichStringCellValue().getString();
        } else {
            str = "" + evaluator.evaluate(cell).getNumberValue();
        }

        break;
    }

    return (str == null || str.trim().equals("")) ? null : str.trim();
}

From source file:ucar.unidata.data.DataUtil.java

License:Open Source License

/**
 * Convert excel to csv/*www  . j a  va  2s.c  o m*/
 *
 * @param filename excel file
 * @param skipToFirstNumeric if true, skip to first numeric
 * @param sdf If non null then use this to format any date cells
 *
 * @return csv
 *
 * @throws Exception On badness
 */
public static String xlsToCsv(String filename, boolean skipToFirstNumeric, SimpleDateFormat sdf)
        throws Exception {
    StringBuffer sb = new StringBuffer();
    InputStream myxls = IOUtil.getInputStream(filename, DataUtil.class);
    HSSFWorkbook wb = new HSSFWorkbook(myxls);
    HSSFSheet sheet = wb.getSheetAt(0); // first sheet
    boolean seenNumber = false;
    for (int rowIdx = sheet.getFirstRowNum(); rowIdx <= sheet.getLastRowNum(); rowIdx++) {
        HSSFRow row = sheet.getRow(rowIdx);
        if (row == null) {
            sb.append("\n");
            continue;
        }
        boolean rowOk = true;
        for (short colIdx = row.getFirstCellNum(); colIdx < row.getPhysicalNumberOfCells(); colIdx++) {
            HSSFCell cell = row.getCell(colIdx);
            if (cell == null) {
                continue;
            }
            if (skipToFirstNumeric && !seenNumber) {
                if (cell.getCellType() != HSSFCell.CELL_TYPE_NUMERIC) {
                    rowOk = false;
                    break;
                }
                seenNumber = true;
            }

            String cellValue = null;

            if ((sdf != null) && (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC)) {
                if (HSSFDateUtil.isCellDateFormatted(cell)) {
                    Date date = cell.getDateCellValue();
                    cellValue = sdf.format(date);
                }
            }

            if (cellValue == null) {
                cellValue = cell.toString();
            }

            if (colIdx > 0) {
                sb.append(",");
            }
            sb.append(cellValue);
            /*                if(false && comment!=null) {
            String author = comment.getAuthor();
            String str = comment.getString().getString();
            str = StringUtil.replace(str, author+":","");
            str = StringUtil.replace(str, "\n","");
            sb.append("("+str+")");
            }*/
        }
        if (rowOk) {
            sb.append("\n");
        }
    }
    return sb.toString();
}

From source file:utilesBD.servidoresDatos.JServerServidorDatosExcel.java

/**
 * Metodo que se encarga de leer los datos de un archivo en formato excel
 * @return Fila que contiene la informacion del archivo
 *//*from  w  w w . j av  a 2  s  . co  m*/
public JFilaDatosDefecto leeLineaExcel(HSSFRow hssfRow) {
    JFilaDatosDefecto loLinea = new JFilaDatosDefecto();
    //Me barro todos los elementos de una fila
    for (int i = hssfRow.getFirstCellNum(); i < hssfRow.getLastCellNum(); i++) {
        HSSFCell hssfCell = hssfRow.getCell(i);
        if (hssfCell != null) {
            switch (hssfCell.getCellType()) {
            case HSSFCell.CELL_TYPE_BOOLEAN:
                loLinea.addCampo(String.valueOf(hssfCell.getBooleanCellValue()));
                break;
            case HSSFCell.CELL_TYPE_FORMULA:
                try {
                    loLinea.addCampo(hssfCell.getStringCellValue());
                } catch (Exception e) {
                    try {
                        loLinea.addCampo(String.valueOf(hssfCell.getNumericCellValue()));
                    } catch (Exception e1) {
                        try {
                            loLinea.addCampo(new JDateEdu(hssfCell.getDateCellValue()).toString()
                                    .replace("31/12/1899 ", ""));
                        } catch (Exception e2) {
                            try {
                                loLinea.addCampo(String.valueOf(hssfCell.getBooleanCellValue()));
                            } catch (Exception e3) {
                                loLinea.addCampo("");
                            }
                        }
                    }
                }
                break;
            case HSSFCell.CELL_TYPE_NUMERIC:
                if (HSSFDateUtil.isCellDateFormatted(hssfCell)) {
                    loLinea.addCampo(
                            new JDateEdu(hssfCell.getDateCellValue()).toString().replace("31/12/1899 ", ""));
                } else {
                    double ldValor = hssfCell.getNumericCellValue();
                    loLinea.addCampo(
                            JFormat.msFormatearDouble(ldValor, "############.#########").replace(',', '.'));
                }
                break;
            case HSSFCell.CELL_TYPE_STRING:
                loLinea.addCampo(hssfCell.toString());
                break;
            default:
                loLinea.addCampo("");
            }
        } else {
            loLinea.addCampo("");
        }
    }
    return loLinea;
}