用poi替换ppt中的文字和图片

try {
            // 获取PPT文件
             String pptModelPath =ConfigReadUtil.getInstance().getConfigItem("ppt_path");  //获取路径
            FileInputStream is = new FileInputStream(pptModelPath+"2.pptx");  //读pptx存放的路径

            XMLSlideShow ppt = new XMLSlideShow(is);

            is.close();

            for (XSLFSlide slide : ppt.getSlides()) {
                // 获取每一张幻灯片中的shape
                for (XSLFShape shape : slide.getShapes()) {
                    Rectangle2D anchor = shape.getAnchor();
                    if (shape instanceof XSLFTextShape) {
                        XSLFTextShape txShape = (XSLFTextShape) shape;
                        if (txShape.getText().contains("{time}")) {
                            // 替换文字内容
                            txShape.setText(txShape.getText().replace(
                                    "{time}", time1));
                        }else if (txShape.getText().contains("{eight}")) {
                            // 替换文字内容
                            txShape.setText(txShape.getText().replace(
                                    "{eight}", finalTime));
                        }else if (txShape.getText().contains("{pic}")) {  //替换图片
                             byte[] pictureData = IOUtils.toByteArray(new FileInputStream("C:\Users\gls\Desktop\1.jpg"));
                             int idx = ppt.addPicture(pictureData,XSLFPictureData.PICTURE_TYPE_PNG);
                            XSLFPictureShape pic = slide.createPicture(idx);
                             // 设置XSLFPictureShape的位置信息
                             pic.setAnchor(anchor);
                             // 移除XSLFTextShape
                             slide.removeShape(txShape);
                        }
                        else if (txShape.getText().contains("{today}")) {
                            // 替换文字内容
                            txShape.setText(txShape.getText().replace(
                                    "{today}", firstTime));
                        } else if (txShape.getText().contains("{tomorrow}")) {
                            // 替换文字内容
                            txShape.setText(txShape.getText().replace(
                                    "{tomorrow}", secondTime));
                        }else if (txShape.getText().contains("{the day after tomorrow}")) {
                            // 替换文字内容
                            txShape.setText(txShape.getText().replace(
                                    "{the day after tomorrow}", thirdTime));
                        }
                        else if (txShape.getText().contains("{dwfirstRain}")) {
                            // 替换文字内容
                            txShape.setText(txShape.getText().replace(
                                    "{dwfirstRain}", dwfirstRain));
                        } else if (txShape.getText().contains("{psfirstRain}")) {
                            txShape.setText(txShape.getText().replace(
                                    "{psfirstRain}", psfirstRain));
                        } else if (txShape.getText().contains("{dwsecondRain}")) {
                            txShape.setText(txShape.getText().replace(
                                    "{dwsecondRain}",dwsecondRain));
                        } else if (txShape.getText().contains("{pssecondRain}")) {
                            txShape.setText(txShape.getText().replace(
                                    "{pssecondRain}", pssecondRain));
                        } else if (txShape.getText().contains("{dwthirdRain}")) {
                            txShape.setText(txShape.getText().replace(
                                    "{dwthirdRain}",dwthirdRain));
                        } else if (txShape.getText().contains("{psthirdRain}")) {
                            txShape.setText(txShape.getText().replace(
                                    "{psthirdRain}",psthirdRain));
                        } else if (txShape.getText().contains("{dwFirstphenomena}")) {
                            txShape.setText(txShape.getText().replace("{dwFirstphenomena}",
                                    dwFirstphenomena));
                        } else if (txShape.getText().contains("{psFirstphenomena}")) {
                            txShape.setText(txShape.getText().replace("{psFirstphenomena}",
                                    psFirstphenomena));
                        } else if (txShape.getText().contains("{dwSecondphenomena}")) {
                            txShape.setText(txShape.getText().replace("{dwSecondphenomena}",
                                    dwSecondphenomena));
                        } else if (txShape.getText().contains("{psSecondphenomena}")) {
                            txShape.setText(txShape.getText().replace("{psSecondphenomena}",
                                    psSecondphenomena));
                        } else if (txShape.getText().contains("{dwThirdphenomena}")) {
                            txShape.setText(txShape.getText().replace("{dwThirdphenomena}",
                                    dwThirdphenomena));
                        } else if (txShape.getText().contains("{psThirdphenomena}")) {
                            txShape.setText(txShape.getText().replace("{psThirdphenomena}",
                                    psThirdphenomena));
                        } else if (txShape.getText().contains("{dwfirstMaxTemp}")) {
                            txShape.setText(txShape.getText().replace("{dwfirstMaxTemp}",
                                    dwfirstMaxTemp));
                        } else if (txShape.getText().contains("{psfirstMaxTemp}")) {
                            txShape.setText(txShape.getText().replace("{psfirstMaxTemp}",
                                    psfirstMaxTemp));
                        } else if (txShape.getText().contains("{dwsecondMaxTemp}")) {
                            txShape.setText(txShape.getText().replace(
                                    "{dwsecondMaxTemp}", dwsecondMaxTemp));
                        }else if (txShape.getText().contains("{pssecondMaxTemp}")) {
                            txShape.setText(txShape.getText().replace(
                                    "{pssecondMaxTemp}", pssecondMaxTemp));
                        }else if (txShape.getText().contains("{dwthirdMaxTemp}")) {
                            txShape.setText(txShape.getText().replace(
                                    "{dwthirdMaxTemp}", dwthirdMaxTemp));
                        }else if (txShape.getText().contains("{psthirdMaxTemp}")) {
                            txShape.setText(txShape.getText().replace(
                                    "{psthirdMaxTemp}", psthirdMaxTemp));
                        }else if (txShape.getText().contains("{dwfirstMinTemp}")) {
                            txShape.setText(txShape.getText().replace(
                                    "{dwfirstMinTemp}", dwfirstMinTemp));
                        }else if (txShape.getText().contains("{psfirstMinTemp}")) {
                            txShape.setText(txShape.getText().replace(
                                    "{psfirstMinTemp}", psfirstMinTemp));
                        }else if (txShape.getText().contains("{dwsecondtMinTemp}")) {
                            txShape.setText(txShape.getText().replace(
                                    "{dwsecondtMinTemp}", dwsecondtMinTemp));
                        }else if (txShape.getText().contains("{pssecondMinTemp}")) {
                            txShape.setText(txShape.getText().replace(
                                    "{pssecondMinTemp}", pssecondMinTemp));
                        }else if (txShape.getText().contains("{dwthirdMinTemp}")) {
                            txShape.setText(txShape.getText().replace(
                                    "{dwthirdMinTemp}", dwthirdMinTemp));
                        }else if (txShape.getText().contains("{psthirdMinTemp}")) {
                            txShape.setText(txShape.getText().replace(
                                    "{psthirdMinTemp}", psthirdMinTemp));
                        }else if (txShape.getText().contains("{dwWindDAndV}")) {
                            txShape.setText(txShape.getText().replace(
                                    "{dwWindDAndV}", dwWindDAndV));
                        }else if (txShape.getText().contains("{psWindDAndV}")) {
                            txShape.setText(txShape.getText().replace(
                                    "{psWindDAndV}", psWindDAndV));
                        }else if (txShape.getText().contains("{dwSeaWindDAndV}")) {
                            txShape.setText(txShape.getText().replace(
                                    "{dwSeaWindDAndV}", dwSeaWindDAndV));
                        }else if (txShape.getText().contains("{psSeaWindDAndV}")) {
                            txShape.setText(txShape.getText().replace(
                                    "{psSeaWindDAndV}", psSeaWindDAndV));
                        }else if (txShape.getText().contains("{zyPSMaxTemp}")) {
                            txShape.setText(txShape.getText().replace(
                                    "{zyPSMaxTemp}", zyPSMaxTemp));
                        }else if (txShape.getText().contains("{zyPSMinTemp}")) {
                            txShape.setText(txShape.getText().replace(
                                    "{zyPSMinTemp}", zyPSMinTemp));
                        }else if (txShape.getText().contains("{zyPSPhoenomena}")) {
                            txShape.setText(txShape.getText().replace(
                                    "{zyPSPhoenomena}", zyPSPhoenomena));
                        }else if (txShape.getText().contains("{zyPSWindD}")) {
                            txShape.setText(txShape.getText().replace(
                                    "{zyPSWindD}", zyPSWindD));
                        }else if (txShape.getText().contains("{zyPSWindV}")) {
                            txShape.setText(txShape.getText().replace(
                                    "{zyPSWindV}", zyPSWindV));
                        }else if (txShape.getText().contains("{zyDWMaxTemp}")) {
                            txShape.setText(txShape.getText().replace(
                                    "{zyDWMaxTemp}", zyDWMaxTemp));
                        }else if (txShape.getText().contains("{zyDWMinTemp}")) {
                            txShape.setText(txShape.getText().replace(
                                    "{zyDWMinTemp}", zyDWMinTemp));
                        }else if (txShape.getText().contains("{zyDWPhoenomena}")) {
                            txShape.setText(txShape.getText().replace(
                                    "{zyDWPhoenomena}", zyDWPhoenomena));
                        }else if (txShape.getText().contains("{zyDWWindD}")) {
                            txShape.setText(txShape.getText().replace(
                                    "{zyDWWindD}", zyDWWindD));
                        }else if (txShape.getText().contains("{zyDWWindV}")) {
                            txShape.setText(txShape.getText().replace(
                                    "{zyDWWindV}", zyDWWindV));
                        }
                    }  else if (shape instanceof XSLFPictureShape) {
                        XSLFPictureShape pShape = (XSLFPictureShape) shape;
                        XSLFPictureData pData = pShape.getPictureData();
                    } else {
                        System.out.println("Process me: " + shape.getClass());
                    }
                }
            }

            Object user = ServletActionContext.getRequest().getSession()
                    .getAttribute(ResourceUtil.getSessionInfoName());
            Date date = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss");
            SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd");
            String fileTime = sdf.format(date);
            String time= s.format(date);
             String filePath =ConfigReadUtil.getInstance().getConfigItem("FileSavePath");
        //    String filePath = "D:\Conference";
            filePath = filePath.replace("\", "\\");

            File dir = new File(filePath);
            if(!dir.exists()){
                dir.mkdirs();
            }
            String path = dir.getPath();
            path = path.replace("\", "\\");
            String fileName = "天气会商.pptx";

            String fileType = "pptx";
            WeatherConference c = new WeatherConference();
            c.setFileName(time+fileName);
            c.setFileType(fileType);
            c.setFileTime(fileTime);
            c.setFileUploader(user.toString());
            c.setFilePath(path+"\\"+time+"天气会商制作"+".pptx");
            conferenceManager.add(c);

            FileOutputStream out = new FileOutputStream(path+"\"+time+"天气会商制作"+".pptx");

            ppt.write(out);
            out.close();

原文地址:https://www.cnblogs.com/guolsblog/p/6051872.html