SpringBoot集成easyPoi实现导出word文档

1.导入maven包

<dependency>
    <groupId>org.jeecg</groupId>
    <artifactId>easypoi-base</artifactId>
    <version>2.3.1</version>
</dependency>
<dependency>
    <groupId>org.jeecg</groupId>
    <artifactId>easypoi-web</artifactId>
    <version>2.3.1</version>
</dependency>
<dependency>
    <groupId>org.jeecg</groupId>
    <artifactId>easypoi-annotation</artifactId>
    <version>2.3.1</version>
</dependency>

2。创建word文档样式

创建word文件夹将word放入到文件夹中

 3.创建导出工具类

package cn.net.hylink.qingzhi.utils;


import org.apache.poi.POIXMLDocument;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.jeecgframework.poi.word.WordExportUtil;
import org.springframework.util.Assert;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

public class ExportWordUtils {


    public static void exportWord(String templatePath, String temDir, String fileName, Map<String, Object> params,HttpServletResponse response,HttpServletRequest request) {
      /*  Assert.notNull(templatePath,"模板路径不能为空");
        Assert.notNull(temDir,"临时文件路径不能为空");
        Assert.notNull(fileName,"导出文件名不能为空");
        Assert.isTrue(fileName.endsWith(".docx"),"word导出请使用docx格式");*/
        if (!temDir.endsWith("/")){
            temDir = temDir + File.separator;
        }
        File dir = new File(temDir);
        if (!dir.exists()) {
            dir.mkdirs();
        }
        try {
            String userAgent = request.getHeader("user-agent").toLowerCase();
            if (userAgent.contains("msie") || userAgent.contains("like gecko")) {
                fileName = URLEncoder.encode(fileName, "UTF-8");
            } else {
                fileName = new String(fileName.getBytes("utf-8"), "ISO-8859-1");
            }
            XWPFDocument doc = WordExportUtil.exportWord07(templatePath, params);
            String tmpPath = temDir + fileName;
            FileOutputStream fos = new FileOutputStream(tmpPath);
            doc.write(fos);
            // 设置强制下载不打开
            response.setContentType("application/force-download");
            // 设置文件名
            response.addHeader("Content-Disposition", "attachment;fileName=" + fileName);
            OutputStream out = response.getOutputStream();
            doc.write(out);
            out.close();


//            XWPFDocument doc = WordExportUtil.exportWord07(templatePath, params);
//            String tmpPath = temDir + fileName;
//            FileOutputStream fos = new FileOutputStream(tmpPath);
//            doc.write(fos);
//            fos.flush();
//            // 设置强制下载不打开
//
//            // 设置文件名
//
//            //doc.write(out);
//            //out.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //delFileWord(temDir,fileName);//这一步看具体需求,要不要删
        }
    }



    /**
     * 删除零时生成的文件
     */
    public static void delFileWord(String filePath, String fileName){
        File file =new File(filePath+fileName);
        File file1 =new File(filePath);
        file.delete();
        file1.delete();
    }

}

4.controller创建方法

@RequestMapping(value = "/ceshi", method = RequestMethod.GET)
public void ceshi() throws ParseException {


    Map<String, Object> params = new HashMap<>();

    params.put("rwdj","三级");
    params.put("createtime", "2021-01-15  08:03");
    ExportWordUtils.exportWord("word/export.docx", "D:/test", "ceshi.docx", params, response, request);
}

map里的key对应word里的变量值 就可以直接将值赋值到word里.

表格里可以填写list的值 如下:

在map里添加伟list的值就可以循环绑定上.

在赋值上样式问题的代码: 

 根据自己的需要变换word里的变量值

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用 EasyPoi 导出 Word 文档需要进行以下步骤: 1. 引入 EasyPoi 依赖 在 pom.xml 文件中添加以下依赖: ```xml <dependency> <groupId>cn.afterturn</groupId> <artifactId>easypoi-base</artifactId> <version>5.2.0</version> </dependency> ``` 2. 创建模板 在 Word 文档中创建一个需要填充数据的模板,可以使用 Word 编辑器创建一个包含标签的文档。标签可以在文档中使用 ${} 表示。 例如,创建一个包含用户信息的模板,可以在 Word 文档中添加以下内容: ``` 姓名:${name} 年龄:${age} ``` 3. 创建数据源 创建一个 Java 类,包含需要导出的数据,例如: ```java public class User { private String name; private int age; // 省略 getter 和 setter 方法 } ``` 4. 使用 EasyPoi 导出 Word 文档 在 Java 代码中使用 EasyPoi 提供的 API 将数据填充到模板中,并将生成的 Word 文档保存到本地磁盘。例如: ```java // 创建模板对象 XWPFTemplate template = XWPFTemplate.compile("template.docx").render(data); // 将数据填充到模板中 Map<String, Object> data = new HashMap<>(); data.put("name", "张三"); data.put("age", 18); // 导出 Word 文档 try (FileOutputStream out = new FileOutputStream("output.docx")) { template.write(out); } template.close(); ``` 上述代码中,XWPFTemplate.compile("template.docx") 表示创建一个模板对象,template.render(data) 表示将数据填充到模板中,FileOutputStream("output.docx") 表示将生成的 Word 文档保存到本地磁盘。 以上就是使用 EasyPoi 导出 Word 文档的步骤。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值