java poi-tl 根据模板生成word、合并多个word文档

一、导入依赖
<dependency>
   <groupId>com.deepoove</groupId>
   <artifactId>poi-tl</artifactId>
   <version>1.12.0</version>
</dependency>
二、根据模板生成word,合并多个word的代码
import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.xwpf.NiceXWPFDocument;

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

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class POITlWordTest {

    /**
     * 根据模板生成word
     */
    public static void gen() {

        /**
         * 通过map存放要填充的数据
         */
        Map<String, Object> data = new HashMap<>();

        data.put("title", "Java 全栈知识体系");
        data.put("importants", "hao-kjsdjkdsjk 重点在今后的实际的就是Java重点在今后的实际的就是Java重点在今后的实际的就是Java");
        data.put("normal", "重点在今后的实际的就是Java 全栈知识体系,重点在今后的实际的就是Java 全栈知识体系重点在今后的实际的就是Java 全栈知识体系,重点在今后的实际的就是Java 全栈知识体系重点在今后的实际的就是Java 全栈知识体系重点在今后的实际的就是Java 全栈知识体系重点在今后的实际的就是Java 全栈知识体系重点在今后的实际的就是Java 全栈知识体系重点在今后的实际的就是Java 全栈知识体系重点在今后的实际的就是Java 全栈知识体系重点在今后的实际的就是Java 全栈知识体系重点在今后的实际的就是Java 全栈知识体系重点在今后的实际的就是Java 全栈知识体系重点在今后的实际的就是Java 全栈知识体系");

        XWPFTemplate template = XWPFTemplate.compile("C:\\Users\\Administrator\\Desktop\\tpl.docx").render(data);
        try {
            FileOutputStream out = new FileOutputStream("C:\\Users\\Administrator\\Desktop\\test.docx");
            template.write(out);
            out.flush();
            out.close();
            template.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 合并word
     */
    public static void concat() {

        try {

            NiceXWPFDocument main = new NiceXWPFDocument(new FileInputStream("C:\\Users\\Administrator\\Desktop\\tpl.docx"));

            NiceXWPFDocument sub = new NiceXWPFDocument(new FileInputStream("C:\\Users\\Administrator\\Desktop\\test.docx"));

            //添加段落
            XWPFParagraph p1 = main.createParagraph();
            p1.setPageBreak(true);


            // 合并成三个文档
            NiceXWPFDocument newDoc1 = main.merge(sub);

            // 又添加一个新的段落
            XWPFParagraph p2 = newDoc1.createParagraph();
            p2.setPageBreak(true);

            NiceXWPFDocument newDoc2 = newDoc1.merge(sub);

            // 生成新文档
            FileOutputStream out = new FileOutputStream("C:\\Users\\Administrator\\Desktop\\new_test.docx");
            newDoc2.write(out);
            newDoc2.close();
            out.close();
            System.out.println("合并word成功!");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
//        gen();

        concat();
    }
}

三、效果图
  • 1、模板的tpl.docx
    在这里插入图片描述

  • 2、生成的test.docx
    在这里插入图片描述

  • 3、合并后的new_doc.docx

在这里插入图片描述

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Spring Boot中使用poi-tl库来导出带有合并列的Word表格并下载,您可以按照以下步骤操作: 1. 首先,确保您的Spring Boot项目中已经添加了poi-tl的依赖。您可以在pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>com.deepoove</groupId> <artifactId>poi-tl</artifactId> <version>1.6.0</version> </dependency> ``` 2. 创建一个Controller来处理导出请求。例如,创建一个名为WordExportController的类,并添加一个处理导出请求的方法。 ```java import com.deepoove.poi.XWPFTemplate; import com.deepoove.poi.data.*; import com.deepoove.poi.util.BytePictureUtils; import org.apache.poi.xwpf.usermodel.XWPFTable; import org.apache.poi.xwpf.usermodel.XWPFTableRow; import org.springframework.core.io.InputStreamResource; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.List; @Controller public class WordExportController { @GetMapping("/export") public ResponseEntity<InputStreamResource> exportWord() throws IOException { // 创建一个数据模型 List<List<String>> tableData = new ArrayList<>(); tableData.add(createRow("Merged Cells", "Cell 3")); tableData.add(createRow("Cell 4", "Cell 6")); // 使用poi-tl的XWPFTemplate来生成Word文档 XWPFTemplate template = XWPFTemplate.compile("templates/template.docx").render( new DataTable(tableData) .setHeader(createRow("Header 1", "Header 2")) .setCellWidth(2000) // 设置单元格宽度 .setHeaderCellStyle(new CellStyle().setBold(true).setColor("FFFFFF").setBgColor("336699")) .setOddRowCellStyle(new CellStyle().setColor("FFFFFF").setBgColor("99CCFF")) .setEvenRowCellStyle(new CellStyle().setColor("FFFFFF").setBgColor("CCEEFF")) ); // 将生成Word文档转换为字节数组 ByteArrayOutputStream out = new ByteArrayOutputStream(); template.write(out); byte[] documentBytes = out.toByteArray(); // 设置下载响应的头信息 HttpHeaders headers = new HttpHeaders(); headers.setContentDispositionFormData("attachment", "merged_table.docx"); headers.setContentType(MediaType.APPLICATION_OCTET_STREAM); // 创建一个包含Word文档字节数组的InputStreamResource InputStreamResource resource = new InputStreamResource(new ByteArrayInputStream(documentBytes)); // 返回响应实体 return ResponseEntity.ok() .headers(headers) .body(resource); } private List<String> createRow(String cell1, String cell2) { List<String> row = new ArrayList<>(); row.add(cell1); row.add(cell2); return row; } } ``` 3. 在resources目录下创建一个名为template.docx的Word模板文件。在模板文件中,您可以根据自己的需求设置表格样式和内容。 4. 启动您的Spring Boot应用程序,并访问导出请求的URL(例如:http://localhost:8080/export)。将会自动下载名为merged_table.docx的Word文档,其中包含合并列的表格。 请确保按照您的需求修改代码,并根据模板文件的位置进行相应的调整。 希望对您有所帮助!如果您有任何其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值