Hi Hannes,
Thanks for that info and the code. My need is very similar to yours,
without the scaling.
But I'm not clear on the fix.
Is the bug in POI and need to be patched prior to your code working, or
does the code example you provided work around the bug?
Thanks
Tim
On 18/04/2011 06:39, Hannes Kleindienst wrote:
Good morning,
Am 16.04.2011 11:01, schrieb Mark Beardsley:
I do not think it is a bug, rather that is how Excel works;
I can assure you, it is a bug. I fixed the code, and since then it is
working just as expected. I didn't want to bother the list with
technical details, but I can provide further info, if you want.
Tim, maybe this can help: In my application, I had the requirement that
the image should fill once cell, not extend over a range of cells. I
have several images in a row, the cell width is set to 200px. The images
are resized to a widht of 400px (scaling factor 2).
My code looks like this (row and col are integer row index and col index):
// image scale (relevant for higher DPI)
float imgscale = 2;
// resize image
BufferedImage img = Util.resizeImage(ImageIO.read(imagefile),
(int)(width*imgscale));
// get dimensions of image (height)
int height = img.getHeight();
Row r = sheet.getRow(row);
if (r == null) {
r = sheet.createRow(row);
}
// set row height depending on image height
r.setHeightInPoints(1+ height * 0.75F / imgscale);
// write image to cell
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(img, "JPG", baos);
int pictureIdx = wb.addPicture(baos.toByteArray(),
Workbook.PICTURE_TYPE_JPEG);
CreationHelper helper = wb.getCreationHelper();
// Create the drawing patriarch. This is the top level container for all
shapes.
Drawing drawing = sheet.createDrawingPatriarch();
//add a picture shape
ClientAnchor anchor = helper.createClientAnchor();
//set top-left corner of the picture (relative to cells)
anchor.setCol1(col);
anchor.setRow1(row);
// important for being able to sort
anchor.setAnchorType(ClientAnchor.MOVE_AND_RESIZE);
Picture pict = drawing.createPicture(anchor, pictureIdx);
//auto-size picture relative to its top-left corner
pict.resize(1/imgscale);
Maybe this helps.
Cheers
Hannes
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]