freemarker根据word模板导出动态word(Excel模板导出Excel)
1.创建word模板
将需要替换的位置用"${}"代替,如下图:
2.将做好的模板文件保存为xml文件
3.打开xml文件,查看"${unitName}"是否在一起,不在一起的话删除多余部分
4.将修改的xml文件后缀改为ftl
5.工具类
package com.util;import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.util.Map;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;public class DocumentHandler {private Configuration configuration = null;public DocumentHandler() {configuration = new Configuration();configuration.setDefaultEncoding("utf-8");}public void createDoc(Map<String, Object> dataMap, String fileName, String tempName,String tempPath) throws UnsupportedEncodingException {//需要导出模板的包路径try {configuration.setDirectoryForTemplateLoading(new File(tempPath));} catch (IOException e2) {// TODO Auto-generated catch blocke2.printStackTrace();}//configuration.setClassForTemplateLoading(this.getClass(), "template");Object obj = this.getClass();Template t = null;try {t = configuration.getTemplate(tempName);} catch (IOException e) {e.printStackTrace();}File outFile = new File(fileName);Writer out = null;FileOutputStream fos = null;try {fos = new FileOutputStream(outFile);OutputStreamWriter oWriter = new OutputStreamWriter(fos, "UTF-8");out = new BufferedWriter(oWriter);} catch (FileNotFoundException e1) {e1.printStackTrace();}try {t.process(dataMap, out);out.close();fos.close();} catch (TemplateException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}System.out.println("文档导出完成");}
}
6.实例
/*** 导出word*/@RequiresPermissions("ai:park:houseContract:edit")@RequestMapping(value = "importWord")@ResponseBodypublic String importWord(HttpServletRequest request) {JSONArray jsonArray = new JSONArray();JSONObject o = new JSONObject();//获取跟路径String solutePath = request.getSession().getServletContext().getRealPath("/");String tempPath = solutePath+"/TemplateFile";Random myRandom = new Random();Date now = new Date();String strFileName = new SimpleDateFormat("yyyyMMddhhMMss").format(now) + myRandom.nextInt(10000) + "房屋租赁合同";String strSaveFileName = strFileName + ".doc";String strDirectoryName = "/TemplateFile/Files/" + new SimpleDateFormat("yyyyMM").format(now) + "/";String newFilePath = solutePath + strDirectoryName + strSaveFileName;DocumentHandler mdoc = new DocumentHandler();File file = new File(solutePath + strDirectoryName);if(!file.exists()){file.mkdir();}String id = request.getParameter("id");HouseContract houseContract = houseContractService.get(id);ImportBusiness importBusiness = importBusinessService.get(houseContract.getImportId());BusinessPark businessPark = businessParkService.get(importBusiness.getParkId());SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");Map<String,Object> map = new HashMap<String,Object>();map.put("parkName", businessPark.getAName());map.put("businessName", importBusiness.getBusinessName());map.put("parkAddress", businessPark.getParkAddress());map.put("floor", houseContract.getFloor());map.put("qu", houseContract.getQu());map.put("unit", houseContract.getUnit());map.put("leaseTerm", houseContract.getLeaseTerm());String leaseStartDate = formatter.format(houseContract.getLeaseStartDate());map.put("leaseStartDate", leaseStartDate);String leaseEndDate = formatter.format(houseContract.getLeaseEndDate());map.put("leaseEndDate", leaseEndDate);map.put("monthlyRent", houseContract.getMonthlyRent());map.put("parkPost", businessPark.getParkPost());try {mdoc.createDoc(map, newFilePath,"房屋租赁合同.ftl",tempPath);} catch (UnsupportedEncodingException e) {// TODO Auto-generated catch blocke.printStackTrace();}o.put("success", true);o.put("filepath", newFilePath);jsonArray.add(o);return jsonArray.toJSONString();}
导出文件下载参考Java获取文件路径后文件下载
freemarker根据word模板导出动态word(Excel模板导出Excel)相关推荐
- 使用poi-tl导出动态word模板,包含折线图、柱状图、饼状图
1 依赖引入 <dependency> <groupId>com.deepoove</groupId> <artifa ...
- 浅析Page.LoadTemplate(模板)方法动态获取绑定模板后,通过FindControl获取服务端控件的方法。...
平常使用DataList数据控件绑定数据时,都是在ItemTemplate项里面放入 <asp:DataList ID="list2" runat="server& ...
- PR模板|MOGRT动态开场视频模板剪辑素材
这一个很棒的时尚扁平化设计PR视频剪辑模板素材,适用于自媒体视频制作.内容宣传展示.品牌营销等.可以使用这个PR模板轻松制作视频,帮助推广您的品牌. PR2018 | 1920×1080 (HD) | ...
- 利用freemarker模板引擎进行word导出
FreeMarker是一个用Java语言编写的模板引擎,它基于模板来生成文本输出.FreeMarker与Web容器无关,即在Web运行时,它并不知道Servlet或HTTP.它不仅可以用作表现层的实现 ...
- SpringBoot导出word模板并动态渲染数据
导出word模板并动态渲染数据 一.需求介绍 背景:需要导出word模板的时候,有些数据是动态或者图片等不确定因素的时候.根据需求定制好的模板要求填充数据,那么这个时候就需要进行根据word模板进行动 ...
- Java项目中利用Freemarker模板引擎导出--生成Word文档
应邀写的一篇文章:Java项目中利用Freemarker模板引擎导出--生成Word文档 资源下载:https://download.csdn.net/download/weixin_41367523 ...
- freemarker根据静态模板和动态模板生成PDF与Word
背景介绍:最近在做老旧项目的二次开发,所以用到了freemarker去生成PDF和Word,涉及到打印静态模板与动态模板.查看了这方面的资料发现大都不全,似是而非.废话不多说,上案例. freemar ...
- 【Freemarker】java代码wps word带图片动态模板生成
一. 新建word模板 先通过wps或word等文字处理工具新建一个想要的结构的word文档,数据都先用假数据填充好,填充的数据实际使用上是没用的,是为了咱们修改模板能够迅速定位到需要动态填充区域的. ...
- Beetl 模板引擎生成word以及excel总结
Beetl Java模板引擎生成word excel 之前项目中使用freemarker和POI进行word以及excel的模板导出,在使用的过程中为了解决一些小问题,意外的接触了Beetl这款模板生 ...
最新文章
- 什么是信度分析(Reliability)?有哪些信度分析方法?那什么又是效度?
- ios应用内跳转到appstore里评分
- Access中字段类型及修改字段类型的SQL语句
- html字符串转svg,【SVG】如何操作SVG Text
- Error:(2, 0) Plugin with id 'com.github.dcendents.android-maven' not found
- android 动态仿磁贴,高仿 WIN8系统 磁贴点击下沉倾斜效果源码
- 视频教程-【吴刚】电商活动站设计初级入门标准视频教程-UI
- 如何用随机森林算法,在深海养肥一群鱼
- matlab mosa算法,尔雅尔雅2020年科学计算与MATLAB语言答案大全
- Qt的基本控件——显示控件
- 原理分析:安卓手机桌面长按分享,安卓发送软件apk,qq发送软件apk 原理
- 地理国情监测类毕业论文文献都有哪些?
- 给你推荐一个文件阅读器:Neat Reader
- 天宇,snapseed,第五课,建筑照片调整
- 快手直播弹幕数据采集
- 中断服务程序编写规则
- Ansoft HFSS 2021R1 求解类型详解
- android仿音乐播放器,Android仿音乐播放器功能
- 数据库系统SSD7 实验3《存储过程》
- 删掉炒股软件,重新做人