解决word合并打不开的问题
最近在做需求的时候,发现word合并之后打不开,经过调研之后,发现是word里面的document.xml里面某些标签有问题,需要将特殊的标签去掉。
代码如下:
1.导入pom.xml
<dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>4.1.2</version></dependency>
2.测试类:
package com.atguigu.common.utils;import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.openxml4j.util.ZipSecureFile;
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;import java.io.File;
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;/*** @Author mischen* @Description 文件合并 只支持.docx* @Date 2022/11/21 16:30* @Version 1.0*/
public class DocxMerge {public static void main(String[] args) {File file1 = new File("F:\\test1\\3.docx");List<File> targetFile1 = new ArrayList<>();targetFile1.add(new File("F:\\test1\\a.docx"));targetFile1.add(new File("F:\\test1\\01-系统架构设计师教程(第4版)_划了重点.docx"));appendDocx(file1, targetFile1);System.out.println("合并成功!!!");}/*** 把多个docx文件合并成一个** @param outfile 输出文件* @param targetFile 目标文件*/public static void appendDocx(File outfile, List<File> targetFile) {try {OutputStream dest = new FileOutputStream(outfile);ArrayList<XWPFDocument> documentList = new ArrayList<>();XWPFDocument doc = null;for (int i = 0; i < targetFile.size(); i++) {ZipSecureFile.setMinInflateRatio(-1.0d);FileInputStream in = new FileInputStream(targetFile.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) {//解决word合并完后,所有表格都紧紧挨在一起,没有分页。加上了分页符可解决documentList.get(i).createParagraph().setPageBreak(true);appendBody(doc, documentList.get(i));}}doc.write(dest);} 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();// 记录图片合并前及合并后的IDMap<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);}//这个代码主要解决合并word报错,解析抛出压缩炸弹ZipSecureFile.setMinInflateRatio(-1.0d);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("<"));//下面这部分可以去掉,我加上的原因是合并的时候,有时候出现打不开的情况,对照document.xml将某些标签去掉就可以正常打开了addPart = addPart.replaceAll("w14:paraId=\"[A-Za-z0-9]{1,10}\"", "");addPart = addPart.replaceAll("w14:textId=\"[A-Za-z0-9]{1,10}\"", "");addPart = addPart.replaceAll("w:rsidP=\"[A-Za-z0-9]{1,10}\"", "");addPart = addPart.replaceAll("w:rsidRPr=\"[A-Za-z0-9]{1,10}\"", "");addPart = addPart.replace("<w:headerReference r:id=\"rId8\" w:type=\"default\"/>","");addPart = addPart.replace("<w:footerReference r:id=\"rId9\" w:type=\"default\"/>","");addPart = addPart.replace("xsi:nil=\"true\"","");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合并打不开的问题相关推荐
- 表格提示html内容消失,如何解决Word里面的表格插入题注后页面上内容消失、无法编辑的问题...
office 2010的Word文档,在表格上插入题注后,页面被消失,然后就无法编辑了,页面视图见不到文件,只是显示背景,文字,图表均不见了,点击撤销键后,回复原有文件,但是也不能进行编辑了.这种情况 ...
- 计算机再带word打不开,word打不开,详细教您怎么解决word打不开
在我们平时工作的时候,都会遇到一个问题,那就是打不开word文档.但是我们的工作时间有很紧迫,那么这个时候,我们该怎么办呢?不要慌,今天小编就来告诉你们当遇到word打不开的时候应该怎么办. 对于那些 ...
- 如何解决word文档报错打不开呢?
在日常办公中,会经常使用到word文档.有时,在我们的电脑上会不小心将两个不同的文件保存在同一个群集中.这样导致文件是交叉链接的,致使我们经常会收到一个错误提示.此外,由于病毒或数据损坏而误删除文件很 ...
- 如何解决word文档损坏打不开呢?
当下班后关闭电脑或重新启动电脑,而致使Word文件仍在使用中并意外关闭时,发生这种情况.再次使用电脑上打开一些文档,可能会发现word文档损坏打不开,收到错误信息:"文件已损坏,无法打开&q ...
- word 2010打不开03格式文档解决办法
word 2010打不开03格式文档解决办法,右键属性,点击提示框下部"解除锁定",确认后,再双击文件就能打开了.
- npoi插入word图片(终于解决插入图片后打不开word)
npoi向word插入图片总结 npoi插入word,一把辛酸一把泪呀,开始总是不能插入图片,一插入就word就打不开了,网上百度,好多人都有这个问题,后来看到一篇poi博客poi博客介绍到这个是一个 ...
- word锁定:解决Word文档由于文档保护而无法修改、查找、编辑的方法
方法一: 今天有一朋友从网上下载了一个Word文档,可是打开该Word文档发现无法修改.查找.编辑,更可恶的是都不能选中Word文档中的内容.其实这是由于该Word文档被文档保护了的原因 ...
- python 合并word文档,实现同一个文件夹下面的word合并成一个word
python 合并word文档,实现同一个文件夹下面的word合并成一个word -- coding:utf-8 -- import win32com.client as win32 import o ...
- Word合并邮件,怎么选择outlook里的发件人
背景:以前的财务离职了,新财务接手,发工资条需要用到word的邮件合并功能,因为发送工资条是用以前的财务的邮箱发送的,现在以前的财务离职了,新财务接收,发送工资条自然要用新财务的邮箱去发送.由于以前的 ...
最新文章
- zoj 1438 - Asteroids!
- 2021-11-04Oracle19c 图形化安装,在windoes上)
- java 微信转账 ca_error_C#关于微信红包开发问题:CA证书出错,请登录微信支付商户平台下载证书...
- linux module_init
- 服务器红帽操作系统安装,pxe无人值守安装 红帽操作系统
- lock是悲观锁还是乐观锁_图文并茂的带你彻底理解悲观锁与乐观锁
- 简单理解LSTM神经网络
- windows远程linux桌面
- 中国农业大学计算机专业在陕西录取分数线,中国农业大学2018年在陕西省高考一本投档录取分数线...
- JavaEE | 增强for循环
- java学校,班级,学生类的三级关系
- 基于阿里云CentOS8搭建远程服务器
- AB计算机总线,什么是总线?简要说明AB、DB、CB的含义及其性能?
- 盲用计算机图片,这个实验室让盲人可以“看到”图像
- mac 微信多开 应用程序多开
- 怎样在微信中点击app下载链接直接打开下载安卓/苹果(apk/ipa,pxl,deb)文件
- STM32输出PWM波形并实现呼吸灯
- Python-OpenCV棋盘检测
- 一顿饭吃一两斤肉和一顿饭吃几百斤肉
- Linux平台常用终端指令合集【超级全】