將多個 docx 文件使用 POI 進行合並,生成單個文檔,包含圖片


1 添加 maven 依賴,需要使用 poi 的依賴項

<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-scratchpad -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-scratchpad</artifactId>
        </dependency>


        <!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml-schemas -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml-schemas</artifactId>
        </dependency>

 

  •  對於對個文檔合並中間需要添加分頁符有兩種方式:段前添加分頁符、段后添加分頁符

段后添加分頁符示例代碼

//段后添加分頁符
XWPFDocument doc;
XWPFParagraph paragraph = doc.createParagraph();
paragraph.setPageBreak(true);

 

段前添加分頁符示例

//段前添加分頁符
XWPFDocument doc;
XWPFRun run = doc.getLastParagraph().createRun();
run.addBreak(BreakType.PAGE)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM