java修改ppt模板并导出

需求:公司要求将部分数据用ppt模板形式展示,里面的文字可以替换和修改!!!

所以自己开始尝试着去写个小demo

/**
 * 读取ppt文件流
 * @return
 * @throws Exception
 */
public InputStream readTemplateInput() throws Exception {
    FileInputStream fileInputStream=new FileInputStream(new File("D:/template.pptx"));
    return fileInputStream;
}
/**
 * 将文件流转pptx
 */
public XMLSlideShow convertPPtx(InputStream inputStream) throws IOException {
    XMLSlideShow xmlSlideShow=new XMLSlideShow(inputStream);
    return xmlSlideShow;
}
/**
 * 分析pptx
 */
public XMLSlideShow analysePPTX(XMLSlideShow xmlSlideShow) throws Exception {
    List<XSLFSlide> slides = xmlSlideShow.getSlides();
    for (XSLFSlide slide : slides) {
        List<XSLFShape> shapes = slide.getShapes();
        for (XSLFShape shape : shapes) {
            if(shape instanceof XSLFAutoShape){
                XSLFAutoShape autoShape = (XSLFAutoShape) shape;
                String text = autoShape.getText();
                if(text.contains("{customer}")){
                    XSLFTextRun textR = autoShape.setText(text.replace("{customer}", "中国移动"));
                    textR.setFontColor(Color.white);
                    textR.setFontFamily("微软雅黑");
                    textR.setFontSize(14.0);
                }
                continue;
            }
            if(shape instanceof XSLFTextBox){
                XSLFTextBox textBox=((XSLFTextBox) shape);
                String text = textBox.getText();
                if(text.contains("{customer}")){
                    TextRun textR=textBox.setText(text.replace("{customer}","中国移动"));
                    textR.setFontColor(Color.white);
                    textR.setFontFamily("微软雅黑");
                    textR.setFontSize(16.0);
                }
                continue;
            }
        }
    }
   return xmlSlideShow;
}
public static void main(String[] args) throws Exception{
    GenerateTempPpt g=new GenerateTempPpt();
    g.analysePPTX(g.convertPPtx(g.readTemplateInput())).write(new FileOutputStream("D:/1.template.pptx"));
}

这里有个问题,就是这种写法是可以替换文字但是样式也不再是之前固定好的样式,重新设置样式也没作用

通过搜阅一些资料发现这种使用的是单个文字,下面使用段落形式修改终于解决了样式不变的问题。

/**
 * 分析pptx
 */
public XMLSlideShow analysePPTX(XMLSlideShow xmlSlideShow) throws Exception {
    List<XSLFSlide> slides = xmlSlideShow.getSlides();

    for (XSLFSlide slide : slides) {
        List<XSLFShape> shapes = slide.getShapes();
        for (XSLFShape shape : shapes) {
            if(shape instanceof TextShape){
                List<XSLFTextParagraph> textParagraphs = ((TextShape) shape).getTextParagraphs();
                replaceTextShape(textParagraphs);
            }
        }
    }
   return xmlSlideShow;
}

private void replaceTextShape(List<XSLFTextParagraph> textParagraphs) {
    if(textParagraphs==null||textParagraphs.size()==0){
        return;
    }
    for (XSLFTextParagraph textParagraph : textParagraphs) {
        if(textParagraph==null){
            continue;
        }
        List<XSLFTextRun> textRuns = textParagraph.getTextRuns();
        if(textRuns==null){
            continue;
        }
        for (XSLFTextRun textRun : textRuns) {
            textRun.setText(textRun.getRawText().replace("{customer}","中国移动"));
        }
    }

}

 

通过以上修改后就可以只修改文字样式不再变化了!!!

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值