admin 管理员组

文章数量: 887016

参考文章:https://wwwblogs/yin1361866686/p/9561006.html

 

1:提供一个jar包的下载链接

链接:https://pan.baidu/s/1P58L_9EdjhcOPWlenI1xHQ 
提取码:q9cx

2:代码如下:

//调用如下
public static void main(String[] args) {
        //sohuyepath:第一个word文件路径
        //xuyepath:第二个word文件路径
		
	Document word=new Document(sohuyepath);
	Document word1=new Document(xuyepath);
	appendDoc(word, word1, true);
				
	word.save(sohuyepath);
}

private static void appendDoc(Document dstDoc, Document srcDoc, boolean includeSection) throws Exception {
       if (includeSection) {
           Iterator<Section> var3 = srcDoc.getSections().iterator();
           while (var3.hasNext()) {
               Section srcSection = (Section) var3.next();
               Node dstNode = dstDoc.importNode(srcSection, true, 0);
               dstDoc.appendChild(dstNode);
           }
       } else {
           Node node = dstDoc.getLastSection().getBody().getLastParagraph();
           if (node == null) {
               node = new Paragraph(srcDoc);
               dstDoc.getLastSection().getBody().appendChild(node);
           }

           if (node.getNodeType() != 8 & node.getNodeType() != 5) {
               throw new Exception("Use appendDoc(dstDoc, srcDoc, true) instead of appendDoc(dstDoc, srcDoc, false)");
           }

           insertDocumentAfterNode(node, dstDoc, srcDoc);
       }   



   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();

           while (null != srcDoc.getLastSection().getBody().getLastParagraph()
                   && !srcDoc.getLastSection().getBody().getLastParagraph().hasChildNodes()) {
               srcDoc.getLastSection().getBody().getLastParagraph().remove();
           }

           NodeImporter importer = new NodeImporter(srcDoc, mainDoc, 1);
           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;
               }
           }

       }
   }

  
  
       }

 

本文标签: 两个 文档 aspose java Word