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;
}
}
}
}
}
版权声明:本文标题:java通过aspose.words将两个word文档合并 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.freenas.com.cn/jishu/1726312388h934664.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论