WORD模板替换,将文件给前端下载

1.word模板设计填充字段加{{填充字段名}}

2.后端依赖

<poi-tl.version>1.7.3</poi-tl.version>
<poi.version>4.1.2</poi.version>
<dependency>
    <groupId>com.deepoove</groupId>
    <artifactId>poi-tl</artifactId>
    <version>${poi-tl.version}</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>${poi.version}</version>
</dependency>

3.后端代码

3.1工具类

package com.ruoyi.common.utils.poi;

import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.config.Configure;
import com.deepoove.poi.policy.HackLoopTableRenderPolicy;
import com.ruoyi.common.utils.uuid.UUID;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.math.BigDecimal;
import java.nio.file.Files;
import java.nio.file.Paths;

public class WordUtil {

    public static void test(Object data, String filePath, HttpServletResponse response) {
        HackLoopTableRenderPolicy policy = new HackLoopTableRenderPolicy();
        Configure config = Configure.newBuilder().bind("list", policy).build();
        XWPFTemplate template = XWPFTemplate.compile(filePath, config).render(data);//调用模板,填充数据
        /*src\main\resources\wordTemplate\test.docx*/
        String uuid = UUID.randomUUID().toString();
        try {

            FileOutputStream out = new FileOutputStream("C:\\Users\\ZDHS\\Desktop\\"+uuid+".docx");//要导出的文件名
            template.write(out);
            out.flush();
            out.close();
            template.close();
            /*==*/
            String fileName = "Operator.docx"; // 文件的默认保存名
            // 读到流中
            InputStream inStream = new FileInputStream("C:\\Users\\ZDHS\\Desktop\\"+uuid+".docx");// 文件的存放路径
            // 设置输出的格式
            response.reset();
            response.addHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
            // 循环取出流中的数据
            byte[] b = new byte[100];
            int len;
            try {
                while ((len = inStream.read(b)) > 0)
                    response.getOutputStream().write(b, 0, len);
                inStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            /*==*/

        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            Files.delete(Paths.get("C:\\Users\\ZDHS\\Desktop\\" + uuid + ".docx"));
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

3.2接口类

//@PreAuthorize("@ss.hasPermi('system:selection:export')")
//@Log(title = "技术参数", businessType = BusinessType.EXPORT)
@PostMapping("/wordTemplate")
public void wordTemplate(HttpServletResponse response, HttpServletRequest request, HistoryVo history) {
    /*todo:处理数据*/
    WordUtil.test(history,"todo:文件路径",response);
}

4.前端事件方法

handleDownload(row) {

      this.queryParams = row

      console.log(row)

      this.download('system/selection/wordTemplate', {

        ...this.queryParams

      }, `selection_${new Date().getTime()}.docx`)

    },

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

20岁30年经验的码农

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值