最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java實(shí)現(xiàn)合并word文檔的示例代碼

 更新時(shí)間:2022年08月10日 14:49:30   作者:江西-吳彥祖  
在做項(xiàng)目中,經(jīng)常會(huì)遇到一種情況,需要將一個(gè)小word文檔的內(nèi)容插入到一個(gè)大word(主文檔)中。本文就為大家準(zhǔn)備了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)文章

  • Java安全之CC1利用鏈詳解

    Java安全之CC1利用鏈詳解

    這篇文章主要介紹了Java安全之CC1利用鏈的使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2025-05-05
  • Java中 shuffle 算法的使用

    Java中 shuffle 算法的使用

    本篇文章,小編將為大家介紹,在Java中 shuffle 算法的使用,有需要的朋友可以參考一下
    2013-04-04
  • Java調(diào)用SQL腳本執(zhí)行常用的方法示例

    Java調(diào)用SQL腳本執(zhí)行常用的方法示例

    這篇文章主要給大家介紹了關(guān)于Java調(diào)用SQL腳本執(zhí)行常用的方法的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2018-04-04
  • 基于swagger參數(shù)與實(shí)體中參數(shù)不一致的原因分析

    基于swagger參數(shù)與實(shí)體中參數(shù)不一致的原因分析

    這篇文章主要介紹了基于swagger參數(shù)與實(shí)體中參數(shù)不一致的原因分析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-11-11
  • Mybatis insert方法主鍵回填和自定義操作

    Mybatis insert方法主鍵回填和自定義操作

    這篇文章主要介紹了Mybatis insert方法主鍵回填和自定義操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • SpringBoot啟動(dòng)yaml報(bào)錯(cuò)的解決

    SpringBoot啟動(dòng)yaml報(bào)錯(cuò)的解決

    這篇文章主要介紹了SpringBoot啟動(dòng)yaml報(bào)錯(cuò)的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • 基于Java編寫一個(gè)數(shù)據(jù)庫比較工具類

    基于Java編寫一個(gè)數(shù)據(jù)庫比較工具類

    這篇文章主要為大家詳細(xì)介紹了如何基于Java編寫一個(gè)數(shù)據(jù)庫比較工具類,其中比較結(jié)果會(huì)以現(xiàn)數(shù)據(jù)庫的視角說明,感興趣的小伙伴可以了解一下
    2023-07-07
  • 在springboot中使用AOP進(jìn)行全局日志記錄

    在springboot中使用AOP進(jìn)行全局日志記錄

    這篇文章主要介紹就在springboot中使用AOP進(jìn)行全局日志記錄,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • Java實(shí)現(xiàn)EasyCaptcha圖形驗(yàn)證碼的具體使用

    Java實(shí)現(xiàn)EasyCaptcha圖形驗(yàn)證碼的具體使用

    Java圖形驗(yàn)證碼,支持gif、中文、算術(shù)等類型,可用于Java Web、JavaSE等項(xiàng)目,下面就跟隨小編一起來了解一下
    2021-08-08
  • Spring Boot CORS 配置方法允許跨域請(qǐng)求的最佳實(shí)踐方案

    Spring Boot CORS 配置方法允許跨域請(qǐng)求的最佳實(shí)踐方案

    跨域請(qǐng)求在現(xiàn)代Web開發(fā)中非常重要,特別是在涉及多個(gè)前端和后端服務(wù)時(shí),本文詳細(xì)介紹了跨域請(qǐng)求的背景、重要性以及如何解決跨域問題,通過SpringBoot框架的CORS配置,可以有效地處理跨域請(qǐng)求,確保數(shù)據(jù)傳輸?shù)陌踩院陀脩趔w驗(yàn),感興趣的朋友跟隨小編一起看看吧
    2024-11-11

最新評(píng)論

星子县| 台东市| 离岛区| 进贤县| 黄平县| 林州市| 茌平县| 黑山县| 台前县| 达日县| 息烽县| 沐川县| 通海县| 通江县| 科技| 宜宾市| 多伦县| 光泽县| 西畴县| 铜山县| 曲周县| 清河县| 普宁市| 疏勒县| 册亨县| 赤水市| 调兵山市| 故城县| 彭泽县| 道孚县| 陆良县| 利津县| 甘孜县| 阿克苏市| 鸡西市| 资溪县| 饶阳县| 湘西| 开平市| 汽车| 聊城市|