简体   繁体   English

XSSFWorkbook中图像的大小因工作表而异

[英]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: 我正在尝试在每张Excel表格中插入徽标,以下是代码段:

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. 我在Excel中有2张纸,但是问题是当我单击第二张纸时,图像有点拉伸了。 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 我可以从XSSFPicture文档中获得什么

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). 请注意,此方法仅适用于具有默认字体大小的工作簿(.libsx为Calibri 11pt)。 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 所以我的设置也是默认的,如上所述

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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