前言:

临时来了一条新的需求:多个doc文档进行合并。

在网上苦苦搜罗了很久才找到可用的文件(原文出处到不到了 所以暂时不能加链接地址了),现在记录下留给有需要的人。

一:doc转docx

所需jar包:链接: https://pan.baidu.com/s/1WQ33HDsON8lpFQKgLu8pCQ 提取码: n1xt

具体代码

public class Doc2Docx {
public static void main(String[] args) {
String docFile = "D:/files/dfa3cbb9-a0a0-497a-aa9f-d26cbee9a25b_linux.doc";
String docxFile = "D:/print/linux.docx";
doc2docx(docFile, docxFile);
} /**
* doc转docx
* @param docFile 源文件
* @param docxFile 目标文件
*/
public static void doc2docx(String docFile, String docxFile) {
Document doc;
try {
String tempFile = docxFile.substring(0, docxFile.lastIndexOf(".")) + "_temp"
+ docxFile.substring(docxFile.lastIndexOf("."), docxFile.length());
doc = new Document(docFile);
doc.save(tempFile);
Map<String, String> map = new HashMap<String, String>();
map.put("Evaluation Only. Created with Aspose.Words. Copyright 2003-2018 Aspose Pty Ltd.", "");
DocxReplace.replaceAndGenerateWord(tempFile, docxFile, map);
// forceDelete(new File(tempFile));
} catch (Exception e1) {
log.error("doc 2 docx exception:{}",e1.getMessage());
} } }
public class DocxReplace {
// 返回Docx中需要替换的特殊字符,没有重复项
// 推荐传入正则表达式参数"\\$\\{[^{}]+\\}"
public ArrayList<String> getReplaceElementsInWord(String filePath,
String regex) {
String[] p = filePath.split("\\.");
if (p.length > 0) {// 判断文件有无扩展名
// 比较文件扩展名
if (p[p.length - 1].equalsIgnoreCase("doc")) {
ArrayList<String> al = new ArrayList<>();
File file = new File(filePath);
HWPFDocument document = null;
try {
InputStream is = new FileInputStream(file);
document = new HWPFDocument(is);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Range range = document.getRange();
String rangeText = range.text();
CharSequence cs = rangeText.subSequence(0, rangeText.length());
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(cs);
int startPosition = 0;
while (matcher.find(startPosition)) {
if (!al.contains(matcher.group())) {
al.add(matcher.group());
}
startPosition = matcher.end();
}
return al;
} else if (p[p.length - 1].equalsIgnoreCase("docx")) {
ArrayList<String> al = new ArrayList<>();
XWPFDocument document = null;
try {
document = new XWPFDocument(
POIXMLDocument.openPackage(filePath));
} catch (IOException e) {
e.printStackTrace();
}
// 遍历段落
Iterator<XWPFParagraph> itPara = document
.getParagraphsIterator();
while (itPara.hasNext()) {
XWPFParagraph paragraph = (XWPFParagraph) itPara.next();
String paragraphString = paragraph.getText();
CharSequence cs = paragraphString.subSequence(0,
paragraphString.length());
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(cs);
int startPosition = 0;
while (matcher.find(startPosition)) {
if (!al.contains(matcher.group())) {
al.add(matcher.group());
}
startPosition = matcher.end();
}
}
// 遍历表
Iterator<XWPFTable> itTable = document.getTablesIterator();
while (itTable.hasNext()) {
XWPFTable table = (XWPFTable) itTable.next();
int rcount = table.getNumberOfRows();
for (int i = 0; i < rcount; i++) {
XWPFTableRow row = table.getRow(i);
List<XWPFTableCell> cells = row.getTableCells();
for (XWPFTableCell cell : cells) {
String cellText = "";
cellText = cell.getText();
CharSequence cs = cellText.subSequence(0,
cellText.length());
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(cs);
int startPosition = 0;
while (matcher.find(startPosition)) {
if (!al.contains(matcher.group())) {
al.add(matcher.group());
}
startPosition = matcher.end();
}
}
}
}
return al;
} else {
return null;
}
} else {
return null;
}
} // 替换word中需要替换的特殊字符
public static boolean replaceAndGenerateWord(String srcPath, String destPath, Map<String, String> map) {
String[] sp = srcPath.split("\\.");
String[] dp = destPath.split("\\.");
if ((sp.length > 0) && (dp.length > 0)) {// 判断文件有无扩展名
// 比较文件扩展名
if (sp[sp.length - 1].equalsIgnoreCase("docx")) {
try {
XWPFDocument document = new XWPFDocument(POIXMLDocument.openPackage(srcPath));
// 替换段落中的指定文字
Iterator<XWPFParagraph> itPara = document.getParagraphsIterator();
while (itPara.hasNext()) {
XWPFParagraph paragraph = (XWPFParagraph) itPara.next();
List<XWPFRun> runs = paragraph.getRuns();
for (int i = 0; i < runs.size(); i++) {
String oneparaString = runs.get(i).getText(runs.get(i).getTextPosition());
for (Entry<String, String> entry : map.entrySet()) {
if(oneparaString.indexOf(entry.getKey())!=-1){
oneparaString = oneparaString.replace(entry.getKey(), entry.getValue());
runs.get(i).setText(oneparaString, 0);
}
}
}
}
// 替换表格中的指定文字
Iterator<XWPFTable> itTable = document.getTablesIterator();
while (itTable.hasNext()) {
XWPFTable table = (XWPFTable) itTable.next();
int rcount = table.getNumberOfRows();
for (int i = 0; i < rcount; i++) {
XWPFTableRow row = table.getRow(i);
List<XWPFTableCell> cells = row.getTableCells();
for (XWPFTableCell cell : cells) {
String cellTextString = cell.getText();
for (Entry<String, String> e : map.entrySet()) {
if (cellTextString.contains(e.getKey())) {
cellTextString = cellTextString.replace(e.getKey(),e.getValue());
cell.removeParagraph(0);
cell.setText(cellTextString);
cell.setColor(cell.getColor());
}
}
}
}
}
FileOutputStream outStream = null;
outStream = new FileOutputStream(destPath);
document.write(outStream);
outStream.close();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
} else
// doc只能生成doc,如果生成docx会出错
if ((sp[sp.length - 1].equalsIgnoreCase("doc"))
&& (dp[dp.length - 1].equalsIgnoreCase("doc"))) {
HWPFDocument document = null;
try {
document = new HWPFDocument(new FileInputStream(srcPath));
Range range = document.getRange();
for (Entry<String, String> entry : map.entrySet()) {
range.replaceText(entry.getKey(), entry.getValue());
}
FileOutputStream outStream = null;
outStream = new FileOutputStream(destPath);
document.write(outStream);
outStream.close();
return true;
} catch (FileNotFoundException e) {
e.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
}
} else {
return false;
}
} else {
return false;
}
} }

二:合并多个docx

public class WordMergeUtil {
public static void main (String[] args) throws Exception { File newFile = new File("D:\\wdpj\\t.docx");
List<File> srcfile = new ArrayList<>();
// File file1 = new File("D:\\wdpj\\函.docx");
// File file2 = new File("D:\\wdpj\\1.docx");
File file1 = new File("D:\\wdpj\\2.docx");
File file2 = new File("D:\\wdpj\\3.docx");
File file3 = new File("D:\\wdpj\\函.docx"); // srcfile.add(file2);
// srcfile.add(file1);
srcfile.add(file1);
srcfile.add(file2);
srcfile.add(file3); 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);
}
for (int i = 0; i < documentList.size(); i++) {
doc = documentList.get(0); if(i != 0){
documentList.get(i).createParagraph().setPageBreak(true);
appendBody(doc,documentList.get(i));
}
}
// doc.createParagraph().setPageBreak(true);
doc.write(dest);
} catch (Exception e) {
e.printStackTrace();
}
} public static void appendBody(XWPFDocument src, XWPFDocument append) throws Exception {
XWPFParagraph p = src.createParagraph();
//设置分页符
p.setPageBreak(true);
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,map); } 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+mainPart+addPart+sufix);
src.set(makeBody); }

