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


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

    .ppt和.pptx的区别在于:

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

    .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.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 操作2007 Powerpoint 文档
    18  * 获取ppt的内容并输出到console,遇到ppt中的第一张图片输出images1.class,第二张为images2.class,以此类推
    19  * 并将ppt中的"{word}"字符串替换成指定字符串,将"{picture}"字符串替换成指定的图片
    20  * 
    21  * (尚未解析出来的):
    22  *  org.apache.poi.xslf.usermodel.XSLFGraphiFrame
    23  *  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         XMLSlideShow ppt = new XMLSlideShow(is);
    33         is.close();
    34         // 获取幻灯片
    35         for (XSLFSlide slide : ppt.getSlides()) {
    36             // 获取每一张幻灯片中的shape
    37             for (XSLFShape shape : slide.getShapes()) {
    38                 // position on the canvas
    39                 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                         // 移除XSLFTextShape
    54                         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 }
  • 相关阅读:
    万字总结:学习MySQL优化原理,这一篇就够了!
    sql中自连接的使用
    SQL 优化原则
    Thumbnailator java图片压缩,加水印,批量生成缩略图
    java使用Thumbnailator处理图片
    Mysql优化原则_小表驱动大表IN和EXISTS的合理利用
    MySQL千万级多表关联SQL语句调优
    了解MySQL联表查询中的驱动表,优化查询,以小表驱动大表
    【explain】MySQL联表查询中的驱动表
    pyCharm最新2018激活码
  • 原文地址:https://www.cnblogs.com/linyuhuan/p/4664861.html
Copyright © 2020-2023  润新知