博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
多个word合并代码
阅读量:4261 次
发布时间:2019-05-26

本文共 3581 字,大约阅读时间需要 11 分钟。

多个word合并代码

 

 

package com.pxxedu.question.exportword;

 

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;

 

import org.apache.poi.openxml4j.opc.OPCPackage;

import org.apache.poi.xwpf.usermodel.BreakType;

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;

/**

* 合成word文档工具类

* @ClassName: CompoundWordUtil

*/

public class CompoundWordUtil {

public static void main (String[] args) throws Exception {

File newFile = new File("E:\\workspace\\idea\\pxxedu-question-repo\\src\\test\\java\\com\\pxxedu\\question\\ftl\\dest.docx");

List<File> srcfile = new ArrayList<>();

 

File file1 = new File("E:\\workspace\\idea\\pxxedu-question-repo\\src\\test\\java\\com\\pxxedu\\question\\ftl\\head.docx");

File file2 = new File("E:\\workspace\\idea\\pxxedu-question-repo\\src\\test\\java\\com\\pxxedu\\question\\ftl\\test.docx");

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);

}

for (int i = 0; i < documentList.size(); i++) {

doc = documentList.get(0);

if(i == 0){//首页直接分页,不再插入首页文档内容

// documentList.get(i).createParagraph().createRun().addBreak(BreakType.PAGE);

// appendBody(doc,documentList.get(i));

}else if(i == documentList.size()-1){//尾页不再分页,直接插入最后文档内容

appendBody(doc,documentList.get(i));

}else{

documentList.get(i).createParagraph().createRun().addBreak(BreakType.PAGE);

appendBody(doc,documentList.get(i));

}

}

doc.write(dest);

System.out.println("*****合成成功********");

} 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<String,String>();

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);

}

}

转载地址:http://eexei.baihongyu.com/

你可能感兴趣的文章
简易的ATM机
查看>>
旧版本的ATM
查看>>
关于super()
查看>>
关于JAVA中GUI的使用
查看>>
接口的简单使用
查看>>
关于接口的几点
查看>>
自己封装的一个简单组件:文字标签 文本框
查看>>
集合的一些概念总结
查看>>
几个常见的关于日期的SQL
查看>>
常见约束、事务及其他查询语句
查看>>
关于jdbc
查看>>
利用jdbc做的一个简单系统(接上一篇)
查看>>
对TextField 和JTextField 等文本编辑区的监听
查看>>
详解个推java服务端集成(干货)
查看>>
常见聚合函数
查看>>
简单子查询
查看>>
联表查询
查看>>
关于WindowListener的使用
查看>>
关于KeyListener的简单使用
查看>>
关于鼠标移动监听接口:MouseMotionListener
查看>>