gen*_* b. 8 java excel image hssf apache-poi
我正在使用Apache POI-HSSF将图片添加到单元格中.图像是120x100,但无论我做什么以及如何调整它,Excel电子表格总是显示它跨越多行并将其扭曲到比宽度更大的高度.
如何保持原始尺寸?
我的代码:
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();
Run Code Online (Sandbox Code Playgroud)
我已经尝试了所有的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);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8442 次 |
| 最近记录: |