简体   繁体   中英

The size of an image is different from sheet to sheet in XSSFWorkbook

I'm trying to insert a logo in every sheet of excel below is the code snippet:

int numberOfSheets = wb.getNumberOfSheets();
for(int i=0;i<numberOfSheets;i++)
{
    XSSFSheet sheet = wb.getSheetAt(i);

    try {
        //insert a logoS
        String location = "/src/main/resources/logo/logo.jpg";
        InputStream is = new FileInputStream(location);
        byte[] bytes = IOUtils.toByteArray(is);
        int pictureIdx = wb.addPicture(bytes, Workbook.PICTURE_TYPE_JPEG);
        is.close();

        CreationHelper creationHelper = wb.getCreationHelper();
        Drawing drawing = sheet.createDrawingPatriarch();

        ClientAnchor anchor = creationHelper.createClientAnchor();
        anchor.setCol1(0);
        anchor.setRow1(0);
        Picture pict = drawing.createPicture(anchor, pictureIdx);
        pict.resize(3.0,3.0);
    } catch (FileNotFoundException e) {
        log.info("Problem in reading logo.jpg");
    } catch (IOException io) {
        log.info("Problem in reading logo.jpg");
    }
}

But in the result it does add an image in every sheet. I have 2 sheets in excel but the problem is when I click on 2nd sheet the image get stretched a little bit. I have resize it explicitly, if I dont put any number for resize then the stretched ratio is measurable.

What I can get from XSSFPicture Document

public void resize(double scaleX,double scaleY)

Resize the image relatively to its current size. Please note, that this method works correctly only for workbooks with the default font size (Calibri 11pt for .xlsx). If the default font is changed the resized image can be streched vertically or horizontally.

So my settings are also default as per stated above in

通过将列大小设置为其默认值可以解决此问题,我已经设置了无法调整大小功能的列大小,但是如果我们将电子表格以及列和行大小创建为其默认值,那么就不会有任何问题。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM