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

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

    I created another empty image with the width taken from sheet.getColumnWidthInPixels, draw the necessary image over it and used AddDimensionedImage.EXPAND_ROW_AND_COLUMN to fit it exactly into the cell:

        HSSFRow imageRow = sheet.createRow(row);
    
        BufferedImage image = ImageIO.read(Paths.get(pathToImage).toUri().toURL());
    
        AddDimensionedImage addImage = new AddDimensionedImage();
        float columnWidth = sheet.getColumnWidthInPixels(0);
    
        BufferedImage off_Image = new BufferedImage((int) columnWidth, actualImageHeight, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2 = off_Image.createGraphics();
        g2.setColor(new Color(192,192,192));
        g2.fillRect(0, 0, off_Image.getWidth(), off_Image.getHeight());
        g2.drawImage(image, 0, 0, null);
        g2.dispose();
    
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write(off_Image, "jpg", baos);
        byte[] bytes = baos.toByteArray();
    
        double reqImageWidthMM = ((double) off_Image.getWidth()) / ConvertImageUnits.PIXELS_PER_MILLIMETRES;
        double reqImageHeightMM = ((double) off_Image.getHeight()) / ConvertImageUnits.PIXELS_PER_MILLIMETRES;
    
        addImage.addImageToSheet("A1", sheet, bytes, reqImageWidthMM, reqImageHeightMM, AddDimensionedImage.EXPAND_ROW_AND_COLUMN);
    

    I had to copy AddDimensionedImage.java to my classpath and overload addImageToSheet to accept byte array.

    Before that I tried other options mentioned here but it didn't help. Hope my answer will be useful to someone.

    0 讨论(0)
  • 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);
    
    0 讨论(0)
  • 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);
    
    0 讨论(0)
  • 2021-02-15 12:18

    As far as i understood from documentation of Apache POI, it is because of pict.resize(); ,as it says here that if the default font size for the workbook was changed, the picture might get stretched vertically or horizontally.

    0 讨论(0)
提交回复
热议问题