Java – Image stretched in Excel using POI

apache-poiexceljava

In my application I'm generating an Excel file using the POI API. One of the cells, the top left, of the document holds an image. The problem I'm getting is that the image is being stretched to fill the cell that holds it.

Depending on which resize-methods I use on the Picture object the image appears in various sizes but it always has the horizontal-vertical ratio that the cell which it is inside has, in other words it won't keep its own ratio.

This is my code:

titleRow = sheet.createRow(0);
            titleRow.setHeightInPoints(25f);

            titleRow.createCell(0);

            sheet.getRow(0).getCell(0).setCellStyle(defaultTitleStyle(wb));

            WebApplicationContext webCtx = ((WebApplicationContext)AtlasApplicationUtils.getCurrentApplication().getContext());
            ServletContext sc = webCtx.getHttpSession().getServletContext();
            String contextPath = sc.getContextPath();
//          sc.getResourceAsStream(contextPath + "/VAADIN/themes/m2m/../m2m/img/gnd_logo_white.png")
            String f = new File("").getAbsolutePath();  
            InputStream is = new FileInputStream(System.getProperty("user.dir") + "/src/main/webapp/VAADIN/themes/m2m/img/gnd_logo_white.png");
            byte[] bytes = IOUtils.toByteArray(is);
            int pictureIdx = wb.addPicture(bytes, Workbook.PICTURE_TYPE_PNG);
            is.close();

            CreationHelper helper = wb.getCreationHelper();
            Drawing drawing = sheet.createDrawingPatriarch();
            ClientAnchor anchor = helper.createClientAnchor();

            anchor.setCol1(0);
            anchor.setRow1(0);
            sheet.autoSizeColumn(0);
            Picture pict = drawing.createPicture(anchor, pictureIdx);
            pict.getPreferredSize();
            pict.resize(0.25);

The styling method:

protected CellStyle defaultTitleStyle(final HSSFWorkbook wb) {

        CellStyle style;
        final Font titleFont = wb.createFont();


        titleFont.setFontHeightInPoints((short) 18);
        titleFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
        titleFont.setColor(IndexedColors.GREY_80_PERCENT.getIndex());

        style = wb.createCellStyle();
        style.setAlignment(CellStyle.ALIGN_CENTER);
        style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
        style.setFillForegroundColor(IndexedColors.BLUE_GREY.getIndex());
        style.setFillPattern(CellStyle.SOLID_FOREGROUND);
//        style.setFillBackgroundColor(IndexedColors.BLUE_GREY.getIndex());
        style.setFont(titleFont);

        HSSFPalette palette = wb.getCustomPalette();
        palette.setColorAtIndex(HSSFColor.BLUE_GREY.index, 
                (byte) 7, 
                (byte) 64, 
                (byte) 127);

        palette.setColorAtIndex(HSSFColor.GREY_80_PERCENT.index, 
                (byte) 228, 
                (byte) 228, 
                (byte) 228);


        return style;

Does anyone know of a way to avoid this problem and let the image keep its original ratio?

Best Answer

There is a bit of warning in the documentation.

void resize()

Reset the image to the original size.

Please note, that this method works correctly only for workbooks with default font size (Arial 10pt for .xls and Calibri 11pt for .xlsx). If the default font is changed the resized image can be streched vertically or horizontally.