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

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

Introduction

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

Prototype

Date getDateCellValue();

Source Link

Document

Get the value of the cell as a date.

Usage

From source file:javacommon.excel.ExcelReader.java

/**
 * ???//  www. j  a va 2  s  .c  om
 *
 * @param c ?
 * @return
 */
private Object getCellValue(Cell c) {
    if (c == null) {
        return null;
    }
    switch (c.getCellType()) {
    case Cell.CELL_TYPE_BLANK:
        return null;
    case Cell.CELL_TYPE_BOOLEAN:
        return c.getBooleanCellValue();
    case Cell.CELL_TYPE_NUMERIC:
        if (DateUtil.isCellDateFormatted(c)) {
            return c.getDateCellValue();
        }
        return c.getNumericCellValue();
    case Cell.CELL_TYPE_STRING:
        return c.getStringCellValue();
    case Cell.CELL_TYPE_FORMULA:
        return c.getCellFormula();
    }
    return null;
}

From source file:Logica.L_Exel.java

public String Importar(File archivo, JTable tablaD) {
    String respuesta = "Revisr";
    DefaultTableModel model = new DefaultTableModel();
    tablaD.setModel(model);/*from www. ja  va2s .c o  m*/
    try {
        wb = WorkbookFactory.create(new FileInputStream(archivo));
        Sheet Hoja = wb.getSheetAt(0);
        Iterator filaIterator = Hoja.rowIterator();
        int indiceFila = -1;
        while (filaIterator.hasNext()) {
            indiceFila++;
            Row fila = (Row) filaIterator.next();
            Iterator columnaIterator = fila.cellIterator();
            Object[] ListaColumna = new Object[7];
            int indicecolumna = -1;
            while (columnaIterator.hasNext()) {
                indicecolumna++;
                Cell celda = (Cell) columnaIterator.next();
                if (indiceFila == 0) {
                    model.addColumn(celda.getStringCellValue());
                } else {
                    if (celda != null) {
                        switch (celda.getCellType()) {
                        case Cell.CELL_TYPE_NUMERIC:
                            ListaColumna[indicecolumna] = (int) Math.round(celda.getNumericCellValue());
                            break;
                        case Cell.CELL_TYPE_STRING:
                            ListaColumna[indicecolumna] = celda.getStringCellValue();
                            break;
                        case Cell.CELL_TYPE_BOOLEAN:
                            ListaColumna[indicecolumna] = celda.getBooleanCellValue();
                            break;
                        default:
                            ListaColumna[indicecolumna] = celda.getDateCellValue();
                            break;
                        }
                    }
                }
            }
            if (indiceFila != 0)
                model.addRow(ListaColumna);
        }
        respuesta = "Funciona";
    } catch (Exception e) {
    }
    return respuesta;
}

From source file:magicware.scm.redmine.tools.util.ExcelUtils.java

License:Apache License

public static String getCellContent(Cell cell, FormulaEvaluator evaluator) {

    String result = null;/* ww  w.ja  v a  2  s .co  m*/

    switch (cell.getCellType()) {
    case Cell.CELL_TYPE_STRING:
        result = cell.getRichStringCellValue().getString();
        break;
    case Cell.CELL_TYPE_NUMERIC:
        if (DateUtil.isCellDateFormatted(cell)) {
            result = Constants.DATE_FORMAT.format(cell.getDateCellValue());
        } else {
            result = String.valueOf(Double.valueOf(cell.getNumericCellValue()).intValue());
        }
        break;
    case Cell.CELL_TYPE_BOOLEAN:
        result = String.valueOf(cell.getBooleanCellValue());
        break;
    case Cell.CELL_TYPE_FORMULA:
        switch (evaluator.evaluateFormulaCell(cell)) {
        case Cell.CELL_TYPE_BOOLEAN:
            result = String.valueOf(cell.getBooleanCellValue());
            break;
        case Cell.CELL_TYPE_NUMERIC:
            if (DateUtil.isCellDateFormatted(cell)) {
                result = Constants.DATE_FORMAT.format(cell.getDateCellValue());
            } else {
                result = String.valueOf(Double.valueOf(cell.getNumericCellValue()).intValue());
            }
            break;
        case Cell.CELL_TYPE_STRING:
            result = String.valueOf(cell.getStringCellValue());
            break;
        case Cell.CELL_TYPE_BLANK:
            break;
        case Cell.CELL_TYPE_ERROR:
            result = String.valueOf(cell.getErrorCellValue());
            break;
        case Cell.CELL_TYPE_FORMULA:
            break;
        }
        break;
    default:
        break;
    }
    return result;
}

