springboot整合POI导出word(文字加图片)

1、引入Poi依赖

<dependency>
        <groupId>com.deepoove</groupId>
        <artifactId>poi-tl</artifactId>
        <version>1.8.2</version>
</dependency>

2、设置word模板

在这里插入图片描述
注意"{{}}"为中文模式下符号,图片占位{{@pic}},文字为{{text}}。

3、(工具类)Utils

import com.deepoove.poi.XWPFTemplate;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.net.URLEncoder;
import java.util.Map;

public class ExportWordUtils {
    public static void exportWord(String templatePath, String temDir, String fileName, Map<String, Object> params, HttpServletRequest request, HttpServletResponse response) {
        if (!temDir.endsWith("/")) {
            temDir = temDir + File.separator;
        }
        File dir = new File(temDir);
        if (!dir.exists()) {
            dir.mkdirs();
        }
        try {
            XWPFTemplate doc = XWPFTemplate.compile(templatePath).render(params);
            String tmpPath = temDir + fileName;//新文件路径
            FileOutputStream fos = new FileOutputStream(tmpPath);
            doc.write(fos);
            fos.flush();
            fos.close();
            doc.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

import com.deepoove.poi.data.PictureRenderData;
import com.qh.makeword.utils.ExportWordUtils;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Controller
public class WordController {
    @RequestMapping("/export")
    public void export(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Map<String,Object> params = new HashMap<>();
        //key值与模板中的占位符一致
        params.put("id","2020212345");
        params.put("name","李四");
        params.put("Image", new PictureRenderData(100, 140, new ClassPathResource("static/1.jpg").getFile().getPath()));
        ClassPathResource oldDoc = new ClassPathResource("word/export.docx");
        //一行代码
        ExportWordUtils.exportWord(oldDoc.getFile().getPath(),"newWord/","new.docx",params,request,response);
    }
}

5、文件放置

在这里插入图片描述

export.docx为模板,1.jpg为插入模板的图片

6、运行结果

在这里插入图片描述
参考:
https://blog.csdn.net/weixin_43694038/article/details/106123534?spm=1001.2014.3001.5506

https://blog.csdn.net/qq_41938882/article/details/89445678?utm_source=app&app_version=5.3.0&code=app_1562916241&uLinkId=usr1mkqgl919blen

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值