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

}

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

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在使用easyPoi导出word时,需要使用WordExportUtil工具类来word文档。要合并列相同的单元格,需要在成表格时设置合并单元格的信息。 例如,假设要导出以下表格: | 姓名 | 年龄 | 地址 | | :----: | :----: | :----: | | 张三 | 20 | 北京 | | 李四 | 20 | 上海 | | 王五 | 21 | 广州 | 如果要合并年龄相同的单元格,可以按以下步骤操作: 1. 创建一个List对象,用于存储表格数据。 ```java List<Map<String, Object>> dataList = new ArrayList<>(); ``` 2. 设置表头,按照以下格式设置。 ```java Map<String, Object> header = new LinkedHashMap<>(); header.put("姓名", "name"); header.put("年龄", "age"); header.put("地址", "address"); dataList.add(header); ``` 3. 设置表格数据,按照以下格式设置。 ```java Map<String, Object> data1 = new LinkedHashMap<>(); data1.put("name", "张三"); data1.put("age", 20); data1.put("address", "北京"); dataList.add(data1); Map<String, Object> data2 = new LinkedHashMap<>(); data2.put("name", "李四"); data2.put("age", 20); data2.put("address", "上海"); dataList.add(data2); Map<String, Object> data3 = new LinkedHashMap<>(); data3.put("name", "王五"); data3.put("age", 21); data3.put("address", "广州"); dataList.add(data3); ``` 4. 创建一个WordExportContext对象,并设置表格样式。 ```java WordExportContext context = new WordExportContext(); TableStyle tableStyle = new TableStyle(); tableStyle.setBackgroundColor("FFFFFF"); tableStyle.setAlign(STJc.CENTER); tableStyle.setVerticalAlign(STTblVerticalAlignment.CENTER); tableStyle.setBold(false); tableStyle.setFontSize(12); tableStyle.setFontFamily("微软雅黑"); tableStyle.setBorderSize(4); tableStyle.setBorderColor("000000"); context.setTableStyle(tableStyle); ``` 5. 创建一个Table对象,并设置表格样式。 ```java Table table = new Table(); table.setHeaders(dataList.subList(0, 1)); table.setRows(dataList.subList(1, dataList.size())); table.setStyle(tableStyle); ``` 6. 设置合并列信息。 ```java MergeRow[] mergeRows = new MergeRow[1]; mergeRows[0] = new MergeRow(1, 1, 1, 2); table.setMergeRows(mergeRows); ``` 7. 将Table对象添加到WordExportContext对象中。 ```java context.getTables().add(table); ``` 8. 使用WordExportUtil工具类word文档。 ```java byte[] byteArray = WordExportUtil.exportWord07("table.docx", context); ``` 以上就是使用easyPoi导出word合并列相同的单元格的步骤。其中,MergeRow对象的参数依次为:起始行、结束行、起始列、结束列。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值