简体   繁体   English

Apache POI 禁用图片自动调整大小

[英]Apache POI disable auto resize of picture

I am using Apache POI to put an image into an Excel sheet.我正在使用 Apache POI 将图像放入 Excel 表中。 When I open the file with Excel and I resize manually the cell containing the image, the image gets resized too.当我使用 Excel 打开文件并手动调整包含图像的单元格时,图像也会调整大小。

How can I insert a picture into a cell that does not resize depending on the size of the cell?如何将图片插入不根据单元格大小调整大小的单元格?

My code:我的代码:

private void addImageToExcelReport(InputStream is, OutputStream os, File image) {
    try (BufferedReader br = new BufferedReader(new FileReader(image))) {
        if (br.readLine() != null) {
            XSSFWorkbook wb = new XSSFWorkbook(is);
            XSSFSheet sheet = wb.getSheetAt(0);

            InputStream inputStream = new FileInputStream(image);
            byte[] bytes = IOUtils.toByteArray(inputStream);

            int pictureIdx = wb.addPicture(bytes, Workbook.PICTURE_TYPE_PNG);
            inputStream.close();

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

            anchor.setRow1(0);
            anchor.setRow2(1);
            anchor.setCol1(0);
            anchor.setCol2(1);

            drawing.createPicture(anchor, pictureIdx);

            wb.write(os);
            wb.close();
        }
    } catch (IOException e) {
        loggerService.traceError(e.getMessage(), e);
    }
}

As Alex Richter suggested, the answer is to set the anchor type.正如 Alex Richter 所建议的,答案是设置锚类型。 The following code works for me:以下代码适用于我:

private void addImageToExcelReport(InputStream is, OutputStream os, File image) {
    if (image != null) {
        try {
            Workbook wb = new XSSFWorkbook(is);
            Sheet sheet = wb.getSheetAt(0);

            InputStream inputStream = new FileInputStream(image);
            byte[] bytes = IOUtils.toByteArray(inputStream);

            int pictureIdx = wb.addPicture(bytes, Workbook.PICTURE_TYPE_PNG);
            inputStream.close();

            CreationHelper helper = wb.getCreationHelper();
            Drawing<?> drawing = sheet.createDrawingPatriarch();
            ClientAnchor anchor = helper.createClientAnchor();
            anchor.setAnchorType(AnchorType.DONT_MOVE_AND_RESIZE);
            sheet.createRow(1).createCell(0);

            anchor.setRow1(0);
            anchor.setRow2(1);
            anchor.setCol1(0);
            anchor.setCol2(1);

            drawing.createPicture(anchor, pictureIdx);

            wb.write(os);
            wb.close();

        } catch (IOException e) {
            loggerService.traceError(e.getMessage(), e);
        }
    }
}

Also, make sure to have the right librairies.此外,请确保拥有正确的库。 My pom.xml contains these librairies:我的 pom.xml 包含这些库:

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>4.0.1</version>
</dependency>

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>4.0.1</version>
</dependency>

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml-schemas</artifactId>
    <version>4.0.1</version>
</dependency>

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>ooxml-schemas</artifactId>
    <version>1.4</version>
</dependency>

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

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