Pregunta

Yo estoy usando la PDI HSSF API para mi excel manipulaciones en Java.Tengo un valor de fecha "8/1/2009" en uno de mis celda de excel y mientras yo trato de leer este valor mediante HSSF de la API, detecta el tipo de célula como Numérico y devuelve el 'Doble' valor de mi fecha.Ver el código de ejemplo siguiente:

cell = row.getCell(); // date in the cell '8/1/2009'
switch (cell.getCellType()) {

case HSSFCell.CELL_TYPE_STRING:
    cellValue = cell.getRichStringCellValue().getString();
    break;
case HSSFCell.CELL_TYPE_NUMERIC:
    cellValue = new Double(cell.getNumericCellValue()).toString();
    break;
default:
}

De la célula.getCellType() devuelve NUMERIC_TYPE y por lo tanto este código convierte la fecha a doble!:(

Hay alguna forma de leer la fecha como lo es en HSSF POI !?

¿Fue útil?

Solución

Puedes echar un vistazo en:

HSSFDateUtil.isCellDateFormatted()

Ver la PDI Horrible Formato de Hoja de cálculo de la API para obtener más detalles sobre HSSFDateUtil:

http://poi.apache.org/apidocs/org/apache/poi/hssf/usermodel/HSSFDateUtil.html

Que también proporciona algunos métodos auxiliares para el retorno de Excel getExcelDate() y Java fechas getJavaDate().Usted necesita ser un poco cuidadoso de los diferentes formatos de fecha, aunque...

Otros consejos

Si desea hacer referencia a la fecha en el mismo formato en el que en el archivo de Excel, se debe utilizar el CellDateFormatter. Código de ejemplo:

CellValue cValue = formulaEv.evaluate(cell);
double dv = cValue.getNumberValue();
if (HSSFDateUtil.isCellDateFormatted(cell)) {
    Date date = HSSFDateUtil.getJavaDate(dv);

    String dateFmt = cell.getCellStyle().getDataFormatString();
    /* strValue = new SimpleDateFormat(dateFmt).format(date); - won't work as 
    Java fmt differs from Excel fmt. If Excel date format is mm/dd/yyyy, Java 
    will always be 00 for date since "m" is minutes of the hour.*/

    strValue = new CellDateFormatter(dateFmt).format(date); 
    // takes care of idiosyncrasies of Excel
}

Excel trata las fechas y horas como números ... Jon dicho mejor, así que no lo hará eco aquí ...

Sin embargo, el código de ejemplo para lo que ha puesto en la pregunta está en http : //poi.apache.org/spreadsheet/quick-guide.html#CellContents

Si el uso de la PDI 3.5 se puede utilizar el siguiente

cell.getDateCellValue () método. Esto funcionará para Excel 2007 también.

Desde PDI 3.15 beta3 algunas funciones son deprecated. Puede comprobar el formato de datos y recuperar como Java Date.

SimpleDateFormat format = new SimpleDateFormat("");
String cellValue;
if(cell != null && cell.getCellTypeEnum() != CellType.STRING &&
    cell.getCellTypeEnum() == CellType.NUMERIC && DateUtil.isCellDateFormatted(cell)){

    cellValue = format.format(cell.getDateCellValue());
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top