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 :