Easypoi 合并 word 1生2 2合1

该博客介绍了如何使用Apache POI库和XWPFTemplate在Java中批量读取模板文件,通过HashMap动态替换内容,并插入分页,最终将多个个性化Word文档合并到一个输出文件中。核心技术包括模板编译、数据映射和XML操作。
摘要由CSDN通过智能技术生成
package com.gsy.tool.utils;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;

import com.deepoove.poi.XWPFTemplate;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.xmlbeans.XmlOptions;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBody;

public class WordS {
    /*public static void main(String[] args) throws Exception {
        InputStream in1 = null;
        InputStream in2 = null;
        OPCPackage src1Package = null;
        OPCPackage src2Package = null;

        OutputStream dest = new FileOutputStream("D:\\aaaaa\\w2w.docx");

        in1 = new FileInputStream("D:\\aaaaa\\wwww.docx");
        in2 = new FileInputStream("D:\\aaaaa\\wwww2.docx");
        //src1Package = OPCPackage.open(in1);
        src2Package = OPCPackage.open(in2);

        XWPFDocument src1Document;// = new XWPFDocument(src1Package);

        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("birthday", "SDFASDFADSF");

        XWPFTemplate render = XWPFTemplate.compile(in1).render(hashMap);
        src1Document = render.getXWPFDocument();

        CTBody src1Body = src1Document.getDocument().getBody();
        XWPFParagraph p = src1Document.createParagraph();
        //设置分页符
        p.setPageBreak(true);
        XWPFDocument src2Document = new XWPFDocument(src2Package);
        CTBody src2Body = src2Document.getDocument().getBody();
        appendBody(src1Body, src2Body);
        src1Document.write(dest);

    }*/

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

        OutputStream dest = new FileOutputStream("D:\\aaaaa\\w2w.docx");

        ArrayList<XWPFDocument> xwpfDocuments = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            HashMap<String, Object> hashMap = new HashMap<>();
            hashMap.put("birthday", "SDFASDFADSF");
            InputStream in1 = new FileInputStream("D:\\aaaaa\\wwww.docx");
            XWPFTemplate render = XWPFTemplate.compile(in1).render(hashMap);
            XWPFDocument src1Document = render.getXWPFDocument();
            xwpfDocuments.add(src1Document);
        }
        XWPFDocument appendbodys = appendbodys(xwpfDocuments);
        appendbodys.write(dest);

    }*/

    public static void main(String[] args) throws Exception {
        ArrayList<HashMap<String, Object>> arrayList = new ArrayList<>();
        OutputStream dest = new FileOutputStream("D:\\aaaaa\\w2w.docx");
        for (int i = 0; i < 10; i++) {
            HashMap<String, Object> hashMap = new HashMap<>();
            hashMap.put("birthday", "SDFASDFADSF   " + i);
            arrayList.add(hashMap);
        }
        getWords(arrayList, dest);
    }

    public static void getWords(ArrayList<HashMap<String, Object>> arrayList, OutputStream dest) throws Exception {

        ArrayList<XWPFDocument> xwpfDocuments = new ArrayList<>();
        for (HashMap<String, Object> hashMap : arrayList) {
            InputStream in1 = new FileInputStream("D:\\aaaaa\\wwww.docx");
            XWPFTemplate render = XWPFTemplate.compile(in1).render(hashMap);
            XWPFDocument src1Document = render.getXWPFDocument();
            xwpfDocuments.add(src1Document);
        }
        XWPFDocument appendbodys = appendXWPFDocuments(xwpfDocuments);
        appendbodys.write(dest);
    }

    public static XWPFDocument appendXWPFDocuments(ArrayList<XWPFDocument> arrayList) throws Exception {
        XWPFDocument xwpfDocument = arrayList.get(0);
        if (arrayList.size() == 0) {
            return xwpfDocument;
        }
        for (int i = 1; i < arrayList.size(); i++) {
            XWPFDocument xwpfDocument1 = arrayList.get(i);
            XWPFParagraph p = xwpfDocument.createParagraph();
            //设置分页符
            p.setPageBreak(true);
            xwpfDocument = appendXWPFDocument(xwpfDocument, xwpfDocument1);
        }
        return xwpfDocument;
    }

    private static XWPFDocument appendXWPFDocument(XWPFDocument src, XWPFDocument append) throws Exception {
        CTBody body = src.getDocument().getBody();
        CTBody body1 = append.getDocument().getBody();
        appendBody(body, body1);
        return src;
    }

    private static void appendBody(CTBody src, CTBody append) 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("<"));
        CTBody makeBody = CTBody.Factory.parse(prefix + mainPart + addPart + sufix);
        src.set(makeBody);
    }

}

效果
在这里插入图片描述
未 分页 后
在这里插入图片描述
分页
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值