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

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
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值