Poi xdoc ppt

来自ling
跳转至: 导航搜索

https://github.com/opensagres/xdocreport/wiki/PPTXReportingJavaMain

https://github.com/opensagres/xdocreport/wiki/XDocReport200

==

import org.apache.poi.sl.usermodel.TextRun;
import org.apache.poi.xslf.usermodel.*;

import java.io.*;


public class PptxTest {
    public static void main(String[] args) throws IOException {
        InputStream in = new FileInputStream("C:\\Users\\yaopchen\\Desktop\\LDD Report 0717.pptx");
        XMLSlideShow ppt = new XMLSlideShow(in);
        //埋点 查找table
        XSLFTable templateTable = getXSLFTable(ppt);
        //复制页
        copyPage(ppt.getSlides().get(0),ppt,2);

        XSLFTableCell cell = templateTable.getCell(0,0);

        XSLFTextParagraph p = cell.getTextParagraphs().get(0);

        TextRun textRun=p.getTextRuns().get(0);
        textRun.setText("hahaha");

        ppt.write(new FileOutputStream("C:\\Users\\yaopchen\\Desktop\\out.pptx"));
    }

    public static XSLFTable getXSLFTable(XMLSlideShow ppt) {
        for (XSLFSlide slide : ppt.getSlides()) {
            for (XSLFShape shape : slide.getShapes()) {
                if (shape instanceof XSLFTable) {
                    XSLFTableCell cell = ((XSLFTable) shape).getCell(0, 0);
                    if ("Table1".equals(cell.getText())) {
                        return ((XSLFTable) shape);
                    }
                }
            }
        }
        return null;
    }

    public static XSLFShape getXSLFShape(XMLSlideShow ppt) {
        for (XSLFSlide slide : ppt.getSlides()) {
            for (XSLFShape shape : slide.getShapes()) {
                if (shape instanceof XSLFTable) {
                    XSLFTableCell cell = ((XSLFTable) shape).getCell(0, 0);
                    if ("Table1".equals(cell.getText())) {
                        return shape;
                    }
                }
            }
        }
        return null;
    }
    public static XSLFSlide copyPage(XSLFSlide slide, XMLSlideShow ppt, int index) throws IOException {
        XSLFSlide slide2 = ppt.createSlide();
        slide2.importContent(slide);
        //排序(在PPT中的第几页)
        ppt.setSlideOrder(slide2, index);
        return slide2;
    }
}

==

  protected int getLineCount(String value,int maxCharCount){
        if(StringUtils.isBlank(value)){
            return 1;
        }
        int lineCount=0;
        String[] array=value.split("\n");
        for(String line :array){
            StringTokenizer st = new StringTokenizer(line," ,?.!:\"\"''\n#");
            int position=0;
            while (st.hasMoreTokens()){
                String currentWord= st.nextToken();
                int wordLength=currentWord.length();
                while (wordLength>maxCharCount){
                    //拆分+换行
                    if(position==0) {
                        wordLength = wordLength - maxCharCount;
                    }
                    lineCount++;
                    position=0;
                }
                if(wordLength+position>maxCharCount){
                    //放到下一行
                    lineCount++;
                    position=wordLength+1;
                } else {
                    //一个分隔符
                    position=position+wordLength+1;
                }
            }
            if(position>0){
                lineCount++;
            }
        }

        return lineCount;
    }
    protected int getLineHeight(String value,int maxCharCount){
        int lineCount=getLineCount(value,maxCharCount);
        return 24+(lineCount-1)*10;
    }