Apache POI-HSSF distorts image size when adding picture into Excel cell

前端 未结 4 1521
走了就别回头了
走了就别回头了 2021-02-15 11:37

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 multipl

4条回答
  •  走了就别回头了
    2021-02-15 12:06

    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/examples/ss/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);
    

提交回复
热议问题