Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6854665
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:39:42+00:00 2026-05-27T01:39:42+00:00

In my application I’m generating an Excel file using the POI API. One of

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T01:39:43+00:00Added an answer on May 27, 2026 at 1:39 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Application flow An input file consists of multiple logical documents. Extract one input logical
Application I develop requires several data sources (2 RDBMS and one file storage) to
Application is asp.net MVC. I want to put a textbox for date using mask.
Application: WPF Application consisting of a textbox on top and a listbox below Users
Application stores configuration data in custom section of configuration file. This information is used
Application : I am working on one mid-large size application which will be used
The application I'm currently writing is using MVVM with the ViewModel-first pattern. I have
my application use 10 threads that to read a lot of html file.similar the
Application level events and processes in Excel are restricted to a single instance of
My application fetches information from an API. Unfortunately their data isn't very pretty. This

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.