【实习】之FreeMarker

【实习】之FreeMarker

一、业务逻辑

  • 通过原始的doc文档转换为xml格式,再改为ftl格式
  • 在ftl中将需要注入的数据用${}写入
  • 通过map注入这些数据

二、具体实现

  • image-20210806144309746通过转换将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方法

    • 首先将模板注入

    image-20210806145234776

    • 创建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();
		}
	}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值