使用JXL将图像插入到excel文件中,而不拉伸它 [英] Insert image to excel file using JXL without stretching it

查看:831
本文介绍了使用JXL将图像插入到excel文件中,而不拉伸它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用jxl使用 sheet.addImage(WritableImage obj)将图像插入到我的excel文件中。我的问题是,它基于 WritableImage 的参数延伸。我想知道是否有一种方法,使我插入的图像不会像我插入一个200x200大小的图像一样伸展,它将以200x200的形式出现在表格中。

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.

推荐答案

尽管这已经使我了解了jxl,但我从来没有找到一种方法来插入图像,而不将宽高比与单元格相关联,而不是像素/英寸/任何标准的测量单位,我以前做过体面的研究。

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.

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

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()));

为了保持宽高比,您还需要将WritableImage设置为不重新调整大小用户更改行高或列宽。请执行以下任一操作(您的偏好如果您希望图像锚点锁定或随着调整大小移动):

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;

这篇关于使用JXL将图像插入到excel文件中,而不拉伸它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