JXLS生成EXCEL并下载

jxl.jar 
jxls-core-0.9.9.jar
jxls-reader-0.9.9.jar
poi-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 block
		e.printStackTrace();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.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);
		}
		
		//生成EXcel
		Map 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)]
 

 

你可能感兴趣的:(JXLS生成EXCEL并下载)