Apache POI-HSSF在将图像添加到Excel单元格时失真图像大小 [英] Apache POI-HSSF Distorts Image Size when Adding Picture into Excel Cell
问题描述
如何保留原始大小?
我的代码:
byte [] bytes = IOUtils.toByteArray(is);
int pictureIdx = wb.addPicture(bytes,Workbook.PICTURE_TYPE_JPEG);
is.close();
//添加图片形状
CreationHelper helper = wb.getCreationHelper();
ClientAnchor anchor = helper.createClientAnchor();
//创建绘图族长。这是所有形状的顶级容器。
绘图= sheet1.createDrawingPatriarch();
//设置图片的左上角,
//图片#resize()的后续调用将相对于它操作
anchor.setAnchorType(0);
anchor.setCol1(1);
anchor.setRow1(1);
图片pict = drawing.createPicture(anchor,pictureIdx);
//相对于其左上角的自动大小图片
pict.resize();
我已经尝试过所有dx / dy坐标和Col / Row。这个位置并不重要,它的问题是水平地拉伸图像。感谢
我一直面临类似的问题,我添加的图像变得扭曲。我试过pict.resize()和sheet.autoSizeColumn()但它没有工作。最后我找到了以下URL: -
https://svn.apache.org/repos/asf/poi/trunk/src/examples/src/org/apache/poi/ss/examples/AddDimensionedImage.java
我将上面的类添加到我的代码中,并使用它将图像添加到excel中。我能够添加图像很少失真。希望这也有助于你。我写下面的代码: -
BufferedImage imageIO = ImageIO.read(new URL(image));
int height = imageIO.getHeight();
int width = imageIO.getWidth();
int relativeHeight =(int)(((double)height / width)* 28.5);
new AddDimensionedImage()。addImageToSheet(2,sheet.getPhysicalNumberOfRows() - 1,sheet,sheet.createDrawingPatriarch(),new URL(image),30,relativeHeight,AddDimensionedImage.EXPAND_ROW);
I am adding a picture into a cell using Apache POI-HSSF. The image is 120x100 but no matter what I do and how I resize it, the Excel spreadsheet always shows it spanning multiple rows and distorts it to a much bigger height than width.
How do I keep the original size?
My code:
InputStream is = new FileInputStream(getImageURL());
byte[] bytes = IOUtils.toByteArray(is);
int pictureIdx = wb.addPicture(bytes, Workbook.PICTURE_TYPE_JPEG);
is.close();
//add a picture shape
CreationHelper helper = wb.getCreationHelper();
ClientAnchor anchor = helper.createClientAnchor();
// Create the drawing patriarch. This is the top level container for all shapes.
Drawing drawing = sheet1.createDrawingPatriarch();
//set top-left corner of the picture,
//subsequent call of Picture#resize() will operate relative to it
anchor.setAnchorType(0);
anchor.setCol1(1);
anchor.setRow1(1);
Picture pict = drawing.createPicture(anchor, pictureIdx);
//auto-size picture relative to its top-left corner
pict.resize();
I've tried all dx/dy coordinates and Col/Row. The position doesn't matter, the problem it stretches the image horizontally. Thanks
I had been facing the similar issue where the image I added was getting distorted. I tried pict.resize() and sheet.autoSizeColumn() but it didn't work. Finally I found the below URL:- https://svn.apache.org/repos/asf/poi/trunk/src/examples/src/org/apache/poi/ss/examples/AddDimensionedImage.java
I added the above class into my code and used it's method to add the image into excel. I was able to add the image with little distortion. Hope this helps to you also. I wrote below code:-
BufferedImage imageIO = ImageIO.read(new URL(image));
int height= imageIO.getHeight();
int width=imageIO.getWidth();
int relativeHeight=(int)(((double)height/width)*28.5);
new AddDimensionedImage().addImageToSheet(2, sheet.getPhysicalNumberOfRows()-1 , sheet, sheet.createDrawingPatriarch(),new URL(image), 30, relativeHeight, AddDimensionedImage.EXPAND_ROW);
这篇关于Apache POI-HSSF在将图像添加到Excel单元格时失真图像大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!