usar una recorrer poi para microsoft metodo leer fuera escribir desde datos como celdas archivos java apache excel apache-poi

java - una - ¿Cómo leer la celda de Excel que tiene Fecha con Apache POI?



metodo para leer excel java (4)

Necesita DateUtils: vea este artículo para más detalles.

O, mejor aún, use el JExcel de Andy Khan en lugar del POI.

Estoy usando Apache POI 3.6, quiero leer un archivo de Excel que tenga una fecha como esta 8/23/1991 .

switch (cell.getCellType()) { ... ... case HSSFCell.CELL_TYPE_NUMERIC: value = "NUMERIC value=" + cell.getNumericCellValue(); break; ... }

Pero toma el tipo de valor numérico y devuelve el valor como este 33473.0 .

Intenté utilizar el tipo de celda numérica, aunque sin suerte.

dbltemp=row.getCell(c, Row.CREATE_NULL_AS_BLANK).getNumericCellValue(); if (c == 6 || c == 9) { strTemp= new String(dbltemp.toString().trim()); long tempDate = Long.parseLong(strTemp); Date date = new Date(tempDate); strVal = date.toString(); }

¿Cómo puedo solucionar mi problema?


Puede usar CellDateFormatter para buscar la Fecha en el mismo formato que en la celda de Excel. Vea el siguiente código:

CellValue cv = formulaEv.evaluate(cell); double dv = cv.getNumberValue(); if (HSSFDateUtil.isCellDateFormatted(cell)) { Date date = HSSFDateUtil.getJavaDate(dv); String df = cell.getCellStyle().getDataFormatString(); strValue = new CellDateFormatter(df).format(date); }


Sí, entendí tu problema. Si es difícil de identificar, la celda tiene un valor Numérico o de Datos.

Si desea datos en formato que se muestran en Excel, solo necesita formatear la celda utilizando la clase DataFormatter.

DataFormatter dataFormatter = new DataFormatter(); String cellStringValue = dataFormatter.formatCellValue(row.getCell(0)); System.out.println ("Is shows data as show in Excel file" + cellStringValue); // Here it automcatically format data based on that cell format. // No need for extra efforts


Si sabe qué celda, es decir, la posición de la columna dice 0 en cada fila va a ser una fecha, puede ir row.getCell(0).getDateCellValue() a row.getCell(0).getDateCellValue() .
http://poi.apache.org/apidocs/org/apache/poi/hssf/usermodel/HSSFCell.html#getDateCellValue()

ACTUALIZACIÓN: Aquí hay un ejemplo: puede aplicar esto en el código de su caja de conmutadores anterior. Estoy verificando e imprimiendo el valor numérico así como el valor de la fecha. En este caso, la primera columna de mi hoja tiene fechas, por lo tanto, uso row.getCell (0).

Puede usar el bloque de código if (HSSFDateUtil.isCellDateFormatted .. directamente en su caja de conmutadores.

if (row.getCell(0).getCellType() == HSSFCell.CELL_TYPE_NUMERIC) System.out.println ("Row No.: " + row.getRowNum ()+ " " + row.getCell(0).getNumericCellValue()); if (HSSFDateUtil.isCellDateFormatted(row.getCell(0))) { System.out.println ("Row No.: " + row.getRowNum ()+ " " + row.getCell(0).getDateCellValue()); } }

El resultado es

Row No.: 0 39281.0 Row No.: 0 Wed Jul 18 00:00:00 IST 2007 Row No.: 1 39491.0 Row No.: 1 Wed Feb 13 00:00:00 IST 2008 Row No.: 2 39311.0 Row No.: 2 Fri Aug 17 00:00:00 IST 2007