小工具知识网

当前位置: 首页 > 行业知识 > 2024 java easyexcel poi word模板填充数据,多个word合成一个word

行业知识

2024 java easyexcel poi word模板填充数据,多个word合成一个word

2026-12-04 20:01:34 来源:互联网转载

先看效果

一、准备工作

1.word模版

2.文件路径

二、pom依赖

                       com.alibaba            easyexcel            2.1.7                            javax.servlet            servlet-api            2.5                                    com.deepoove            poi-tl            1.12.0                                    org.apache.poi            poi            5.2.2                                    org.apache.poi            poi-ooxml            5.2.2                            org.apache.poi            poi-ooxml-schemas            4.1.2        

三、两个工具类+自己的实体类(这里是用户作为示例)

1.自己的实体类

import com.mybatisflex.annotation.Id;import com.mybatisflex.annotation.KeyType;import com.mybatisflex.annotation.Table;import java.io.Serializable;import lombok.AllArgsConstructor;import lombok.Builder;import lombok.Data;import lombok.NoArgsConstructor;/** * 用户信息 实体类 * * @author zhaoyan * @since 2024-04-19 */@Data@Builder@NoArgsConstructor@AllArgsConstructor@Table(value = "user")public class User implements Serializable {    /**     * 用户id     */    @Id(keyType = KeyType.Auto)    private Integer id;    /**     * 姓名     */    private String name;    /**     * 身份证号     */    private String cardId;    /**     * 性别     */    private String sex;}

2.合并word工具类

import java.io.*;import java.util.*;import org.apache.poi.openxml4j.opc.OPCPackage;import org.apache.poi.xwpf.usermodel.*;import org.apache.xmlbeans.XmlOptions;import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBody;/** * 合并word工具类 */public class MergeWordUtil {    /**     * 将多个Word文档文件合并成一个新的Word文档文件,     * 并将合并后的内容保存到指定的目标路径中。     *     * @param files      多个word文件     * @param targetPath 目标存放地址     * @throws Exception     */    public static void mergeMroeWord(List files, String targetPath) throws Exception {        // 1. 打开目标路径对应的文件输出流 `dest`,并使用 try-with-resources 语句确保流在使用完毕后会被正确关闭。        try (OutputStream dest = new FileOutputStream(targetPath);) {            // 2. 创建一个 `ArrayList` 类型的 `documentList`,用于存储读取的多个Word文档对象。            ArrayList documentList = new ArrayList();            XWPFDocument doc = null;            // 3. 遍历传入的文件列表 `files`,对每个文件执行以下操作:            for (File file : files) {                try (FileInputStream in = new FileInputStream(file.getAbsoluteFile())) {                    // - 使用 `FileInputStream` 读取文件内容,并通过 `OPCPackage` 打开文件流,创建一个新的XWPFDocument对象 `document`。                    OPCPackage open = OPCPackage.open(in);                    XWPFDocument document = new XWPFDocument(open);                    // - 将读取的文档对象 `document` 添加到 `documentList` 中。                    documentList.add(document);                } catch (FileNotFoundException e) {                    // - 如果读取文件时发生 `FileNotFoundException` 异常,则捕获并打印异常信息。                    e.printStackTrace();                }            }            // 4. 遍历 `documentList` 中的文档对象:            for (int i = 0; i ") + 1);        String mainPart = srcString.substring(srcString.indexOf(">") + 1, srcString.lastIndexOf("

上一篇:轻松下载Tumblr视频到苹果设备,揭秘实用技巧!

下一篇:通过crt怎么连接其他服务器?