From source file:main.KeywordList.java

private String getCellDate(Cell cell) {
    switch (cell.getCellType()) {
    case CELL_TYPE_NUMERIC:
        if (DateUtil.isCellDateFormatted(cell)) {
            return ForcastUi.dateToString(cell.getDateCellValue());
        } else {//from   w  ww. j  a  va  2 s  . c om
            return "01/01/2000";
        }
    case CELL_TYPE_STRING:
        return cell.getStringCellValue();
    case CELL_TYPE_FORMULA:
        return cell.getCellFormula();
    case CELL_TYPE_BLANK:
        return "";
    case CELL_TYPE_BOOLEAN:
        return Boolean.toString(cell.getBooleanCellValue());
    case CELL_TYPE_ERROR:
        return Byte.toString(cell.getErrorCellValue());
    default:
        return "01/01/2000";
    }
}

From source file:midas.sheeco.exceptions.SpreadsheetViolation.java

License:Apache License

private static Object getCellValue(final Cell cell) {
    switch (cell.getCellType()) {
    case Cell.CELL_TYPE_NUMERIC:
        if (DateUtil.isCellDateFormatted(cell)) {
            return cell.getDateCellValue();
        }// w  w w  . j av  a2 s .  com
        return cell.getNumericCellValue();
    case Cell.CELL_TYPE_STRING:
        return cell.getStringCellValue();
    case Cell.CELL_TYPE_BLANK:
        return null;
    case Cell.CELL_TYPE_BOOLEAN:
        return cell.getBooleanCellValue();
    case Cell.CELL_TYPE_ERROR:
        return cell.getErrorCellValue();
    case Cell.CELL_TYPE_FORMULA:
        return cell.getCellFormula();
    }
    throw new UnsupportedOperationException("CellType " + cell.getCellType() + " is invalid");
}

From source file:midas.sheeco.type.adapter.SpreadsheetBooleanAdapterTest.java

License:Apache License

@Test(expected = InvalidCellValueException.class)
public void testInvalidNumeric() {
    SpreadsheetBooleanAdapter adapter = new SpreadsheetBooleanAdapter();

    CellStyle style = mock(CellStyle.class);
    when(style.getDataFormat()).thenReturn((short) 0x0e);
    Cell cell = mock(Cell.class);
    when(cell.getCellType()).thenReturn(Cell.CELL_TYPE_NUMERIC);
    when(cell.getDateCellValue()).thenReturn(new Date(111111));
    when(cell.getCellStyle()).thenReturn(style);

    adapter.fromSpreadsheet(cell);//from   w  ww . j  av a 2s  .c  o  m
}

From source file:midas.sheeco.type.adapter.SpreadsheetDateAdapterTest.java

License:Apache License

@Test
public void testNumericTypeDate() {

    Date expected = new Date(111111);

    CellStyle style = mock(CellStyle.class);
    when(style.getDataFormat()).thenReturn((short) 0x0e);
    Cell cell = mock(Cell.class);
    when(cell.getCellType()).thenReturn(Cell.CELL_TYPE_NUMERIC);
    when(cell.getDateCellValue()).thenReturn(expected);
    when(cell.getCellStyle()).thenReturn(style);

    Date value = adapter.fromSpreadsheet(cell);
    Assert.assertEquals(expected, value);
}

From source file:misuExcel.excelAdd.java

License:Open Source License

