你的位置:首页 > 软件开发 > Java > apache poi 实现将PPT(2007)中指定单元格替换成指定字符串或图片

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

发布时间:2015-07-21 17:00:14
目前PPT文件的格式有两种:(97-2003)版本的后缀为.ppt , (2007和2010)版本的后缀为.pptx..ppt和.pptx的区别在于:.pptx是office 2007,2010的默认格式 .docx、.xlsx以及.pptx是基于.ppt是office 2003 ...

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

.ppt和.pptx的区别在于:

.pptx是office 2007,2010的默认格式 .docx、.xlsx以及.pptx是基于

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

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

http://wenku.baidu.com/link?url=0xhLpnT0eaPTx1LV_8bu-6BI-B4civqQwA27La6Cdsm0YayJiPXL1OueecKGq9s8MPX6WmvpOhsw54txfsADt3RIk_WD61FSOEUdK_RJN3ya

POI API Documentation :http://poi.apache.org/apidocs/index.html

Apache Poi:http://poi.apache.org/

.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. 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 操作2007 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 2015年7月20日25 */26 27 public class Replaceppt {28   @SuppressWarnings("unused")29   public static void main(String[] args) throws Exception {30     //获取ppt文件31     FileInputStream is = new FileInputStream("E:\\abc.pptx");32     new 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             // 替换文字内容45             txShape.setText(txShape.getText().replace("{word}","你好,你来自哪里?"));46           } else if (txShape.getText().contains("{picture}")) {47             // 替换图片48             byte[] pictureData = IOUtils.toByteArray(new FileInputStream("E:\\33.png"));49             int idx = ppt.addPicture(pictureData,XSLFPictureData.PICTURE_TYPE_PNG);50             XSLFPictureShape pic = slide.createPicture(idx);51             // 设置XSLFPictureShape的位置信息52             pic.setAnchor(anchor);53             // 移除XSLFTextShape54             slide.removeShape(txShape);55           }56         } else if (shape instanceof XSLFGroupShape) {57           for (XSLFShape sunshape : ((XSLFGroupShape) shape).getShapes()) {58             XSLFTextShape txSunShape = (XSLFTextShape) sunshape;59             System.out.println(txSunShape.getText());60             if (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 }

 

海外公司注册、海外银行开户、跨境平台代入驻、VAT、EPR等知识和在线办理:https://www.xlkjsw.com

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

关键词:Apache

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

可能感兴趣文章

我的浏览记录