freemarker word

时间:2022-04-26 18:52:09

        Map<String, Object> map = new HashMap<String, Object>();
        Projset projset = projsetService.findById(id);
        String sql = "select * from urm_user where id="   "‘"   projset.getManager()   "‘";
        List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
        map.put("manager", list.get(0).get("FULLNAME"));
        map.put("projsetname", projset.getPname());
        map.put("cnum", projset.getCnum());
        createWord(map, "entryprogressword.ftl", "D:/wordFile/", "项目经理任命书.doc");
/**
     * 生成word文件
     * @param dataMap word中需要展示的动态数据,用map集合来保存
     * @param templateName word模板名称,例如:test.ftl
     * @param filePath 文件生成的目标路径,例如:D:/wordFile/
     * @param fileName 生成的文件名称,例如:test.doc
     * @throws IOException 
     */
    @SuppressWarnings("unchecked")
    public void createWord(Map dataMap,String templateName,String filePath,String fileName){
        try {
            //创建配置实例 
            Configuration configuration = new Configuration();
            //设置编码
            configuration.setDefaultEncoding("UTF-8");
            //ftl模板文件统一放至 com.lun.template 包下面
            configuration.setClassForTemplateLoading(this.getClass(),"../template");
            //获取模板 
            Template template = configuration.getTemplate(templateName);
            //输出文件
            File outFile = new File(filePath File.separator fileName);
            //如果输出目标文件夹不存在,则创建
            if (!outFile.getParentFile().exists()){
                outFile.getParentFile().mkdirs();
            }
            //将模板和数据模型合并生成文件 
            Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile),"UTF-8"));
            //生成文件
            template.process(dataMap, out);
            //关闭流
            out.flush();
            out.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }