合并两个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文档中,最后在输出来. ...
随机推荐
- 我的Grunt之旅-序章
时间:2018-03-05 13:52 事件:安装Grunt 相关网址: grunt官网:https://gruntjs.com node.js下载地址 :https://nodejs.org/en ...
- db.mybatis.config
config1: <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryB ...
- 手把手教你如何玩转CLion
声明:配置是基于CLion的2019.1版本 〇.CLion简介 一.安装 \(JetBrains\)官方下载地址:CLion2019.3 百度网盘:CLion2019.1 个人觉得还是2019.1版 ...
- ①java环境变量配置以及简单的dos框操作
DOS:英文全称Disk Operation System,即磁盘操作系统,是个人电脑上使用的一种操作系统,并曾是世界上最广泛使用的操作系统. JDK: 英文全称Java Development Ki ...
- 吴裕雄 Bootstrap 前端框架开发——Bootstrap 字体图标(Glyphicons):glyphicon glyphicon-road
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ...
- NOR Flash驱动
驱动程序 1 ] ] );81 ;83 }84 85 86 static void __exit nor_exit(void)87 {88 iounmap(nor_ ...
- IIC协议解析
(1)概述 I2C(Inter-Integrated Circuit BUS) 集成电路总线,该总线由NXP(原PHILIPS)公司设计,多用于主控制器和从器件间的主从通信,在小数据量场合使用,传输距 ...
- (九)微信小程序---for指令
对于数据是列表 wxml <view wx:for="{{dataList}}">{{index}}-{{item}}</view> 我们可以看到上面的代码 ...
- SQL注入过WAF(11.4 第三十三天)
WAF是什么? Web应用防护系统(也称:网站应用级入侵防御系统.英文:Web Application Firewall,简称: WAF).也叫Web防火墙,主要是对Web特有入侵方式的加强防护,如D ...
- MyBatis parameterType、resultType的数据类型
parameterType.resultType的数据类型要写全限定类名或者别名. mybatis已经给常用的数据类型起好了别名,参考mybatis.pdf 3.1.1小节: 基本数据类型很少用,一 ...