velocity导出Word,excel模板的工具类
2016-12-16 16:50
281 查看
1,先把要导出的模板转成xml格式,然后加到项目中,将后缀名改成.vm
2.以下是工具类,两种(一个是导出指定位置,一个是浏览器下载)
工具类代码如下:
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(); } } }
相关文章推荐
- velocity根据模板导出word并下载工具方法
- Asp.net开发经验-利用Aspose.Words按模板导出Word(注:Excel可以使用Aspose.Excel)
- velocity 导出excel模板
- C# Excel 模板导出(Word等类似)
- 模板导出word和 EXcel
- 利用模板导出word里面生成excel
- velocity往word/excel模版填充数据并导出
- 按照word/Excel模板导出word/excel
- 在.NET环境下将报表数据导出EXCEL和WORD
- 在.NET环境下将报表导出EXCEL和WORD
- 将ASP.NET页面内的数据导出到Excel 或 Word中
- 导出sql server 数据库为Excel的数据字典的小工具
- 水晶报表导出为word,excel和pdf格式
- 将ASP.NET页面内的数据导出到Excel 或 Word中
- 扩展GridView控件(8) - 导出数据源的数据为Excel、Word或Text
- 把本页内容导出成word文件或excel文件
- WEB页面导出为EXCEL和WORD文档的方法(一)
- (转)从数据库导出数据到word、excel、.txt
- 使用DTS导出到Access,FoxPro,Word ,Excel 不需要验证的大批量数据。
- [转帖]一键导出Word和Excel文件的简单服务器控件