velocity导出Word,excel模板的工具类

来源:互联网 发布:建筑虚拟仿真软件 编辑:程序博客网 时间:2024/04/27 21:32

1,先把要导出的模板转成xml格式,然后加到项目中,将后缀名改成.vm

2.以下是工具类,两种(一个是导出指定位置,一个是浏览器下载)

工具类代码如下:

public class VelocityUtil {    /*** 将内容写到vm模板中,存放在指定目录下* VelocityContext 传送内容类* list 数据集合* colMap 列名字* templetePath 模板* */    public static void createDoc(List list, String templetePath,String docpath,Map<String,String> colMap) throws Exception {     VelocityContext vc = new VelocityContext();  //传到vm的内容        String classpath = VelocityUtil.class.getResource("/").getPath();         Properties ps = new Properties();          ps.setProperty(VelocityEngine.FILE_RESOURCE_LOADER_PATH,         classpath.substring(0, classpath.length()-8) + "template/import");// 这是模板所在路径          VelocityEngine ve = new VelocityEngine();          ve.init(ps);          Template template = ve.getTemplate(templetePath+".vm", "utf-8");          File srcFile = new File(docpath);//输出路径          FileOutputStream fos = new FileOutputStream(srcFile);          BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(fos, "utf-8"));          Set<String> cols = colMap.keySet();    List<String> colspan = new ArrayList<>();    for (String col : cols) {    colspan.add(colMap.get(col).replace("\"", ""));}        vc.put("list", list);//数据集合        vc.put("cols", colspan);//列集合        template.merge(vc, writer);          writer.flush();          writer.close();          fos.close();      }          /**     * 由浏览器下载导出的表格     * @param list     * @param templeteName 模板名称     * @param excelName 导出表格名称     * @param response     * @param colMap 列     */public static void createDoc(List list, String templeteName, String excelName,HttpServletResponse response, Map<String, String> colMap) {try {VelocityContext vc = new VelocityContext();  //传到vm的内容   String classpath = VelocityUtil.class.getResource("/").getPath();    Properties ps = new Properties();     ps.setProperty(VelocityEngine.FILE_RESOURCE_LOADER_PATH,     classpath.substring(0, classpath.length()-8) + "template/import");// 这是模板所在路径     VelocityEngine ve = new VelocityEngine();     ve.init(ps);     Template template = ve.getTemplate(templeteName+".vm", "utf-8");   List<String> helpList = new ArrayList<String>(colMap.keySet());Collections.sort(helpList, new Comparator<String>() {public int compare(String a, String b) {return a.compareTo(b);}});Set<String> cols = new TreeSet<String>(helpList);    List<String> colsEN = new ArrayList<>();    List<String> colsCN = new ArrayList<>();    for (String col : cols) {    colsCN.add(colMap.get(col).replace("\"", ""));    colsEN.add(col.split("_")[1]);}       vc.put("list", list);//数据集合       vc.put("colsCN", colsCN);//列集合       vc.put("colsEN", colsEN);//列集合   response.addHeader("Cache-Control","no-cache");     response.setCharacterEncoding("UTF-8");     response.setContentType("application/vnd.ms-excel;charset=UTF-8");     response.addHeader("Content-Disposition","attachment;filename=" + new String(excelName.getBytes(),"ISO8859-1"));      PrintWriter write = response.getWriter();    //解析模版      template.merge(vc, write);    if(write != null){      write.flush();      write.close();      }  } catch (Exception e) {e.printStackTrace();}}}  

1 0
原创粉丝点击