private String getCellString(Cell cell) {
    try {//from w  w w  .  jav  a 2  s  .c o m
        switch (cell.getCellType()) {
        case Cell.CELL_TYPE_STRING:
            return cell.getRichStringCellValue().getString().trim();
        case Cell.CELL_TYPE_NUMERIC:
            if (DateUtil.isCellDateFormatted(cell)) {
                return cell.getDateCellValue().toString().trim();
            } else {
                return String.valueOf(cell.getNumericCellValue()).trim();
            }
        case Cell.CELL_TYPE_BOOLEAN:
            return String.valueOf(cell.getBooleanCellValue()).trim();
        case Cell.CELL_TYPE_FORMULA:
            return String.valueOf(cell.getCellFormula()).trim();
        default:
            return cell.getRichStringCellValue().getString().trim();
        }
    } catch (NullPointerException e) {
        JOptionPane.showMessageDialog(null, e.getMessage(), "", JOptionPane.ERROR_MESSAGE);
    }
    return null;
}

From source file:misuExcel.excelSplit.java

License:Open Source License

private String getCellString(Cell cell) {
    try {//w  w w  .  j  ava 2s .  c o  m
        switch (cell.getCellType()) {
        case Cell.CELL_TYPE_STRING:
            return cell.getRichStringCellValue().getString().trim();
        case Cell.CELL_TYPE_NUMERIC:
            if (DateUtil.isCellDateFormatted(cell)) {
                return cell.getDateCellValue().toString().trim();
            } else {
                return String.valueOf(cell.getNumericCellValue()).trim();
            }
        case Cell.CELL_TYPE_BOOLEAN:
            return String.valueOf(cell.getBooleanCellValue()).trim();
        case Cell.CELL_TYPE_FORMULA:
            return String.valueOf(cell.getCellFormula()).trim();
        default:
            return cell.getStringCellValue().trim();
        }
    } catch (NullPointerException e) {
        JOptionPane.showMessageDialog(null, e.getMessage(), "", JOptionPane.ERROR_MESSAGE);
    }
    return null;
}

From source file:misuExcel.excelWrite.java

License:Open Source License

private void outType01() {
    if (wbSheet != null && names != null && list != null) {
        Log.info("list size:" + list.size());
        String strinfo = "";
        for (int i = 0; i < list.size(); i++) {
            ArrayList<Integer> integers = list.get(i);
            Workbook splitWb = null;//from   w  ww  . j  av  a  2  s  .c om
            if (indexType == 1)
                splitWb = new XSSFWorkbook();
            else if (indexType == 2)
                splitWb = new HSSFWorkbook();
            Sheet sheet = splitWb.createSheet("split");
            for (int j = 0; j < integers.size() + splitJpanel.ignore_Row; j++) {
                Row row = null;
                Row copy = null;
                if (j >= splitJpanel.ignore_Row) {
                    row = sheet.createRow(j);
                    copy = wbSheet.getRow(integers.get(j - splitJpanel.ignore_Row));
                } else {
                    row = sheet.createRow(j);
                    copy = wbSheet.getRow(j);
                }
                for (int k = 0; k < copy.getLastCellNum(); k++) {
                    Cell cell = row.createCell(k);
                    Cell copyCell = copy.getCell(k);
                    if (copyCell != null) {
                        switch (copyCell.getCellType()) {
                        case Cell.CELL_TYPE_STRING:
                            cell.setCellValue(copyCell.getRichStringCellValue().getString().trim());
                            break;
                        case Cell.CELL_TYPE_NUMERIC:
                            if (DateUtil.isCellDateFormatted(copyCell)) {
                                cell.setCellValue(copyCell.getDateCellValue());
                            } else {
                                cell.setCellValue(copyCell.getNumericCellValue());
                            }
                            break;
                        case Cell.CELL_TYPE_BOOLEAN:
                            cell.setCellValue(copyCell.getBooleanCellValue());
                            break;
                        case Cell.CELL_TYPE_FORMULA:
                            cell.setCellValue(copyCell.getCellFormula());
                            break;
                        default:
                            cell.setCellValue(copyCell.getStringCellValue().trim());
                        }
                    }
                }
            }
            createWB(splitWb, names.get(i));
            Log.info(names.get(i) + ".xlsx?");
            strinfo += names.get(i) + "." + _index + "?;";
            if (i != 0 && i % 3 == 0) {
                strinfo += "\n";
            }
        } //end for
        JOptionPane.showMessageDialog(null, strinfo);
    }
}