JXLS生成EXCEL并下载

来源:互联网 发布:软件水平考试报名 编辑:程序博客网 时间:2024/04/29 15:50
jxl.jar jxls-core-0.9.9.jarjxls-reader-0.9.9.jarpoi-3.5-FINAL.jar(必须3.5以上的版本)其他jar根据提示可以去 http://jarvana.com/jarvana/ 查找// 生成excel 传入模板文件 要生成的内容 生成文件 返回生成文件的完整路径public static String doExcel(String from, Map beans, String to) {ServletContext sc = (ServletContext) ActionContext.getContext().get(ServletActionContext.SERVLET_CONTEXT);String path = sc.getRealPath("/model");XLSTransformer transformer = new XLSTransformer();String sfrom = path + "\\" + from;// 模板文件String sto = path + "\\" + to;// 要生成的文件try {transformer.transformXLS(sfrom, beans, sto);} catch (ParsePropertyException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}return sto;}// 下载public static void doDownLoad(String path, String name,HttpServletResponse response) {try {response.reset();response.setHeader("Content-disposition","attachment;success=true;filename ="+ URLEncoder.encode(name, "utf-8"));BufferedInputStream bis = null;BufferedOutputStream bos = null;OutputStream fos = null;InputStream fis = null;File uploadFile = new File(path);fis = new FileInputStream(uploadFile);bis = new BufferedInputStream(fis);fos = response.getOutputStream();bos = new BufferedOutputStream(fos);// 弹出下载对话框int bytesRead = 0;byte[] buffer = new byte[8192];while ((bytesRead = bis.read(buffer, 0, 8192)) != -1) {bos.write(buffer, 0, bytesRead);}bos.flush();fis.close();bis.close();fos.close();bos.close();} catch (Exception e) {e.printStackTrace();}}调用:List resultList=new ArrayList();for (int i = 0; i < list.size(); i++) {。。。。。。InspectionStisfaction vo=new InspectionStisfaction();vo.setXjrq(SystemUtil.getTimeStr2Str(String.valueOf(map.get("apply_date"))));vo.setXjsl(lxjsl);vo.setFcmy(lfcmy);vo.setMy(lmy);vo.setYb(lyb);vo.setBmy(lbmy);vo.setFcbmy(lfcbmy);resultList.add(vo);}//生成EXcelMap beans = new HashMap();beans.put("resultList",resultList);String path=SystemUtil.doExcel("巡检商户满意度报表模板.xls" , beans, "巡检商户满意度报表.xls");SystemUtil.doDownLoad(path, "巡检商户满意度报表.xls",this.response);return null;    模板样式:巡检数量(个)  总体满意度非常满意满意   一般      不满意 非常不满意  合计<jx:forEach items="${resultList}" var="l">${l.xjrq}      ${l.xjsl}${l.fcmy}${l.my}   ${l.yb}  ${l.bmy} ${l.fcbmy}   $[B#+C#+D#]</jx:forEach>当月数量总计(个)  $[SUM(B4)]$[SUM(D4)]$[SUM(E4)] $[SUM(F4)] $[SUM(G4)] $[SUM(H4)]   $[SUM(I4)]