Simone Parca
Simone Parca

Reputation: 11

Problems with POI when I add more images

I want to edit an Excel file with images inside, and add more images. this is my test code:

1           File inputFile=new File(path);
2           InputStream inp = new FileInputStream(inputFile);
3           Workbook wb = WorkbookFactory.create(inp);
4           InputStream is = new FileInputStream("catalog.png");
5           byte[] bytes = IOUtils.toByteArray(is);
6           int picture=wb.addPicture(bytes, wb.PICTURE_TYPE_PNG);
7           Drawing drawing  = sheet.createDrawingPatriarch();
8           //drawing = ((XSSFSheet)sheet).getDrawingPatriarch(); //missing method??
9           CreationHelper helper = wb.getCreationHelper();
10          ClientAnchor anchor = helper.createClientAnchor();
11          anchor.setCol1(0);
12          anchor.setRow1(currentRow.getRowNum());
13          anchor.setCol2(1);
14          anchor.setRow2(currentRow.getRowNum()+1);
15          Picture pict = drawing.createPicture(anchor, picture);

the addPicture method at line 6 make other existing images broken (a red X appear instead of image) then I suppose i need a getDrawingPathiarch() method, but there is only createDrawingPatriarch(), and this will destroy existing images... How can I handle this?

Upvotes: 1

Views: 1740

Answers (1)

Gagravarr
Gagravarr

Reputation: 48326

With HSSF, you can only add new images, you can't edit existing ones, or append more. They need to all go in in one go

The .xlsx file format is much saner, so XSSF is able to let you add new images into a file which already has them

Upvotes: 1

Related Questions