Excel generation with Apache POI, do not write in the same line in different cells

0

I am currently working on the generation of an Excel, this is done by supporting me from Apache POI. I found myself with a problem when writing the information in Excel, this is because with the code that I'm doing I can not write two different texts on the same line but in different cells. In the end the result I'm looking for is something like this:

My code is the following, in it it generates a couple of methods, one that adds an image and another that adds the desired text. As I mentioned, I have problems when writing a different text on the same line but different cells, since it writes the last one I joined.

package pruebaExportExcel;

public static void main(String[] args) throws IOException {

    String imagenLogo = "../pruebaExportExcel/src/logo.png/";   

    /*Crea libro de trabajo excel en blanco*/
        HSSFWorkbook workbook = new HSSFWorkbook();

    /* Crea una hoja nueva */
    HSSFSheet pagina = workbook.createSheet("Nueva Hoja);

    /* Crea el estilo para las celdas */
    CellStyle sinStyle = workbook.createCellStyle();
    CellStyle style = workbook.createCellStyle();
    HSSFFont font = workbook.createFont();

    /*Estilo para texto*/
    style.setFillForegroundColor(IndexedColors.AQUA.getIndex());
    style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
    style.setAlignment(HorizontalAlignment.CENTER);

/ AT THIS POINT WHEN CALLING THE METHOD, SHOW ME ONE OR ANOTHER VALUE, BEING THAT I NEED THE TWO VALUES IN THE SAME LINE BUT IN DIFFERENT CELLS /

    pagina.addMergedRegion(new CellRangeAddress(2, 6, 0, 9));
    pagina.addMergedRegion(new CellRangeAddress(7, 7, 0, 8)); 

    agregaImagen(workbook, pagina, imagenLogo, 3, 3);


    generaFilasColumnas(pagina, 8, 0, style, "TEXTO1");
    generaFilasColumnas(pagina, 8, 9, sinStyle,"TEXTO2");

    try {
    /*Se genera el documento*/
        FileOutputStream salida = new FileOutputStream(new File("C:/Users/salida/ejemplo.xlsx"));
        workbook.write(salida);
        salida.close();
    } catch (Exception e) {
        e.printStackTrace();
        System.out.println("No se genero");
    }
}

public static void agregaImagen(HSSFWorkbook workbook, HSSFSheet pagina, String image, int columna, int fila) {
    try {
        InputStream imagen = new FileInputStream(image);
        byte[] bytes = IOUtils.toByteArray(imagen);
        int my_picture_id = workbook.addPicture(bytes, Workbook.PICTURE_TYPE_JPEG);
        imagen.close();
        HSSFPatriarch drawing = pagina.createDrawingPatriarch();
        ClientAnchor my_anchor = new HSSFClientAnchor();
        my_anchor.setCol1(columna);
        my_anchor.setRow1(fila);
        HSSFPicture my_picture = drawing.createPicture(my_anchor, my_picture_id);
        my_picture.resize();
    } catch (Exception e) {
        System.out.println("No se agrega imagen, detalle: " + e.getMessage());
    }
}

public static String generaFilasColumnas(HSSFSheet pagina, int valorFila, int valorCelda, CellStyle estilo, String textoCelda) {
    /* Se crea una fila en la hoja, en la posicion de VALORFILA*/
    Row fila = pagina.createRow(valorFila);
    /* Se crea una celda en la posicion CREACELDA*/
    Cell celda = fila.createCell(valorCelda);
    /* Se asignan estilos a las celdas con el valor ESTILO*/
    celda.setCellStyle(estilo);
    celda.setCellValue(textoCelda);

    return textoCelda;
}   
    
asked by Jose Antonio 03.01.2019 в 22:19
source

1 answer

0

In case someone occupies. Generate a method that writes in the same row in different cells, it is an option to solve the problem that I described.

    public static String generaFilas2Celdas(HSSFSheet pagina, int valorFila, int valorCelda, CellStyle estilo,
        String textoCelda, int segundaCelda, String valorCeldaDos, CellStyle estilo2, float altoFila) {

    Row fila = pagina.createRow(valorFila); // Crea la fila
    Cell celda = fila.createCell(valorCelda);// Crea la celda
    celda.setCellValue(textoCelda);// Asigna el texto a la celda
    celda.setCellStyle(estilo);// Asigna el estilo
    celda = fila.createCell(segundaCelda);// Crea la segunda celda
    celda.setCellValue(valorCeldaDos);// Asigna el segundo valor
    celda.setCellStyle(estilo2);// Asigna el estilo 2
    celda.getRow().setHeightInPoints(altoFila);//altura de la fila
    celda.setAsActiveCell();

    return textoCelda;
} 
    
answered by 10.01.2019 в 15:16