word文档操作-doc转docx、合并多个docx的更多相关文章

  1. Word文档操作知识

    Word文档操作知识 #持续更新 本次更新时间:2019-03-06 14:34 一.换行时字体空间过大 问题情景:当我们编写中文的文档时,中间插入了西方的字体或符号,在以它为行尾换行时: 会出现字体 ...

  2. word文档操作

    1.如何把word文档修改的地方标记出来   :  https://zhidao.baidu.com/question/73648149.html 2.word 的几种 视图:https://zhid ...

  3. poi导出word文档,doc和docx

    maven <!-- https://mvnrepository.com/artifact/org.apache.poi/poi --><dependency> <gro ...

  4. JAVA Asponse.Word Office 操作神器,借助 word 模板生成 word 文档,并转化为 pdf,png 等多种格式的文件

    一,由于该 jar 包不是免费的, maven 仓库一般不会有,需要我们去官网下载并安装到本地 maven 仓库 1,用地址   https://www-evget-com/product/564  ...

  5. 利用POI操作不同版本号word文档中的图片以及创建word文档

    我们都知道要想利用java对office操作最经常使用的技术就应该是POI了,在这里本人就不多说到底POI是什么和怎么用了. 先说本人遇到的问题,不同于利用POI去向word文档以及excel文档去写 ...

  6. JAVA Freemarker + Word 模板 生成 Word 文档 (普通的变量替换,数据的循环,表格数据的循环,以及图片的东替换)

    1,最近有个需求,动态生成 Word 文当并供前端下载,网上找了一下,发现基本都是用 word 生成 xml 然后用模板替换变量的方式 1.1,这种方式虽然可行,但是生成的 xml 是在是太乱了,整理 ...

  7. word 文档导出 (freemaker+jacob)--java开发

    工作中终于遇到了 需要导出word文旦的需求了.由于以前没有操作过,所以就先百度下了,基本上是:博客园,简书,CDSN,这几大机构的相关帖子比较多,然后花了2周时间 才初步弄懂.  学习顺序: 第一阶 ...

  8. C# 设置Word文档保护(加密、解密、权限设置)

    对于一些重要的word文档,出于防止资料被他人查看,或者防止文档被修改的目的,我们在选择文档保护时可以选择文档打开添加密码或者设置文档操作权限等,在下面的文章中将介绍如何使用类库Free Spire. ...

  9. C#/VB.NET 给Word文档添加/撤销书签

    在现代办公环境中,阅读或者编辑较长篇幅的Word文档时,想要在文档中某一处或者几处留下标记,方便日后查找.修改时,需要在相对应的文档位置插入书签.那对于开发者而言,在C#或者VB.NET语言环境中,如 ...

