Bug 55705

Summary: Inserting picture into sheet with chart corrupts workbook
Product: POI Reporter: Davor Krtic <davnroid>
Component: HSSFAssignee: POI Developers List <dev>
Status: RESOLVED WONTFIX    
Severity: normal    
Priority: P2    
Version: 3.10-dev   
Target Milestone: ---   
Hardware: PC   
OS: All   
Attachments: Test file

Description Davor Krtic 2013-10-25 15:21:52 UTC
Created attachment 30971 [details]
Test file

When a picture is inserted into sheet that has a chart, resulting file is corrupted and causes Excel crash in my case.

Code used:

Workbook wb = WorkbookFactory.create(new File("Test_chart.xls"));
InputStream is = new FileInputStream("project-logo.jpg");
byte[] bytes = IOUtils.toByteArray(is);
int pictureIdx = wb.addPicture(bytes, Workbook.PICTURE_TYPE_JPEG);
is.close();

CreationHelper helper = wb.getCreationHelper();
Sheet sheet = wb.getSheetAt(0);
Drawing drawing = sheet.createDrawingPatriarch();
ClientAnchor anchor = helper.createClientAnchor();
anchor.setCol1(10);
anchor.setRow1(2);
anchor.setCol2(13);
anchor.setRow2(8);
drawing.createPicture(anchor, pictureIdx);
String file = "Chart_and_picture.xls";
FileOutputStream fileOut = new FileOutputStream(file);
wb.write(fileOut);
fileOut.close();
Comment 1 Nick Burch 2013-10-25 15:27:35 UTC
From http://poi.apache.org/apidocs/org/apache/poi/ss/usermodel/Sheet.html#createDrawingPatriarch%28%29

> Note that this will normally have the effect of removing any existing drawings on this sheet.

So, as documented, if you call createDrawingPatriarch on a sheet with existing drawings/charts, things will break

You'll either need to ensure you only add drawings to sheets without any, or volunteer to help enhance HSSF to support fetching the existing Drawing to modify. The latter would be great for everyone, but sadly in many years no-one has thus far volunteered to help write it / pay someone else to write it :/