1

Tengo un problema al convertir archivos xls a xlsx y es que al momento de leer las celdas donde tienen las fechas el tipo de dato en la celda del xls figura como 13/04/2016 (El tipo de dato en el documento dice personalizado) pero cuando genero el nuevo documento en xlsx me lo toma como un numero por ejemplo 14690 y al formatearlo me da una fecha erronea, como puedo hacer para que en la lectura de esa celda lo tome como un simple string y que no lo convierta a int ? dejare el codigo que hace la conversion de xls a xlsx?

public class xls2xlsx {

/**
 * @param archivo
 * @param salida
 * @throws InvalidFormatException
 * @throws IOException
 */
public String convertirLibro(String archivo,String salida) throws InvalidFormatException,
        IOException {

    String inpFn = archivo;
    String outFn = salida;

    InputStream in = new BufferedInputStream(new FileInputStream(inpFn));
    try {
        Workbook wbIn = new HSSFWorkbook(in);
        File outF = new File(outFn);
        if (outF.exists())
            outF.delete();

        Workbook wbOut = new XSSFWorkbook();
        int sheetCnt = wbIn.getNumberOfSheets();
        for (int i = 0; i < sheetCnt; i++) {
            Sheet sIn = wbIn.getSheetAt(0);
            Sheet sOut = wbOut.createSheet(sIn.getSheetName());
            Iterator<Row> rowIt = sIn.rowIterator();
            while (rowIt.hasNext()) {
                Row rowIn = rowIt.next();
                Row rowOut = sOut.createRow(rowIn.getRowNum());

                Iterator<Cell> cellIt = rowIn.cellIterator();
                while (cellIt.hasNext()) {
                    Cell cellIn = cellIt.next();

                    Cell cellOut = rowOut.createCell(
                            cellIn.getColumnIndex(), cellIn.getCellType());

                    switch (cellIn.getCellType()) {
                    case Cell.CELL_TYPE_BLANK:
                        break;

                    case Cell.CELL_TYPE_BOOLEAN:
                        cellOut.setCellValue(cellIn.getBooleanCellValue());
                        break;

                    case Cell.CELL_TYPE_ERROR:
                        cellOut.setCellValue(cellIn.getErrorCellValue());
                        break;

                    case Cell.CELL_TYPE_FORMULA:
                        cellOut.setCellFormula(cellIn.getCellFormula());
                        break;

                    case Cell.CELL_TYPE_NUMERIC:
                        cellOut.setCellValue(cellIn.getNumericCellValue());

                        break;


                    case Cell.CELL_TYPE_STRING:
                        cellOut.setCellValue(cellIn.getStringCellValue());
                        break;
                    }

                    {
                      CellStyle styleIn = cellIn.getCellStyle();
                      CellStyle styleOut = cellOut.getCellStyle();
                      styleOut.setDataFormat(styleIn.getDataFormat());
                    }
                    cellOut.setCellComment(cellIn.getCellComment());

                    // HSSFCellStyle cannot be cast to XSSFCellStyle
                    // cellOut.setCellStyle(cellIn.getCellStyle());
                }
            }
        }
        OutputStream out = new BufferedOutputStream(new FileOutputStream(
                outF));
        try {
            wbOut.write(out);

        } finally {
            out.close();

        }
    } finally {
        in.close();
    }
    return outFn;
}

}

2 respuestas 2

1

Te recomiendo que cuando verifiques el tipo numérico agregues una validación si la celda es un número o es una fecha. Puedes usar este código:

case Cell.CELL_TYPE_NUMERIC:
    if (DateUtil.isCellDateFormatted(cellIn)) {
        //cambiar esto al formato que deseas de fecha
        CellStyle cellStyle = wbOut.createCellStyle();
        CreationHelper createHelper = wb.getCreationHelper();
        cellStyle.setDataFormat(
            createHelper.createDataFormat().getFormat("d/m/yyyy h:mm"));
        cellOut.setCellValue(cellIn.getDateCellValue());
        cellOut.setCellStyle(cellStyle);
    } else {
        cellOut.setCellValue(cellIn.getNumericCellValue());
    }
    break;

Adaptado de: Issue reading in a cell from Excel with Apache POI y POI - How do I set cell value to Date and apply default Excel date format?

0

Puedes usar DateUtil.isCellDateFormatted Echa un ojo a este hilo también

1
  • Muchas gracias por la respuesta, le vere en un instante y le comentare mis resultados. el 16 dic. 2016 a las 17:17

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.