随机推荐

  1. python实现fibonacci数列的三种方法

    第一种:递归法 def fibo(n): if n < 3: return 1 return fibo(n-1) + fibo(n-2) print(fibo(6)) 第二种:循环 def fi ...

  2. 【bzoj5339】[TJOI2018]教科书般的亵渎(拉格朗日插值/第二类斯特林数)

    传送门 题意: 一开始有很多怪兽,每个怪兽的血量在\(1\)到\(n\)之间且各不相同,\(n\leq 10^{13}\). 然后有\(m\)种没有出现的血量,\(m\leq 50\). 现在有个人可 ...

  3. Python 分析到底是谁操纵《庆余年》上了热搜?

    庆余年电视剧终于在前两天上了,这两天赶紧爬取数据看一下它的表现. ​ ​ 庆余年 ​ <庆余年>是作家猫腻的小说.这部从2007年就开更的作品拥有固定的书迷群体,也在文学IP价值榜上有名. ...

  4. ETCD:运行时重新配置

    原文地址:runtime reconfiguration etcd带有增量运行时重新配置的支持.允许我们在集群运行的时候更新集群成员关系. 仅当大多数集群成员都在运行时,才能处理重新配置请求,强烈建议 ...

  5. Linux下Mysql安装教程详解

    Linux下软件安装一般有三种方式:RPM包方式(通过Redhat 第三方包管理系统).二进制包和源码包.本篇主要介绍二进制包安装mysql数据库的方式. 如何获取二进制源码包 当然是到mysql官网 ...

  6. python django-admin.py startproject xxx 错误:from django.core import management

    1. Python安装路径以及Python安装路径\Script文件夹,已经添加到PATH环境变量中. 2. 查看django 版本正常: import django print(django.__v ...

  7. Web前端基础(15):jQuery基础(二)

    1. jQuery选择器 jQuery选择器是jQuery强大的体现,它提供了一组方法,让我们更加方便的获取到页面中的元素. 1.1 基本选择器 例子如下: <!DOCTYPE html> ...

  8. C++ 深入浅出工厂模式(进阶篇)

    介绍 前文初始篇C++ 深入浅出工厂模式(初始篇),主要阐述了简单工厂模式.工厂方法模式和抽象工厂模式的结构.特点和缺陷等.以上三种方式,在新增产品时,要么修改工厂类,要么需新增具体的工厂类,说明工厂 ...

  9. 关于vue-detools chorme创建安装完成,但是控制台不显示问题

    搜了一下发现挺多人遇到这个问题的,绝大多数的回答都是在main.js中添加下面代码 Vue.config.devtools = true; 但是发现并不行. 后来看到有人说刷新然后在按F12就好了,居 ...

  10. [转]Introduction - Run Excel Macro using VBScript

    本文转自:https://wellsr.com/vba/2015/excel/run-macro-without-opening-excel-using-vbscript/ Have you ever ...