这里的模板是用docx文档转成的xml文件 Map map = new HashMap();
map.put("spotDate","数据")); String folderPath = "D:\\zhuiExperiment\\template"; // 模板路径 String folderPath22 = "D:\\zhuiExperiment\\downloadPath"; // 文件夹路径 String path= "模板名字"; //先生成为doc文件 存放文件模板的位置\文件名称\存放文件的路径\数据 this.parse(folderPath, path, folderPath22+"\\"+ "要生成文件的名字"+ ".docx", map);
//先生成为doc文件 存放文件模板的位置\文件名称\存放文件的路径\数据
//存放文件模板的位置\文件名称\存放文件的路径\数据 public void parse(String templateDir, String templateName, String excelPath, Map<String, Object> data) throws IOException, freemarker.template.TemplateException { //初始化工作 Configuration cfg = new Configuration(); //设置默认编码格式为UTF-8 cfg.setDefaultEncoding("gbk"); //全局数字格式 cfg.setNumberFormat("0"); //设置模板文件位置 cfg.setDirectoryForTemplateLoading(new File(templateDir)); cfg.setObjectWrapper(new DefaultObjectWrapper()); //加载模板 freemarker.template.Template template = cfg.getTemplate(templateName, "utf-8"); //OutputStreamWriter writer = null; Writer writer = null; try { //填充数据至Excel //writer = new OutputStreamWriter(new FileOutputStream(excelPath),"UTF-8"); writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(excelPath), "UTF-8")); template.process(data, writer); writer.flush(); } catch (freemarker.template.TemplateException e) { e.printStackTrace(); } finally { if (writer != null) { writer.close(); } } }