怎么使用poi-tl操作word模板
发表于:2025-02-07 作者:千家信息网编辑
千家信息网最后更新 2025年02月07日,怎么使用poi-tl操作word模板,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。使用poi-tl操作word模板1.导入jar包支
千家信息网最后更新 2025年02月07日怎么使用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 ListdetailList;//列表数据}
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("生成测试数据"); Listlist = 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安全错误
数据库的锁怎样保障安全
天津gpu服务器租赁报价
aws数据库技术
数据库营销的实施过程
日本拉曼光谱数据库
数据库备份群晖
全球2021服务器排名
xctf高校网络安全挑战赛
数据库按指定值排序
腾畅网络技术有限公司
如何获取任意web的服务器时间
雀魂不同服务器能加好友吗
医学科研数据库都有哪些
博兴染整管理软件开发
自我保护网络安全演讲稿
西安金品位网络技术有限公司
南京工程软件开发平均价格
网络安全专业的大学生
云边协同数据库
数据库损坏游戏无法运行
学生记账系统数据库
天津gpu服务器租赁报价
山东发布最新网络安全报告
中拓互联网络科技工作的人
深圳市擎天软件开发有限公司
服务器挂停
糖类分子数据库
石家庄软件开发工作怎么样
时序数据库的技术难点
如何查看微数据库文件大小
七巧国网络技术