Insert image to excel file using JXL without stretching it
Asked Answered
L

2

8

I can insert image to my excel file using jxl usingsheet.addImage(WritableImage obj). My problem is that, it stretches based on the args of WritableImage. I'm wondering if there is a way so that the image that I insert will not stretch like if I insert a 200x200 sized image it will appear to the sheet as 200x200.

Lambaste answered 15/11, 2011 at 13:3 Comment(0)
B
7

As much as this has bugged me about jxl, I've never found a way to insert an image without associating the aspect ratio to cells instead of pixels/inches/any standard unit of measurement, and I've done decent research in the past on doing so.

The best you can do is to adapt the images to the height/width of the cells you are inserting it into, or even better, set the cell width/height for the cells you are putting the image in.

From the JExcel FAQ- http://jexcelapi.sourceforge.net/resources/faq/

private static final double CELL_DEFAULT_HEIGHT = 17;
private static final double CELL_DEFAULT_WIDTH = 64;

File imageFile = new File(GIF_OR_JPG_IMAGE_FILE);
BufferedImage input = ImageIO.read(imageFile);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(input, "PNG", baos);
sheet.addImage(new WritableImage(1,1,input.getWidth() / CELL_DEFAULT_WIDTH,
    input.getHeight() / CELL_DEFAULT_HEIGHT,baos.toByteArray()));

To keep the aspect ratio, you'll also want to set the WritableImage to not re-size if the user changes the row height or column width. Do this with either of the below (your preference based on if you want the image anchor locked or to move with resizing):

WritableImage.MOVE_WITH_CELLS;
WritableImage.NO_MOVE_OR_SIZE_WITH_CELLS;
Brehm answered 15/11, 2011 at 20:6 Comment(4)
Thank you for your reply! I think I can't apply it to my current situation since the column width and height is subjected to the length of the longest word in the same column where i need to put an image. But I appreciate your answer so much.Lambaste
No problem, this situation has happened to me before as well. The above works IF you can fix the width/height of the cells, but in the case where you cannot (such as when you set AUTOSIZE to true on columns), I haven't found a way to do so. If you do find a way to do so in jxl, be sure to post here, as it may help others.Brehm
@shareef If you have to have your columns at a dynamic width, such as when using AUTOSIZE, I've not found a way to do this properly still. I haven't had looked into this issue for a good while, so maybe jxl has handled it in an update, but I am unaware. That'd be your best bet, since I haven't found a good solution with the case that the row/column sizes are dynamic. :(Brehm
thanx for replying but what i need to set image size as like pixels 50,50 its a logo by the way and it seem hard to add it to excel because its getting streched according to the cell witdh how to set it any ideasScent
G
0

Actually, this is possible. Assume that the width of the picture that you want to include is 4000. Then you do the following:

CellView cv = excelSheetTemp.getColumnView(0);
//get the width of the column where you want to insert the picture
int width = forLogo.getSize();
//if the width is less than the size you want, set the column width to
//the width. This will ensure that your image does not shrink
if (width < 4000) {
    forLogo.setSize(4000);
    excelSheetTemp.setColumnView(0, cv);
    width = 4000;
}
double c = 4000/width;
WritableImage im = new WritableImage(0, 1, c, 3, the image file);
excelSheetTemp.addImage(im);
Gastroenterology answered 15/8, 2016 at 12:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.