Bug 55705 - Inserting picture into sheet with chart corrupts workbook
Summary: Inserting picture into sheet with chart corrupts workbook
Status: RESOLVED WONTFIX
Alias: None
Product: POI
Classification: Unclassified
Component: HSSF (show other bugs)
Version: 3.10-dev
Hardware: PC All
: P2 normal (vote)
Target Milestone: ---
Assignee: POI Developers List
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2013-10-25 15:21 UTC by Davor Krtic
Modified: 2013-10-25 15:27 UTC (History)
0 users



Attachments
Test file (26.50 KB, application/vnd.ms-excel)
2013-10-25 15:21 UTC, Davor Krtic
Details

Note You need to log in before you can comment on or make changes to this bug.
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 :/