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
- velocity导出Word,excel模板的工具类
- 模板导出word和 EXcel
- velocity往word/excel模版填充数据并导出
- velocity根据模板导出word并下载工具方法
- 利用模板导出word里面生成excel
- POI导出Excel模板工具类(使用jxls)
- java 通过velocity导出word
- 使用Velocity导出Word文档
- 导出Excel工具类
- 导出Excel工具类
- Excel导出工具类
- 导出Excel工具类
- Excel导出工具类.
- EXCEL导出工具类
- 导出Excel工具类
- Excel导出工具类
- 【工具】模板引擎 Velocity
- 利用poi导出excel的工具类
- tp 模板布局
- Android与web缠绵
- iphone oc/swift 编程视频
- 让下拉列表可编辑
- 20161216C语言基础19_文件处理函数
- velocity导出Word,excel模板的工具类
- Eclipse中设置JVM内存方法
- 单点登录的三种实现方式
- 禁止listview下拉
- Cancan权限角色设计的最佳实践
- hdu-1029 Ignatius and the Princess IV
- Windows+VS2013爆详细Caffe编译安装教程
- 学习笔记 android开发 一些函数记录
- cocopods 安装