合并word

合并两个word 为一个(只处理好了图片,格式问题还是没解决,有解决的伙伴可以分享一下)
代码

package com.baic.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
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;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 合并word
 * 
 * @author wyh
 *
 */
public class WordMergeUtils {

	private static Logger logger = LoggerFactory.getLogger(WordMergeUtils.class);

	/**
	 * 连个word合并成一个word
	 * 
	 * @param filePath1
	 *            第一个word
	 * @param filePath2
	 *            第二个word
	 * @param filePath3
	 *            合并后的word
	 */
	public static void words2One(String filePath1, String filePath2, String filePath3) {
		try {
			
			// 第一个文输入件流
			InputStream in1 = new FileInputStream(filePath1);
			// 第二个文件输入流
			InputStream in2 = new FileInputStream(filePath2);
			// 获取第一个文件的读写权限
			OPCPackage src1Package = OPCPackage.open(in1);
			// 获取第二个文件的读写权限
			OPCPackage src2Package = OPCPackage.open(in2);
			// 第一个文件对象
			XWPFDocument src1Document = new XWPFDocument(src1Package);
			// 第二文件对象
			XWPFDocument src2Document = new XWPFDocument(src2Package);
			// 往第一个文件追加内容
			appendBody(src1Document, src2Document);
			// 删除第一个文件
			new File(filePath1).delete();
			// 删除第二个文件
			new File(filePath2).delete();
			// 输入流
			OutputStream out = new FileOutputStream(filePath3);
			// 输出文件
			src1Document.write(out);
			// 关闭流
			out.close();
		} catch (FileNotFoundException e) {
			logger.error("合并word", e);
		} catch (IOException e) {
			logger.error("合并word", e);
		} catch (Exception e) {
			logger.error("合并word", e);
		}
	}

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值