【实习】之FreeMarker
一、业务逻辑
- 通过原始的doc文档转换为xml格式,再改为ftl格式
- 在ftl中将需要注入的数据用${}写入
- 通过map注入这些数据
二、具体实现
-
通过转换将word转换为ftl文件
-
控制层
@RequestMapping(value = { "xxx" }, method = { RequestMethod.POST, RequestMethod.GET }) @ResponseBody public Object generHorReport(String repid, String repStatus) throws Exception { try { if (repStatus.equals(GlobalQc.REPORT_STATUS_FILE)) { return new ResponseResult<>(ResponseCodeCanstants.FAILED, "已经生成文档,不需要再次生成"); } if (repStatus.equals(GlobalQc.REPORT_STATUS_AUDIT_PASS)) { if (repSingleReportService.generaterHorDocMent(repid)) { return new ResponseResult<>(ResponseCodeCanstants.SUCCESS, "生成成功"); } } } catch (Exception e) { e.printStackTrace(); logger.info(e.getMessage()); } return new ResponseResult<>(ResponseCodeCanstants.FAILED, "未成功生成报告"); }
-
Service层
// 将输入存入map中 Map<String, Object> map = Maps.newHashMap(); // 主表信息 RepSingleIndex RI = repSingleIndexDao.get(repid); map.put("repIndex", RI); // 质量目标 List<RepSingleTarget> targetsArr = repSingleTargetDao.findList(new RepSingleTarget(null, repid)); map.put("targets", targetsArr); // 检查结果信息 RepSingleResult RR = repSingleResultDao.get(repid); map.put("isResult", RR==null?null:RR.getIsResult()); ****** //repHorSingleQualityTable选择的模板 String file = WordGenerator.createDoc(map, "repHorSingleQualityTable", repid); if (StringUtils.isBlank(file)) { return false; } else { RepSingleIndex ri=new RepSingleIndex(); ri.setFileOk(GlobalQc.USEABLE_ON); ri.setRepStatus(GlobalQc.REPORT_STATUS_FILE); ri.setFileSrc(file); ri.setId(repid); repSingleIndexDao.update(ri); } return true;
-
生成Word方法
- 首先将模板注入
-
创建word
public static String createDoc(Map<?, ?> dataMap, String type,String reportId) { String savePath = Global.getConfig("userfiles.basedir")+ Global.USERFILES_BASE_URL+"qc/report/"; File f = new File(savePath); if(!f.exists()){ f.mkdirs(); } Template t = allTemplates.get(type); try { // 这个地方不能使用FileWriter因为需要指定编码类型否则生成的Word文档会因为有无法识别的编码而无法打开 Writer w = new OutputStreamWriter(new FileOutputStream(savePath+reportId+".doc"), "utf-8"); t.process(dataMap, w); w.close(); } catch (Exception ex) { ex.printStackTrace(); throw new RuntimeException(ex); } if(!isOSLinux()){ logger.debug("file path : "+savePath+reportId+".doc"); if(DocConvetor.xmlToDoc(savePath+reportId+".doc", savePath+reportId+"-1.doc")){ File f1 = new File(savePath+reportId+".doc"); } DocConvetor.word2pdf( savePath+reportId+"-1.doc", savePath+reportId+"-1.pdf"); return Global.USERFILES_BASE_URL+"qc/report/"+reportId+"-1.doc,"+Global.USERFILES_BASE_URL+"qc/report/"+reportId+"-1.pdf"; }else{ return Global.USERFILES_BASE_URL+"qc/report/"+reportId+".doc"; } }
-
将doc转为docx和pdf文件
public static boolean xmlToDoc(String sourcePath, String targetPath){
logger.debug("sourcePath:"+sourcePath);
logger.debug("targetPath:"+targetPath);
ComThread.InitSTA();
//Word.Application代表COM OLE编程标识,可查询MSDN得到
ActiveXComponent app = new ActiveXComponent("Word.Application");
try{
//设置Word不可见
app.setProperty("Visible",new Variant(false));
//调用Application对象的Documents属性,获得Documents对象
Dispatch docs = app.getProperty("Documents").toDispatch();
Dispatch doc = Dispatch.call(docs,"Open",sourcePath).getDispatch();
Dispatch.call(doc,"SaveAS",targetPath,12);
//关闭打开的Word文件
Dispatch.call(doc,"Close",false);
return true;
}catch(Exception e){
e.printStackTrace();
return false;
}finally{
//关闭Word应用程序
app.invoke("Quit",new Variant(0));
ComThread.InitSTA();
ComThread.Release();
ComThread.quitMainSTA();
}
}