springmvc下解析模板生成Excel ,用freemarker

工具类解析模板生成Excel

 

public class TemplateParseUtil {
	/** 
     * 解析模板生成Excel 
     * @param templateDir  模板目录 
     * @param templateName 模板名称 
     * @param excelPath 生成的Excel文件路径 
     * @param data 数据参数 
     * @throws IOException 
     * @throws TemplateException 
     */  
    public static void parse(String templateDir,String templateName,String excelPath,Map data) throws IOException, TemplateException {  
        //初始化工作  
        Configuration cfg = new Configuration();  
        //设置默认编码格式为UTF-8  
        cfg.setDefaultEncoding("UTF-8");   
        //全局数字格式  
        cfg.setNumberFormat("0.00");  
        //设置模板文件位置  
        cfg.setDirectoryForTemplateLoading(new File(templateDir));  
        cfg.setObjectWrapper(new DefaultObjectWrapper());  
        //加载模板  
        Template template = cfg.getTemplate(templateName,"utf-8");  
        OutputStreamWriter writer = null;  
        try{  
            //填充数据至Excel  
            writer = new OutputStreamWriter(new FileOutputStream(excelPath),"UTF-8");  
            template.process(data, writer);  
            writer.flush();  
        }finally{  
            writer.close();  
        }     
    }  
  
  
    /** 
     * 解析模板返回字节数组 
     * @param templateDir  模板目录 
     * @param templateName 模板名称 
     * @param data 数据参数 
     * @throws IOException 
     * @throws TemplateException 
     */  
    public static byte[] parse(String templateDir,String templateName,Map data) throws TemplateException, IOException{  
        Configuration cfg = new Configuration();  
        cfg.setDefaultEncoding("UTF-8");  
        cfg.setNumberFormat("0.00");  
        cfg.setDirectoryForTemplateLoading(new File(templateDir));  
        cfg.setObjectWrapper(new DefaultObjectWrapper());  
        Template template = cfg.getTemplate(templateName,"utf-8");  
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();  
        Writer out = new OutputStreamWriter(outStream,"UTF-8");  
        template.process(data, out);  
        return outStream.toByteArray();  
    }  
      
    /** 
     * 自定义模板字符串解析 
     * @param templateStr  模板字符串 
     * @param data 数据   
     * @return 解析后的字符串 
     * @throws IOException 
     * @throws TemplateException 
     */  
    public static String parse(String templateStr, Map data)  
            throws IOException, TemplateException {  
        Configuration cfg = new Configuration();  
        cfg.setNumberFormat("#.##");  
        //设置装载模板  
        StringTemplateLoader stringLoader = new StringTemplateLoader();   
        stringLoader.putTemplate("myTemplate", templateStr);      
        cfg.setTemplateLoader(stringLoader);  
        //加载装载的模板  
        Template temp = cfg.getTemplate("myTemplate", "utf-8");  
        Writer out = new StringWriter();  
        temp.process(data, out);  
        return out.toString();  
    }  
}

模板文件excel.ft




	
		mocking
		
		2018-05-17T06:23:39Z
		2018-05-17T06:31:50Z
	
	
		2052-10.1.0.7346
		
	
	
		27525
		13650
		False
		False
	
	
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
	
	
		ss:ExpandedRowCount="${rsvCountList?size + 2}"
			x:FullColumns="1" x:FullRows="1" ss:DefaultColumnWidth="54"
			ss:DefaultRowHeight="14.25">
			
				
					所在市
				
				
					大(1)型
				
				
					大(2)型
				
				
					中型
				
				
					小(1)型
				
				 
					小(2)型</Data>
				
				
					合计
				
			<#list rsvCountList as rsv>  
   			 
   			 ${rsv.name!}
			    <#list rsv.countList as c> 
				    ${c.count!}  
			 	
			 	${rsv.count!}