Java用freemarker导出word文档
2015-10-02 21:57
225 查看
前段时间某个项目有个需求,需要从数据库读取数据并导出word文档,于是通过查阅资料,使用freemarker成功实现了该功能。
1.使用office word软件先写好需要导出的文档的大体框架,并保存为xml文件;
2.把该模版对应需要显示数据的地方用${}填充,并把后缀改为.ftl格式放到项目中;
3核心代码:
1.使用office word软件先写好需要导出的文档的大体框架,并保存为xml文件;
2.把该模版对应需要显示数据的地方用${}填充,并把后缀改为.ftl格式放到项目中;
3核心代码:
@SuppressWarnings("resource") private void createLeaveDoc(Leave leave, String <span style="color:#FF0000;">tempDocPath</span>, <span style="color:#FF0000;">//红色字体表示输出路径</span> String tempDocName) throws Exception { <span style="color:#FF0000;">String templatePath = "D:\\synchronized folder\\version2\\WebRoot\\lea_sys\\template\\";//模版所在路径</span> Configuration cfg = new Configuration(); cfg.setDefaultEncoding("utf-8"); cfg.setDirectoryForTemplateLoading(new File(templatePath)); cfg.setObjectWrapper(new DefaultObjectWrapper()); <span style="color:#FF0000;">Template temp = cfg.getTemplate("template.ftl");//加载模版</span> temp.setEncoding("utf-8"); HashMap contentValue = new HashMap<>(); Integer id = leave.getId(); Integer empId = leave.getEmpId(); String empName = leave.getEmpName(); String empTitle = leave.getEmpTitle(); String office = leave.getOffice(); String department = leave.getDepartment(); String arrivalVerifier = leave.getArrivalVerifier(); Date arrivalDate = leave.getArrivalDate(); Date beginDate = leave.getBeginDate(); Date endDate = leave.getEndDate(); Date submitDate = leave.getSubmitDate(); String place = leave.getPlace(); String reason = leave.getReason(); String type = leave.getType(); Integer requestedLv = leave.getRequestedLv(); String hasFee = leave.getHasFee(); String status = leave.getStatus(); Integer approveCount = leave.getApproveCount(); String approvers = leave.getApprovers(); String arrivalVerifyStatus = leave.getArrivalVerifyStatus(); String rejectReason = leave.getRejectReason(); String icon = leave.getIcon(); String approver1 = leave.getApprover1(); String approver2 = leave.getApprover2(); Date approveDate1 = leave.getApproveDate1(); Date approveDate2 = leave.getApproveDate2(); contentValue.put("id", id); contentValue.put("empId", empId); contentValue.put("empName", empName); contentValue.put("empTitle", empTitle); contentValue.put("office", office); contentValue.put("department", department); contentValue.put("arrivalVerifier", arrivalVerifier); contentValue.put("arrivalDate", arrivalDate); contentValue.put("beginDate", beginDate); contentValue.put("endDate", endDate); contentValue.put("submitDate", submitDate); contentValue.put("place", place); contentValue.put("reason", reason); contentValue.put("type", type); contentValue.put("requestedLv", requestedLv); contentValue.put("hasFee", hasFee); contentValue.put("status", status); contentValue.put("approveCount", approveCount); contentValue.put("approvers", approvers); contentValue.put("arrivalVerifyStatus", arrivalVerifyStatus); contentValue.put("rejectReason", rejectReason); contentValue.put("icon", icon); contentValue.put("approver1", approver1); contentValue.put("approver2", approver2); contentValue.put("approveDate1", approveDate1); contentValue.put("approveDate2", approveDate2); File file = new File(tempDocPath, tempDocName); if (!file.getParentFile().exists()) { file.getParentFile().mkdirs(); } FileOutputStream fos = new FileOutputStream(file); OutputStreamWriter osw = new OutputStreamWriter(fos, "utf-8"); BufferedWriter bw = new BufferedWriter(osw); fos.flush(); osw.flush(); bw.flush(); temp.process(contentValue, bw); fos.close(); osw.close(); bw.close(); logger.debug("输出完成"); }
相关文章推荐
- 【Java EE 学习 52】【Spring学习第四天】【Spring与JDBC】【JdbcTemplate创建的三种方式】【Spring事务管理】【事务中使用dbutils则回滚失败!!!??】
- Java注解阐述
- Mybatis系列(八)集合映射
- Java设计模式之封装算法的模板模式
- ubunt14.04.3的vnc中,ti ccsv5 java异常退出,导致ccsv5崩溃
- Java设计模式之适配器模式
- 深入Java虚拟机笔记---ClassLoader
- Java中的null到底是什么?
- Java学习笔记----封装和private关键字
- equal和==的区别(原理讲解)
- Eclipse 项目导入 Android Studio 导致的乱码问题
- 1 java程序设计概述
- springMVC配置spring AOP功能中遇到的问题
- spring配置文件详解--真的蛮详细
- Java核心技术第4章(1)
- 关于数组的个人理解
- java回收机制
- Generics and Wildcards in Java
- Java学习笔记----匿名对象
- ==和equal()的区别