合并两个word文档,保持样式不变
一、需求说明
例如将封面插入到word正文上方
二、导入依赖
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.1</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.1</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>4.1.1</version>
</dependency> <dependency>
<groupId>org.apache.xmlbeans</groupId>
<artifactId>xmlbeans</artifactId>
<version>3.1.0</version>
</dependency>
三、准备工作
准备两个word文档
四、代码
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.xwpf.usermodel.Document;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFPictureData;
import org.apache.xmlbeans.XmlOptions;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBody; public class test {
public static void main (String[] args) throws Exception { /*String[] str = {"街道(年度)","街道(季度)","街道(月度)","全区(年度)","全区(季度)","全区(月度)"};*/
String[] str = {"街道(年度)"};
for (int k = 0; k < str.length; k++) {
String type = str[k];
String path ="E:/1/确认打印报告/"+type+"/";
File fileRoot = new File(path);
List<File> subFileList = new ArrayList<File>();
getAllFile(subFileList,fileRoot);
for (File file : subFileList) {
String name = file.getName();
File newFile = new File("f:/final/"+type+"/"+name);
List<File> srcfile = new ArrayList<>();
File file1 = new File("E:/1/确认打印报告/"+type+"/"+name);
File file2 = new File("D:/word/封面/生成/"+type+"/"+name); srcfile.add(file1);
srcfile.add(file2); try {
OutputStream dest = new FileOutputStream(newFile);
ArrayList<XWPFDocument> documentList = new ArrayList<>();
XWPFDocument doc = null;
for (int i = 0; i < srcfile.size(); i++) {
FileInputStream in = new FileInputStream(srcfile.get(i).getPath());
OPCPackage open = OPCPackage.open(in);
XWPFDocument document = new XWPFDocument(open);
documentList.add(document);
in.close();
}
for (int i = 0; i < documentList.size(); i++) {
doc = documentList.get(0);
if(i != 0){
appendBody(doc,documentList.get(i));
}
}
// doc.createParagraph().setPageBreak(true);
doc.write(dest);
System.out.println(name);
doc.close();
dest.close();
} catch (Exception e) {
e.printStackTrace();
}
}
} } public static void appendBody(XWPFDocument src, XWPFDocument append) throws Exception {
CTBody src1Body = src.getDocument().getBody();
CTBody src2Body = append.getDocument().getBody(); List<XWPFPictureData> allPictures = append.getAllPictures();
// 记录图片合并前及合并后的ID
Map<String,String> map = new HashMap();
for (XWPFPictureData picture : allPictures) {
String before = append.getRelationId(picture);
//将原文档中的图片加入到目标文档中
String after = src.addPictureData(picture.getData(), Document.PICTURE_TYPE_PNG);
map.put(before, after);
} appendBody(src1Body, src2Body,null); } private static void appendBody(CTBody src, CTBody append,Map<String,String> map) throws Exception {
XmlOptions optionsOuter = new XmlOptions();
optionsOuter.setSaveOuter();
String appendString = append.xmlText(optionsOuter); String srcString = src.xmlText();
String prefix = srcString.substring(0,srcString.indexOf(">")+1);
String mainPart = srcString.substring(srcString.indexOf(">")+1,srcString.lastIndexOf("<"));
String sufix = srcString.substring( srcString.lastIndexOf("<") );
String addPart = appendString.substring(appendString.indexOf(">") + 1, appendString.lastIndexOf("<")); if (map != null && !map.isEmpty()) {
//对xml字符串中图片ID进行替换
for (Map.Entry<String, String> set : map.entrySet()) {
addPart = addPart.replace(set.getKey(), set.getValue());
}
}
//将两个文档的xml内容进行拼接
CTBody makeBody = CTBody.Factory.parse(prefix+addPart+mainPart+sufix); src.set(makeBody);
} private static void getAllFile(final List<File> subFileList, File fileRoot) {
fileRoot.listFiles(new FileFilter() {
@Override
public boolean accept(File file) {
if (file.isDirectory()) {
getAllFile(subFileList, file);
return false;
}else{
if(file.getName().endsWith(".docx")){
subFileList.add(file);
}
return false;
}
}
});
}
}
合并两个word文档,保持样式不变的更多相关文章
- C# 合并及拆分Word文档
本文简要分析一下如何如何使用C#简单实现合并和拆分word文档.平时我们在处理多个word文档时,可能会想要将两个文档合并为一个,或者是将某个文档的一部分添加到另一个文档中,有的时候也会想要将文档拆分 ...
- C#/VB.NET 比较两个Word文档差异
本文以C#和VB.NET代码为例,来介绍如何对比两个Word文档差异.程序中使用最新版的Spire.Doc for .NET 版本8.8.2.编辑代码前,先在VS程序中添加引用Spire.Doc.dl ...
- C# 实现将多个word文档合并成一个word文档的功能
前段时间项目上遇到这么一个需求,需要将多个OCR识别的word文档合并成一个,于是就在网上找了找,自己修改了一下.在这里跟大家分享一下,希望有用的到的. 要做多word文档合并,首先要导入Micros ...
- JAVA合并多个word文档根据文章标题生成目录
此产品版本是免费版的,我也是在用免费,除了只能单次识别25张一下的word和生成pdf有限制,其他的功能都和正式版差不多. 如果你几十个文档,每个文档几页,输出出来超过25页,那没关系,依然可以使用. ...
- Java 比较两个Word文档差异
本文介绍使用Spire.Doc for Java的比较功能来比较两个相似Word文档的差异.需要使用的版本为3.8.8或者后续发布的新版本.可下载jar包,解压将lib文件夹下的Spire.doc.j ...
- C# 之 比较两个word文档的内容
利用 Microsoft.Office.Interop.Word 组件进行比较.引入命名空间:using Word2013 = Microsoft.Office.Interop.Word; 代码如下: ...
- 用WPS查看两篇word文档异同之处
写的合同,后期又有修改,电脑里同样名字的合同有好几个版本,不知道有什么不同,怎么办? 打开wps-->[审阅]-->[比较],剩下的按照提示很容易,略...
- C#使用NPOI对Word文档进行导出操作的dll最新版2.5.1
Npoi导出非模板 最近使用NPOI做了个导出Word文档的功能,因为之前都是导出Excel很方便(不用模板),所以导出Word也选用了Npoi(也没有用模板,
- POI生成word文档完整案例及讲解
一,网上的API讲解 其实POI的生成Word文档的规则就是先把获取到的数据转成xml格式的数据,然后通过xpath解析表单式的应用取值,判断等等,然后在把取到的值放到word文档中,最后在输出来. ...
随机推荐
- eshop7-mysql
1. Mysql 安装 执行 yum -y install mysql-server 注意:(1)是否使用sudo 权限执行请根据您具体环境来决定 (2)检查是否已经安装mysql-server rp ...
- Day 23:JAVA SE复习
作业 1.多线程下载图片 import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream ...
- 这台计算机上缺少此项目引用的 NuGet 程序包。使用“NuGet 程序包还原”可下载这些程序包
将项目复制到其地方的时候编译会报错,按照官网方法也不行,从网上查了一个有用的方法如下 打开CSPROJ文件.删除如下代码, <Import Project="..\packages\ ...
- use matplotlib to draw scatter plot
There are many pionts in this kind of table. How to do it? We can use scatter() to draw it. Code: im ...
- Golang函数-匿名函数与闭包函数
Golang函数-匿名函数与闭包函数 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任.
- 基于 Annotation 的装配(注解)
注解:就是一个类,使用@注解名称 开发中:使用注解 取代 xml配置文件. 1. @Component取代<bean class=""> @Component(&quo ...
- P 1008 数组元素循环右移问题
转跳点:
- c/c++ 计算屏幕的PPI
PPI(pixels per inch)是图像分辨率的单位,表示的是每英寸所拥有的像素(pixel)数目.那如何计算勒?其实PPI计算有这相应的公式,公式为:sqrt(横向的平方+纵向的平方)/屏幕尺 ...
- spring boot 使用swagger
在pom.xml中添加maven依赖 <dependency> <groupId>io.springfox</groupId> <artifactId> ...
- 洛谷 三月月赛 C
呵呵呵呵,这个sb题做了好久,然并卵,还是不对. 挖坑++ 然而我感觉我做的对了,偷瞄了一下题解应该没什么问题. 这个题有n个点,n条边,所以是个基环树(我也不知道是不是这个名) 要每个点有联通,就是 ...