1

Estoy tratando de insertar una imagen con apache poi en un excel, pero simplemente no hace nada, no marca error, no imprime imagenes, pero sí genera las celdas con texto correctamente.

Aquí está mi código:

public class GenerarExcelConImagen {

public void run() {
    Workbook wb = null;
    try {
        wb = new HSSFWorkbook();
    } catch (EncryptedDocumentException ex) {
        Logger.getLogger(GenerarExcelConImagen.class.getName()).log(Level.SEVERE, null, ex);
    }
    Sheet sheet = wb.createSheet("hola");
    Row row = sheet.createRow(1);
    Cell cell = row.createCell(1);
    cell.setCellValue("hololasdjlasfsajfk");
    insertarImagen(wb, sheet);
    try (FileOutputStream file = new FileOutputStream("dadada.xls")) {
        wb.write(file);
        wb.close();
        file.close();
    } catch (IOException ex) {
    }
}

private void insertarImagen(Workbook wb, Sheet sheet) {
    try (InputStream inputStream = new FileInputStream("logo.png");) {
        System.out.println("insertaré la imagen");

        byte[] imageBytes = IOUtils.toByteArray(inputStream);

        int pictureureIdx = wb.addPicture(imageBytes, Workbook.PICTURE_TYPE_PNG);

        inputStream.close();

        CreationHelper helper = wb.getCreationHelper();

        Drawing drawing = sheet.createDrawingPatriarch();

        ClientAnchor anchor = helper.createClientAnchor();

        anchor.setCol1(1);
        anchor.setRow1(1);

        drawing.createPicture(anchor, pictureureIdx);



        Picture pict = drawing.createPicture(anchor, pictureureIdx);
        Cell cell = sheet.createRow(2).createCell(1);

        System.out.println("imagen insertada");
    } catch (Exception e) {
        System.out.println(e);
    }
}


}

Esto es lo que me genera :

introducir la descripción de la imagen aquí

2 respuestas 2

1

Me paso lo mismo que a ti cuando no puse esto:

pict.resize();

Si no sirve eso puedes usar,este método te permite agregar una imagen en la columna 0 fila 0.

    protected void addImage(Sheet sheet, BufferedImage img) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ImageIO.write(img, ".png", baos);
    baos.flush();
    byte[] imageInByte = baos.toByteArray();
    baos.close();
    //Revisar si es png o jpg
    int pictureIdx = sheet.getWorkbook().addPicture(imageInByte, Workbook.PICTURE_TYPE_PNG);
    CreationHelper helper = sheet.getWorkbook().getCreationHelper();
    Drawing<?> drawing = sheet.createDrawingPatriarch();

    ClientAnchor anchor = helper.createClientAnchor();
    anchor.setCol1(0);
    anchor.setRow1(0);

    Picture pict = drawing.createPicture(anchor, pictureIdx);
    pict.resize();
    }
0

Intentalo con este método. Necesitas pasarle el libro, la hoja ,un nombre y la imagen.

public void setimagen(XSSFWorkbook workbook, Sheet sheet, String name, InputStream file) {
        try {
            Font whiteFont = workbook.createFont();
            whiteFont.setColor(IndexedColors.ORANGE.index);
            whiteFont.setFontHeightInPoints((short) 20.00);
            whiteFont.setBold(true);

        XSSFCellStyle cellheader = workbook.createCellStyle();
        cellheader.setAlignment(XSSFCellStyle.ALIGN_CENTER);
        cellheader.setWrapText(true);
        cellheader.setFont(whiteFont);

        //NOMBRE DEL REPORTE
        Row row = sheet.createRow((short) 1);
        row.setHeightInPoints((float) 69.00);
        Cell cell = row.createCell((short) 4);
        sheet.addMergedRegion(CellRangeAddress.valueOf("$E$2:$G$2"));
        cell.setCellValue(new XSSFRichTextString(name));
        cell.setCellStyle(cellheader);
        //Get the contents of an InputStream as a byte[].
        byte[] bytes = IOUtils.toByteArray(file);
        //Adds a picture to the workbook
        int pictureIdx = workbook.addPicture(bytes, Workbook.PICTURE_TYPE_PNG);
        //close the input stream
        //Returns an object that handles instantiating concrete classes
        CreationHelper helper = workbook.getCreationHelper();
        //Creates the top-level drawing patriarch.
        Drawing drawing = sheet.createDrawingPatriarch();
        //Create an anchor that is attached to the worksheet
        ClientAnchor anchor = helper.createClientAnchor();
        //set top-left corner for the image
         anchor.setDx1(0);
        anchor.setDy1(0);
        anchor.setDx2(1023);
        anchor.setDy2(0);
        anchor.setCol1(1);
        anchor.setRow1(1);
        anchor.setCol2(1);
        anchor.setRow2(1);
        //Creates a picture
        Picture pict = drawing.createPicture(anchor, pictureIdx);
        //Reset the image to the original size
        pict.resize();
        sheet.createFreezePane(0, 3, 0, 3);
    } catch (Exception ex) {
           ex.printStackTrace();
    }

}

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.