Java實(shí)現(xiàn)合并word文檔的示例代碼
說明
在做項(xiàng)目中,遇到了一種情況,需要將一個(gè)小word文檔的內(nèi)容插入到一個(gè)大word(主文檔)中。
實(shí)現(xiàn)
1.首先定義好主文檔
在主文檔需要插入小word文檔的位置上添加一個(gè)書簽,這個(gè)書簽名字要記住,后面要用。
2.定義需要追加的文檔
3. 代碼實(shí)現(xiàn)
package com.test.word; import com.aspose.words.Body; import com.aspose.words.Bookmark; import com.aspose.words.BookmarkCollection; import com.aspose.words.CompositeNode; import com.aspose.words.Document; import com.aspose.words.DocumentBuilder; import com.aspose.words.ImportFormatMode; import com.aspose.words.Node; import com.aspose.words.NodeImporter; import com.aspose.words.Orientation; import com.aspose.words.PaperSize; import com.aspose.words.Section; public class Test1 { public static void main(String[] args) { try { //主文檔 Document mainDocument = new Document("F:\\test\\main.docx"); //需要進(jìn)行追加的文檔 Document addDocument = new Document("F:\\test\\add.docx"); //第四個(gè)參數(shù)是書簽名,需要和步驟1在大word文檔中定義的書簽名對(duì)上 appendDocument(mainDocument, addDocument, true, "shuqian1"); System.out.println("成功!"); //將最終合并完成后的文檔對(duì)象保存到文件中 mainDocument.save("F:\\test\\result.docx"); } catch (Exception e) { e.printStackTrace(); } } /** * @Description 文檔拼接 * @param mainDoc 主文檔 * @param addDoc 要拼接的文檔 * @param isPortrait 是否橫向拼接 * @param bookmark 書簽名稱,將add文檔拼接到主文檔哪個(gè)位置 */ public static void appendDocument(Document mainDoc, Document addDoc, boolean isPortrait, String bookmark) { DocumentBuilder builder = null; try { builder = new DocumentBuilder(mainDoc); BookmarkCollection bms = mainDoc.getRange().getBookmarks(); Bookmark bm = bms.get(bookmark); if (bm != null) { builder.moveToBookmark(bookmark, true, false); builder.writeln(); builder.getPageSetup().setPaperSize(PaperSize.A4); if (isPortrait) { builder.getPageSetup().setOrientation(Orientation.PORTRAIT); } else { builder.getPageSetup().setOrientation(Orientation.LANDSCAPE); } Node insertAfterNode = builder.getCurrentParagraph().getPreviousSibling(); insertDocumentAfterNode(insertAfterNode, mainDoc, addDoc); } } catch (Exception e) { e.printStackTrace(); } } /** * @Description * @param insertAfterNode 插入的位置 * @param mainDoc 主文檔 * @param srcDoc 要拼接進(jìn)去的文檔 * @Return void */ @SuppressWarnings("rawtypes") private static void insertDocumentAfterNode(Node insertAfterNode, Document mainDoc, Document srcDoc) throws Exception { if (insertAfterNode.getNodeType() != 8 && insertAfterNode.getNodeType() != 5) { throw new Exception("The destination node should be either a paragraph or table."); } else { CompositeNode dstStory = insertAfterNode.getParentNode(); Body body = srcDoc.getLastSection().getBody(); while (null != body.getLastParagraph() && !body.getLastParagraph().hasChildNodes()) { srcDoc.getLastSection().getBody().getLastParagraph().remove(); } NodeImporter importer = new NodeImporter(srcDoc, mainDoc, ImportFormatMode.KEEP_SOURCE_FORMATTING); int sectCount = srcDoc.getSections().getCount(); for (int sectIndex = 0; sectIndex < sectCount; ++sectIndex) { Section srcSection = srcDoc.getSections().get(sectIndex); int nodeCount = srcSection.getBody().getChildNodes().getCount(); for (int nodeIndex = 0; nodeIndex < nodeCount; ++nodeIndex) { Node srcNode = srcSection.getBody().getChildNodes().get(nodeIndex); Node newNode = importer.importNode(srcNode, true); dstStory.insertAfter(newNode, insertAfterNode); insertAfterNode = newNode; } } } } }
4. 成果展示
到此這篇關(guān)于Java實(shí)現(xiàn)合并word文檔的示例代碼的文章就介紹到這了,更多相關(guān)Java合并word文檔內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Springboot錯(cuò)誤處理機(jī)制實(shí)現(xiàn)原理解析
這篇文章主要介紹了springboot錯(cuò)誤處理機(jī)制實(shí)現(xiàn)原理解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04IntelliJ IDEA中SpringBoot項(xiàng)目通過devtools實(shí)現(xiàn)熱部署的方法
這篇文章主要介紹了IntelliJ IDEA中SpringBoot項(xiàng)目通過devtools實(shí)現(xiàn)熱部署的方法,本文分步驟給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-08-08關(guān)于jd-gui啟動(dòng)報(bào)This?program?requires?Java?1.8+的錯(cuò)誤問題及解決方法
最近,在Mac使用上JD-GUI啟動(dòng)時(shí)總是報(bào)錯(cuò),接下來通過本文給大家介紹關(guān)于jd-gui啟動(dòng)報(bào)this?program?requires?Java?1.8+的錯(cuò)誤問題及解決方法,需要的朋友可以參考下2022-05-05javaweb 國(guó)際化:DateFormat,NumberFormat,MessageFormat,ResourceBu
本文主要介紹javaWEB國(guó)際化的知識(shí),這里整理了詳細(xì)的資料及實(shí)現(xiàn)代碼,有興趣的小伙伴可以參考下2016-09-09Spring security基于數(shù)據(jù)庫(kù)中賬戶密碼認(rèn)證
這篇文章主要介紹了Spring security基于數(shù)據(jù)庫(kù)中賬戶密碼認(rèn)證,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03mybatis-plus 新增/修改如何實(shí)現(xiàn)自動(dòng)填充指定字段
這篇文章主要介紹了mybatis-plus 新增/修改實(shí)現(xiàn)自動(dòng)填充指定字段方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06關(guān)于SpringBoot3.x中spring.factories功能被移除的解決方案
這篇文章主要介紹了SpringBoot3.x中spring.factories功能被移除的解決方案,在配置好相關(guān)依賴、最小啟動(dòng)類和配置之后,發(fā)現(xiàn)項(xiàng)目無法啟動(dòng),于是根據(jù)啟動(dòng)上下文日志和按行DEBUG找到原因并且在等待組件升級(jí)兼容之前進(jìn)行臨時(shí)性解決,需要的朋友可以參考下2022-12-12sql于navicat中能運(yùn)行在mybatis中不能運(yùn)行的解決方案
這篇文章主要介紹了sql于navicat中能運(yùn)行在mybatis中不能運(yùn)行的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01Spring boot route Controller接收參數(shù)常用方法解析
這篇文章主要介紹了Spring boot route Controller接收參數(shù)常用方法解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-10-10