poi 删除excel图片并插入图片

/**
     * 删除图片
     * https://blog.csdn.net/weixin_42357331/article/details/112827963
     * @author tf
     * @date 2023-02-07 16:35
     * @param xssfPicture
     */
    private static void deleteCTAnchor(XSSFPicture xssfPicture) {

        XSSFDrawing drawing = xssfPicture.getDrawing();

        XmlCursor cursor = xssfPicture.getCTPicture().newCursor();

        cursor.toParent();

        if (cursor.getObject() instanceof org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTTwoCellAnchor) {

            for (int i = 0; i < drawing.getCTDrawing().getTwoCellAnchorList().size(); i++) {

                if (cursor.getObject().equals(drawing.getCTDrawing().getTwoCellAnchorArray(i))) {

                    drawing.getCTDrawing().removeTwoCellAnchor(i);

//                    System.out.println("TwoCellAnchor for picture " + xssfPicture + " was deleted.");

                }

            }

        } else if (cursor.getObject() instanceof org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTOneCellAnchor) {

            for (int i = 0; i < drawing.getCTDrawing().getOneCellAnchorList().size(); i++) {

                if (cursor.getObject().equals(drawing.getCTDrawing().getOneCellAnchorArray(i))) {

                    drawing.getCTDrawing().removeOneCellAnchor(i);

//                    System.out.println("OneCellAnchor for picture " + xssfPicture + " was deleted.");

                }

            }

        } else if (cursor.getObject() instanceof org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTAbsoluteAnchor) {

            for (int i = 0; i < drawing.getCTDrawing().getAbsoluteAnchorList().size(); i++) {

                if (cursor.getObject().equals(drawing.getCTDrawing().getAbsoluteAnchorArray(i))) {

                    drawing.getCTDrawing().removeAbsoluteAnchor(i);

//                    System.out.println("AbsoluteAnchor for picture " + xssfPicture + " was deleted.");

                }

            }

        }

    }

    @SneakyThrows
    public void handleSheet1Img(Workbook workbook){


        Sheet sheet = workbook.getSheetAt(0);

        String logoImgPath = "d://dell_offer_sheet_image.jpg";
    

        //画图的顶级管理器,一个sheet只能获取一个
        Drawing patriarch = sheet.createDrawingPatriarch();
        File file = new File(logoImgPath);
        try {
            BufferedImage bufferImg = ImageIO.read(file);
            ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
            ImageIO.write(bufferImg, "jpg", byteArrayOut);

            XSSFClientAnchor anchor2 = null;
            // 读取图片:https://blog.csdn.net/qq_43279637/article/details/120533464
            List list = ((XSSFSheet) sheet).getRelations();
            outer:
            for (POIXMLDocumentPart part : list) {
                if (part instanceof XSSFDrawing) {
                    XSSFDrawing drawing = (XSSFDrawing) part;
                    List shapes = drawing.getShapes();
                    for (XSSFShape shape : shapes) {
                        XSSFClientAnchor anchor = (XSSFClientAnchor) shape.getAnchor();
                        // 获取excel的行列当作map的key

                        //anchor主要用于设置图片的属性 https://blog.csdn.net/CSDNlzy/article/details/117822556
                        anchor2 = new XSSFClientAnchor(anchor.getDx1(), anchor.getDy1(), anchor.getDx2(),
                                anchor.getDy2(), anchor.getCol1(), anchor.getRow1(), anchor.getCol2(), anchor.getRow2());
                        anchor2.setAnchorType(anchor.getAnchorType());

                        //获取指定图片,删除  https://blog.csdn.net/weixin_42357331/article/details/112827963
                        deleteCTAnchor(((XSSFPicture) shape));

                        // 第一个图片就是公司logo
                        break outer;

                    }
                }
            }

            //插入图片 https://blog.csdn.net/weixin_41221687/article/details/108580951
            patriarch.createPicture(anchor2, workbook.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG));

        }finally {
            if(null != logoAffix) {
                if (null != file) {
                    file.deleteOnExit();
                }
            }
        }

    }

你可能感兴趣的:(poi 删除excel图片并插入图片)