失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > apache poi 实现将PPT()中指定单元格替换成指定字符串或图片

apache poi 实现将PPT()中指定单元格替换成指定字符串或图片

时间:2019-10-09 01:16:10

相关推荐

apache poi 实现将PPT()中指定单元格替换成指定字符串或图片

目前PPT文件的格式有两种:(97-)版本的后缀为.ppt , (和)版本的后缀为.pptx.

.ppt和.pptx的区别在于:

.pptx是office ,的默认格式 .docx、.xlsx以及.pptx是基于XML的文件格式,因此多了个X.微软提到这个新的格式家族为“Microsoft Office Open XML Formats”。Open XML格式具有很多优点,可以减小文件大小,提高安全性和可靠性,还能增加文件与外部源相集成的能力。

.ppt是office 的默认格式,是基于二进制的文件格式。

具体的.pptx文件格式的优势:

/link?url=0xhLpnT0eaPTx1LV_8bu-6BI-B4civqQwA27La6Cdsm0YayJiPXL1OueecKGq9s8MPX6WmvpOhsw54txfsADt3RIk_WD61FSOEUdK_RJN3ya

POI API Documentation :/apidocs/index.html

Apache Poi:/

.pptx主要使用XSLF接口进行操作,.ppt主要使用HSLF接口进行操作。

1 package poi_ppt; 2 3 import java.awt.geom.Rectangle2D; 4 import java.io.FileInputStream; 5 import java.io.FileOutputStream; 6 7 import org.apache.poi.util.IOUtils; 8 import org.apache.poi.xslf.usermodel.XMLSlideShow; 9 import org.apache.poi.xslf.usermodel.XSLFGroupShape;10 import org.apache.poi.xslf.usermodel.XSLFPictureData;11 import org.apache.poi.xslf.usermodel.XSLFPictureShape;12 import org.apache.poi.xslf.usermodel.XSLFShape;13 import org.apache.poi.xslf.usermodel.XSLFSlide;14 import org.apache.poi.xslf.usermodel.XSLFTextShape;15 16 /**17 * Apache poi 操作 Powerpoint 文档18 * 获取ppt的内容并输出到console,遇到ppt中的第一张图片输出images1.class,第二张为images2.class,以此类推19 * 并将ppt中的"{word}"字符串替换成指定字符串,将"{picture}"字符串替换成指定的图片20 * 21 * (尚未解析出来的):22 * org.apache.poi.xslf.usermodel.XSLFGraphiFrame23 * org.apache.poi.xslf.usermodel.XSLFTable 24 * @author lin 7月20日25 */26 27 public class Replaceppt {28@SuppressWarnings("unused")29public static void main(String[] args) throws Exception {30 //获取ppt文件31 FileInputStream is = new FileInputStream("E:\\abc.pptx");32 XMLSlideShow ppt = new XMLSlideShow(is);33 is.close();34 // 获取幻灯片35 for (XSLFSlide slide : ppt.getSlides()) {36 // 获取每一张幻灯片中的shape37 for (XSLFShape shape : slide.getShapes()) {38 // position on the canvas39 Rectangle2D anchor = shape.getAnchor(); 40 if (shape instanceof XSLFTextShape) {41 XSLFTextShape txShape = (XSLFTextShape) shape;42 System.out.println(txShape.getText());43 if (txShape.getText().contains("{word}")) {44// 替换文字内容45txShape.setText(txShape.getText().replace("{word}","你好,你来自哪里?"));46 } else if (txShape.getText().contains("{picture}")) {47// 替换图片48byte[] pictureData = IOUtils.toByteArray(new FileInputStream("E:\\33.png"));49int idx = ppt.addPicture(pictureData,XSLFPictureData.PICTURE_TYPE_PNG);50XSLFPictureShape pic = slide.createPicture(idx);51// 设置XSLFPictureShape的位置信息52pic.setAnchor(anchor);53// 移除XSLFTextShape54slide.removeShape(txShape);55 }56 } else if (shape instanceof XSLFGroupShape) {57 for (XSLFShape sunshape : ((XSLFGroupShape) shape).getShapes()) {58XSLFTextShape txSunShape = (XSLFTextShape) sunshape;59System.out.println(txSunShape.getText());60if (txSunShape.getText().contains("{word}")) {61 // 替换文字内容62 txSunShape.setText(txSunShape.getText().replace("{word}", "你好,你来自哪里?"));63} else if (txSunShape.getText().contains("{picture}")) {64 // 替换图片65 byte[] pictureData = IOUtils.toByteArray(new FileInputStream("E:\\33.png"));66 int idx = ppt.addPicture(pictureData,XSLFPictureData.PICTURE_TYPE_PNG);67 XSLFPictureShape pic = slide.createPicture(idx);68 slide.removeShape(txSunShape);69 pic.setAnchor(anchor);70}71 }72 } else if (shape instanceof XSLFPictureShape) {73 XSLFPictureShape pShape = (XSLFPictureShape) shape;74 XSLFPictureData pData = pShape.getPictureData();75 System.out.println(pData.getFileName());76 } else {77 System.out.println("Process me: " + shape.getClass());78 }79 }80 }81 82 FileOutputStream out = new FileOutputStream("E:\\abc(2).pptx");83 ppt.write(out);84 out.close();85}86 87 }

如果觉得《apache poi 实现将PPT()中指定单元格替换成指定字符串或图片》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。