简体   繁体   中英

Apache POI disable auto resize of picture

I am using Apache POI to put an image into an Excel sheet. When I open the file with Excel and I resize manually the cell containing the image, the image gets resized too.

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. 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:

<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>

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