欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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)文章

最新評(píng)論