java实现word⽂档合并(⽀持⽂档中有图⽚和表格)1:java中使⽤poi实现合并word⽂档,兼容图⽚的合并并分页
package ponent.util;
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 lbeans.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 fallrain
* @Description ⽂件合并只⽀持.docx
* @Date 2021/7/30 16:30
* @Version 1.0
*/
public class DocxMerge {
public static void main(String[] args) {
File file1 = new File("E:\\merge\\mearg3.docx");
List<File> targetFile1 = new ArrayList<>();
targetFile1.add(new File("E:\\merge\\新昌县⼈民医院迁建项⽬策划书20201029报送稿.docx"));
targetFile1.add(new File("E:\\merge\\设计原型评审会议待明确点.docx"));
appendDocx(file1, targetFile1);
}
/**
* 把多个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++) {
FileInputStream in = new (i).getPath());
OPCPackage open = OPCPackage.open(in);
XWPFDocument document = new XWPFDocument(open);
documentList.add(document);
}replaceall()
for (int i = 0; i < documentList.size(); i++) {
doc = (0);
if (i != 0) {
//解决word合并完后,所有表格都紧紧挨在⼀起,没有分页。加上了分页符可解决
<(i).createParagraph().setPageBreak(true);
appendBody(doc, (i));
}
}
doc.write(dest);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void appendBody(XWPFDocument src, XWPFDocument append) throws Exception {
CTBody src1Body = Document().getBody();
CTBody src2Body = Document().getBody();
List<XWPFPictureData> allPictures = AllPictures();
// 记录图⽚合并前及合并后的ID
Map<String, String> map = new HashMap<>();
for (XWPFPictureData picture : allPictures) {
String before = RelationId(picture);
//将原⽂档中的图⽚加⼊到⽬标⽂档中
String after = src.Data(), 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 = lText(optionsOuter);
String srcString = lText();
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("<"));
//下⾯这部分可以去掉,我加上的原因是合并的时候,有时候出现打不开的情况,对照l将某些标签去掉就可以正常打开了        addPart = placeAll("w14:paraId=\"[A-Za-z0-9]{1,10}\"", "");
addPart = placeAll("w14:textId=\"[A-Za-z0-9]{1,10}\"", "");
addPart = placeAll("w:rsidP=\"[A-Za-z0-9]{1,10}\"", "");
addPart = placeAll("w:rsidRPr=\"[A-Za-z0-9]{1,10}\"", "");
addPart = place("<w:headerReference r:id=\"rId8\" w:type=\"default\"/>","");
addPart = place("<w:footerReference r:id=\"rId9\" w:type=\"default\"/>","");
addPart = place("xsi:nil=\"true\"","");
if (map != null && !map.isEmpty()) {
//对xml字符串中图⽚ID进⾏替换
for (Map.Entry<String, String> set : Set()) {
addPart = Key(), Value());
}
}
//将两个⽂档的xml内容进⾏拼接
CTBody makeBody = CTBody.Factory.parse(prefix + mainPart + addPart + sufix);
src.set(makeBody);
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。