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

前端 未结 4 1597
走了就别回头了
走了就别回头了 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:11

    I've the same problem and my solution was copy this class in my project AddDimensionedImage and then used this method.

    protected void addImageInCell(Sheet sheet, URL url, Drawing drawing, int colNumber, int rowNumber) {
        BufferedImage imageIO = ImageIO.read(url);
        int height = imageIO.getHeight();
        int width = imageIO.getWidth();
        int relativeHeight = (int) (((double) height / width) * 28.5);
        new AddDimensionedImage().addImageToSheet(colNumber, rowNumber, sheet, drawing, url, 30, relativeHeight,
                AddDimensionedImage.EXPAND_ROW_AND_COLUMN);
    
    }
    

    you can call this method with follow line:

    URL url = new URL("https://blog.expedia.mx/por-que-los-viajeros-internacionales-visitan-mexico/");
    addImageInCell(sheet, url, sheet.createDrawingPatriarch(), 0, 0);
    

提交回复
热议问题