Insert image into excel file using JXL without stretching it

I can insert an image into my excel file using jxl using sheet.addImage(WritableImage obj) . My problem is that it is stretched based on WritableImage arguments. I am wondering if there is a way so that the image that I insert is not stretched, as if I were inserting an image of size 200x200, it would appear on the sheet as 200x200.

+7
source share
2 answers

As far as I was concerned about jxl, I never found a way to insert an image without associating the aspect ratio with the cells instead of pixels / inches / any standard unit of measurement, and I have done decent research in the past with this.

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

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 maintain aspect ratio, you must also set the WritableImage parameter to not change if the user changes the row height or column width. Do this with any of the following (the preference depends on whether you want the image snapping to be locked or to move with resizing):

 WritableImage.MOVE_WITH_CELLS; WritableImage.NO_MOVE_OR_SIZE_WITH_CELLS; 
+6
source

Actually, this is possible. Suppose that the width of the image 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); 
0
source

All Articles