当前位置: 首页 > news >正文

java写入word表格(poi-tl)

1.导入依赖

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

2.代码

自己创建模板。放在(resource/file)

public ApiResult<String> download2(HttpServletResponse response,@RequestParam(value = "apiCode", required = true) String apiCode) throws Exception {String templatePath = getResourceFilePath("file","wordtempalte.docx");//resource下面有file文件夹,文件夹下面是模板ApiPermitInfo data = getPermitInfo(apiCode);writeword(templatePath, response,data);//写入文件return null;}public static String getResourceFilePath(String folderPath, String fileName) {// 构建资源路径String resourcePath = folderPath + "/" + fileName;// 获取资源的URLURL resourceUrl = ResourceUtil.class.getClassLoader().getResource(resourcePath);if (resourceUrl != null) {// 对于JAR包中的资源,直接返回URL的路径部分return resourceUrl.getPath();} else {// 资源不存在return null;}}private void writeword(String templatePath, HttpServletResponse response,ApiPermitInfo data) throws IOException {//解析模板XWPFTemplate template = XWPFTemplate.compile(templatePath);//封装模型数据HashMap<String, Object> map = new HashMap<>();map.put("apiLabel","apiLabel");map.put("apiPath", Texts.of("apiPath").color("0000FF").bold().create());//apiParams  getApiParams方法获取一个List类型map.put("apiParams", Tables.create(getApiParams(data.getApiParams()).toArray(new RowRenderData[0])));//渲染数据template.render(map);//以文件形式输出//      template.writeAndClose(new FileOutputStream(outputPath));//文件流//下载response.setContentType("application/octet-stream");String formattedNow = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd-HH-mm-ss"));response.setHeader("Content-disposition", "attachment;filename=\"" + formattedNow+".docx" + "\"");OutputStream out = response.getOutputStream();BufferedOutputStream bos = new BufferedOutputStream(out);template.write(bos);//浏览器下载PoitlIOUtils.closeQuietlyMulti(template, bos, out);return;}

3.效果

参考:

poi-tl的使用(通俗易懂,全面,内含动态表格实现 !)-CSDN博客

https://deepoove.com/poi-tl/


http://www.mrgr.cn/news/14674.html

相关文章:

  • 使用 Spring Boot 快速构建 Java Web 应用
  • SpringBoot 项目——抽奖系统
  • 计算机网络模型
  • Leetcode 15.三数之和
  • 【C++】类与对象
  • 爬虫可以通过那几种方式更换IP爬取
  • MyBatis之XML配置文件(一)
  • centos安装mysql8.0版本,并且实现远程连接
  • RSA非对称性加密02: 加密redis的连接密码(下)-私钥加密,公钥解密
  • 【文档合集】软件类常用文档整理大全,软件工程,软件项目管理,技术标书方案,模
  • 代码随想录跟练第九天——LeetCode 232.用栈实现队列、225. 用队列实现栈、20. 有效的括号、1047. 删除字符串中的所有相邻重复项
  • RAG中pdf解析的方法全览
  • 重构贪心算法(二)
  • 【ElasticSearch】基本命令
  • 学懂C++(四十四):C++ 自定义内存管理的深入解析:内存池与自定义分配器
  • wordpress跨境电商外贸独立站 常见获取流量方式
  • 在手机在线预览3D模型,是如何实现的?
  • Qt 调用MFC dll,动态库中有界面
  • 【Python】简单的数据类型——int、float、bool、str
  • 【Leetcode 2215 】 找出两数组的不同 —— 同时操作两个哈希表