怎么使用poi-tl操作word模板
发表于:2025-12-02 作者:千家信息网编辑
千家信息网最后更新 2025年12月02日,怎么使用poi-tl操作word模板,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。使用poi-tl操作word模板1.导入jar包支
千家信息网最后更新 2025年12月02日怎么使用poi-tl操作word模板
怎么使用poi-tl操作word模板,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。
使用poi-tl操作word模板
1.导入jar包支持
com.deepoove poi-tl 1.8.2
2.建立word模板
在电脑E盘中建立word模板:"E:\templete.docx"
修改模板内容
标题:{{title}}工单编号:{{workNo}}发单日期:{{issueDate}}列表数据:{{*list}}3.生成代码方法
/** * 根据word模板生成word文档 * @param request * @param response */@RequestMapping(value = "expdoc")public void expdoc(HttpServletRequest request, HttpServletResponse response){ try { String tempFile="E:\\templete.docx"; XWPFTemplate template = XWPFTemplate.compile(tempFile).render( new HashMap() {{ put("title", "Hi, poi-tl Word模板引擎"); put("workNo", "20200910-001"); put("issueDate", "2020年9月10日"); put("list", new NumbericRenderData(new ArrayList() { { //循环list进行赋值 for(String detail:list){ add(new TextRenderData(detail)); } } })); }}); response.setContentType("application/octet-stream"); response.setHeader("Content-disposition","attachment;filename=\""+"out_template.docx"+"\""); OutputStream out = response.getOutputStream(); BufferedOutputStream bos = new BufferedOutputStream(out); template.write(bos); template.close(); bos.flush(); bos.close(); out.flush(); out.close(); } catch (Exception e) { e.printStackTrace(); }} 4.请求访问
在浏览器中输入请求地址:http://localhost:8080/expdoc ,浏览器自动下载word文档,文档名称为:out_template.docx
5.数据封装
5.1关联实体
public class YdgdTemplete{ private String demand;//要求 private List detailList;//列表数据} 5.2访问层
/** * 根据word模板生成数据信息,返回文档访问地址 * @param request * @param response * @return 文档访问地址 * @throws Exception */@RequestMapping("expdoc")@ResponseBodypublic BasePageData expdoc(HttpServletRequest request, HttpServletResponse response) throws Exception { BasePageData data = new BasePageData(); try { //获取根目录 String realPath = request.getSession().getServletContext().getRealPath("/"); //定义保存文档目录 String fileDir = "/fileData/doc/"; File saveFile = new File(realPath + fileDir); if (!saveFile.exists()) {// 如果目录不存在 saveFile.mkdirs();// 创建文件夹 } //生成word文档名称 String fileName = System.currentTimeMillis() + ".docx"; //保存的文件的路径信息 String docPath = realPath + fileDir + fileName; //返回文档路径 String backPath= fileDir + fileName; //封装数据 YdgdTemplete ydgdTemplete = new YdgdTemplete(); ydgdTemplete.setDemand("生成测试数据"); List list = new ArrayList<>(); list.add("第1条记录"); list.add("第2条记录"); ydgdTemplete.setDetailList(list); //根据模板生成word文档 Boolean falg = FileUtil.createdoc(docPath,ydgdTemplete); //调用返回 if(falg){ data.setData(backPath); data.setCode(WebResponseCode.APPSUCCESS); data.setMsg("操作成功!"); }else{ data.setData(null); data.setCode(WebResponseCode.APPFAIL); data.setMsg("操作失败!"); } return data; } catch (Exception e) { e.printStackTrace(); data.setCode(WebResponseCode.APPFAIL); data.setMsg("操作异常!"); return data; }} 5.3生成方法
/** * 根据word模板生成word文件 * @param docPath * @param ydgdTemplete * @return */public static boolean createdoc(String docPath, YdgdTemplete ydgdTemplete){ try { if(StringUtil.isBlank(docPath)){ return false; } //读取模板 File file = ResourceUtils.getFile("classpath:templates/ydgd_templete.docx"); XWPFTemplate template = XWPFTemplate.compile(file).render( new HashMap(){{ //生成数据 put("demand", ydgdTemplete.getDemand()); //生成列表数据 put("detailList", new NumbericRenderData(new ArrayList() { { if(null!=ydgdTemplete.getDetailList() && ydgdTemplete.getDetailList().size()>0){ for(String detail:ydgdTemplete.getDetailList()){ add(new TextRenderData(detail)); } } } })); }}); FileOutputStream out = new FileOutputStream(docPath); template.write(out); out.flush(); out.close(); template.close(); return true; } catch (Exception e) { e.printStackTrace(); return false; }} 关于怎么使用poi-tl操作word模板问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注行业资讯频道了解更多相关知识。
模板
生成
文档
数据
地址
文件
方法
问题
信息
内容
名称
更多
浏览器
目录
路径
封装
帮助
浏览
解答
易行
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
sql数据库远程过程调用失败
华为服务器部门整体出售价格
网络安全教育家长应该怎么做
网络安全主题漫画四年级
工业网络技术哪个学校好
海南省网络安全五指山论坛
金融科技互联网项目
为什要制定网络安全法
2021年网络安全公司50强
元气骑士哪个服务器没有广告
杨浦区网络技术咨询范围
网络技术激活码能用吗
冰雪插件软件开发
应用层的网络安全需求
成都软件开发园区
网络安全班队会主持稿
万达金服互联网科技有限公司
网络安全上机心得
网络技术哪种最好
中国人民公安大学网络安全
网络技术以后好就业吗
全国著名网络安全干警
java程序连接数据库作用
广西南宁网络安全培训
win2022服务器系统
如何查询数据库表中几个数据
沈阳软件开发制作
自己的电脑如何变成游戏服务器
易语言 服务器验证
广东企业云空间软件服务器