ExcelUtils 是一个在WEB应用开发中的EXCEL导出工具,它基于POI和beanutil工程。类似于velocity,它拥有自己标签。但是ExcelUtils 的标签是写在EXCEL中的。通过EXCEL的自定义标签我们可以自由编辑EXCEL的格式。不用编辑你的应用程序代码。ExcelUtils 会解析你的EXCEL模板中的标签并向模板中填充数据来生成自已的报表。
1,EXCELUTILS所需的JAR包:excelutils.jar poi-2.5.1.jar commons-logging.jar commons-digester.jar commons-beanutils.jar,加外可能还需加添加osh.jar包。将上述jar包加入自己的web工程的classpath中。
2,定义自己的excel模板:
在excel中添加excelutils的自定义标签,常用标签如下:
#foreach detail in ${list}
${detail.name} ${detail.qty} ...
#end
迭代输出list中数据
另外一些标签:
${printDate}
${model.name}
#formula SUM(C${detailStartRowNo}:C${detailEndRowNo})
#each ${model}
#each ${model} on ${keys}
#each ${model} ${width1},${width2}... on ${keys}
#sum qty on ${list} where name=test
#sum qty on ${list} where name like test
#sum qty on ${list} where name like ${value}
#call service.getStr("str",${aaa})
#call service.getModel("str",${aaa}).name
#formula SUM(C${currentRowNo}:F${currentRowNo})
3,编写JAVA代码:
List list= new ArrayList();
Model model = new Model();
.........
list.add(model)
ExcelUtils.addValue("list", list);
ExcelUtils.addSerivce("service", service);
String config = "/WEB-INF/xls/demo.xls";
response.reset(); response.setContentType("application/vnd.ms-excel");
ExcelUtils.export(getServlet().getServletContext(),
config,response.getOutputStream());
return null;