需求:公司要求将部分数据用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}","中国移动"));
        }
    }

}

 

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

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

更多推荐