Hello Mark,

thank you very much for your feedback, I was actually supposing that something was occurring during the intermediate save operation. I am using this java routine inside a more complex (and timely consuming) process that involves a numerical simulation, thus I want to make it possible to have intermediate savings while inserting images.

I have tried to modify the previous code in such a way as to:
1) Write a first image
2) Save and close
3) Open again the saved file
4) Insert a second image
5) Save the final file

However, what I notice is that something strange continues occurring. The second file adds the picture in the right place and with the correct size, but with the "content" of the first picture. Which means that instead of having two different pictures inserted, I got twice the first one.

Please let me know if you can see something wrong in the following code:


        try {

            HSSFWorkbook wb = new HSSFWorkbook();
            HSSFSheet sheet = wb.createSheet("Sheet 1");

            CreationHelper helper = wb.getCreationHelper();
            Drawing drawing = sheet.createDrawingPatriarch();

            {
InputStream is = new FileInputStream(resolvePath("immagine.png"));
                byte[] bytes = IOUtils.toByteArray(is);
int pictureIdx = wb.addPicture(bytes, Workbook.PICTURE_TYPE_PNG);
                is.close();

                ClientAnchor anchor = helper.createClientAnchor();
                anchor.setRow1(1);
                anchor.setCol1(2);
                Picture pict = drawing.createPicture(anchor, pictureIdx);
                pict.resize();
            }

FileOutputStream fileOut1 = new FileOutputStream(sim.getSessionDir() + File.separator + "example_image_1.xls");
            wb.write(fileOut1);
            fileOut1.close();

        } catch (IOException ex) {
             sim.println(ex);
        }

        try {

POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(sim.getSessionDir() + File.separator + "example_image_1.xls"));
            HSSFWorkbook wb = new HSSFWorkbook(fs);
            HSSFSheet sheet = wb.getSheet("Sheet 1");

            CreationHelper helper = wb.getCreationHelper();
            Drawing drawing = sheet.getDrawingPatriarch();

            {
InputStream is = new FileInputStream(resolvePath("immagine2.png"));
                byte[] bytes = IOUtils.toByteArray(is);
int pictureIdx = wb.addPicture(bytes, Workbook.PICTURE_TYPE_PNG);
                is.close();

                ClientAnchor anchor = helper.createClientAnchor();
                anchor.setRow1(14);
                anchor.setRow2(1);
                Picture pict = drawing.createPicture(anchor, pictureIdx);
                pict.resize();
            }


FileOutputStream fileOut2 = new FileOutputStream(sim.getSessionDir() + File.separator + "example_image_2.xls");
            wb.write(fileOut2);
            fileOut2.close();

        } catch (IOException ex) {
             sim.println(ex);
        }



Thanks and best regards,
Flavio




Il 13/06/2012 17:09, Mark Beardsley ha scritto:
Would not say that you are doing anything 'wrong' but it is not normal to
save a workbook away before you have finished modifying it. The reason is
that the binary file format consists of data streams with pointers to the
various sections and POI has to calculate all of these and format the file
correctly before it can save it away to disc. In doing so some things will
be changed and it is this occurrence during the intermediate save operation
that is causing the corruption. You should simply remove that first save
operation and do everything you need to do to the workbook before saving it
away once, at the end of the process..

--
View this message in context: 
http://apache-poi.1045710.n5.nabble.com/Problem-with-inserting-images-after-saving-an-HSSFWorkbook-tp5710135p5710157.html
Sent from the POI - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]


--
Flavio Cimolin
Technical Support Engineer
CD-adapco, Turin Office
phone: +39 011 562 2194 (int 413)
email: [email protected] <mailto:[email protected]>
web: http://www.cd-adapco.com

Reply via email to