Insert Excel Specific Columns without "CreateRow" in POI

0

I have the following existential problem. I have an Excel File where I have to extract certain columns in specific (Working) but when I want to pass it to another Excel to save these Columns the system "Replaces" the previous Columns leaving only the last one. I understand that every time the function "createRow" is executed, it deletes all the rows and can not do what I want. Does anyone have any idea how to do this? Eternally Grateful of all!

private static void traerColumnaExcel(String archivo, int columna, int cont) {
    Row rowArchivo;
    Row rowExcel;
    File pathArchivo = new File(repo + "\" + archivo);
    File pathExcel = new File(repo + "\" + excel);
    Sheet sheetArchivo;
    try {
        FileInputStream fisArchivo = new FileInputStream(pathArchivo);
        FileInputStream fisExcel = new FileInputStream(pathExcel);
        String[] extension = pathArchivo.getName().split("\.");
        if (extension[1].equals("xls")) {
            // xls
            HSSFWorkbook bookArchivo = new HSSFWorkbook(fisArchivo);
            sheetArchivo = bookArchivo.getSheetAt(0);
            bookArchivo.close();
        } else {
            // xlsx
            XSSFWorkbook bookArchivo = new XSSFWorkbook(fisArchivo);
            sheetArchivo = bookArchivo.getSheetAt(0);
            bookArchivo.close();
        }

        Workbook bookExcel = WorkbookFactory.create(fisExcel);
        Sheet sheetExcel = bookExcel.getSheetAt(0);
        Cell celdaUpdate;

        for (int i = 0; i <= sheetArchivo.getLastRowNum(); i++) {
            rowArchivo = sheetArchivo.getRow(i);
            sheetExcel.createRow(i).createCell(columna);
            celdaUpdate = sheetExcel.getRow(i).getCell(columna);
            if (rowArchivo != null) {
                Cell cellArchivo = rowArchivo.getCell(columna);
                // rowExcel.createCell(cont);
                if (cellArchivo != null) {
                    switch (cellArchivo.getCellType()) {
                    case Cell.CELL_TYPE_STRING:
                        // cellExcel.setCellValue(cellArchivo.getStringCellValue());
                        celdaUpdate.setCellValue(cellArchivo.getStringCellValue());
                        System.out.println(cellArchivo.getStringCellValue() + "\t");
                        break;
                    case Cell.CELL_TYPE_NUMERIC:
                        if (HSSFDateUtil.isCellDateFormatted(cellArchivo)) {
                            // cellExcel.setCellValue(cellArchivo.getDateCellValue());
                            celdaUpdate.setCellValue(cellArchivo.getDateCellValue());
                            System.out.println(cellArchivo.getDateCellValue() + "\t");
                        } else {
                            // cellExcel.setCellValue(cellArchivo.getNumericCellValue());
                            celdaUpdate.setCellValue(cellArchivo.getNumericCellValue());
                            System.out.println(cellArchivo.getNumericCellValue() + "\t");
                        }
                        break;
                    case Cell.CELL_TYPE_BOOLEAN:
                        // cellExcel.setCellValue(cellArchivo.getBooleanCellValue());
                        celdaUpdate.setCellValue(cellArchivo.getBooleanCellValue());
                        System.out.println(cellArchivo.getBooleanCellValue() + "\t");
                        break;
                    default:

                    }
                }
            }
        }
        fisExcel.close();
        FileOutputStream outputStream = new FileOutputStream(repo + "\" + excel);
        bookExcel.write(outputStream);
        bookExcel.close();
        outputStream.close();
    } catch (IOException ex) {
        log.warn("leerExcel() - IOException: " + ex.getMessage());
    } catch (InvalidFormatException e) {
        System.out.println(e.getLocalizedMessage());
    }
}
    
asked by Daniel Piña Rivera 25.05.2018 в 21:58
source

0 answers