通过freemarker生成word

来源:互联网 发布:中兴f412 端口23失败 编辑:程序博客网 时间:2024/06/05 04:45

通过freemarker将数据导出到word的模板中。

//新的接口,改为web页面填写生成word @RequestMapping(value="/insertProcess.do")     public @ResponseBody Object insertProcess(HttpServletRequest req ) { String activityId = (String) req.getSession().getAttribute("activityId"); String processInfo = req.getParameter("insert"); String targetTable = req.getParameter("targetTable"); String basePath=servletContext.getRealPath("/").substring(0, servletContext.getRealPath("/").lastIndexOf("cn-admin"))+"/upload/cn-admin/process/"; String type = req.getParameter("type"); try { req.setCharacterEncoding("UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); }  Freemarker freemark = new Freemarker("../template/");//freemark.setTemplateName("wordTemplate.ftl");freemark.setTemplateName("test.ftl");freemark.setFileName(new SimpleDateFormat("yyyy-MM-ddhh-mm-ss").format(new Date())+".doc");freemark.setFilePath(basePath);String fileName = freemark.createWord(processInfo,targetTable);String realUrl = "/upload/cn-admin/process/"+fileName;return processService.insert(processInfo,activityId,type,realUrl);
下面是freemarker类

package com.admin.utils;import java.io.BufferedWriter;import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.OutputStreamWriter;import java.io.UnsupportedEncodingException;import java.io.Writer;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import net.sf.json.JSONArray;import net.sf.json.JSONObject;import freemarker.template.Configuration;import freemarker.template.Template;import freemarker.template.TemplateException;public class Freemarker {/** * freemark初始化 * @param templatePath 模板文件位置 */public Freemarker(String templatePath) {configuration = new Configuration();configuration.setDefaultEncoding("utf-8");configuration.setClassForTemplateLoading(this.getClass(),templatePath);}/** * freemark模板配置 */private Configuration configuration;/** * freemark模板的名字 */private String templateName;/** * 生成文件名 */private String fileName;/** * 生成文件路径 */private String filePath;public String getFileName() {return fileName;}public void setFileName(String fileName) {this.fileName = fileName;}public String getFilePath() {return filePath;}public void setFilePath(String filePath) {this.filePath = filePath;}public String getTemplateName() {return templateName;}public void setTemplateName(String templateName) {this.templateName = templateName;}public String createWord(String processInfo, String targetTable) {JSONObject json = JSONObject.fromObject(processInfo);targetTable = targetTable.substring(1);String[] temp  = targetTable.split("#");//JSONArray target = JSONArray.fromObject(temp);Template t = null;try {t = configuration.getTemplate(templateName);} catch (IOException e) {e.printStackTrace();}File outFile = new File(filePath+fileName);Writer out = null;try {out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "UTF-8"));} catch (UnsupportedEncodingException e) {e.printStackTrace();} catch (FileNotFoundException e) {e.printStackTrace();}Map map = new HashMap<String, Object>();map.put("forum", json.getString("name"));//map.put("image", getImageStr());map.put("ForumTitle", json.getString("belong"));map.put("ParentOrganization", json.getString("parentorganization"));map.put("UndertakingOrganization", json.getString("undertakingorganization"));map.put("beginTime",json.getString("beginTime"));map.put("endTime",json.getString("endTime"));map.put("ForumLocation",json.getString("forumlocation"));map.put("ForumSize", json.getString("forumsize"));/*List<ProcessTable> ProcessTable = new ArrayList<ProcessTable>();String[] temp1 = null;for (int j = 0; j < temp.length; j++) { temp1 = temp[j].split(","); List<String> tmp = new ArrayList<String>();          for(String str:temp1){              if(str!=null && str.length()!=0){                  tmp.add(str);              }          }  ProcessTable pt = new ProcessTable();pt.setBeginDate(tmp.toArray(new String[0])[0]);pt.setEndDate(tmp.toArray(new String[0])[1]);pt.setSpeakTitle(tmp.toArray(new String[0])[2]);pt.setSpeakGuest(tmp.toArray(new String[0])[3]);pt.setGuestDes(tmp.toArray(new String[0])[4]);ProcessTable.add(pt);*///}//map.put("table2", ProcessTable);try {t.process(map, out);out.close();} catch (TemplateException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}return fileName;}}
模板的话先用word建好,如下图:
然后转成  ftl  格式,转好后要检查下标签名是否被错开了,错开的话要进行修补。

原创粉丝点击