生成Excel并下载

       /**
	 * 生成Excel
	 * @since 2015-2-6
	 * @param request
	 * @param response
	 * @param rsMap
	 * @return
	 */
	private void createExcel(HttpServletRequest request,HttpServletResponse response,Map<Integer,Map<String, String>> rsMap){
		WorkbookSettings settings=new WorkbookSettings();
		settings.setEncoding("utf-8");
        // 文件名称与路径
        String realPath = request.getContextPath();
        String fileName = "用户信息表.xls";
        String excelUrl = SystemConfig.excelUrl;
        File file = new File(realPath + excelUrl);//导出文件存放的位置
        if (!file.exists()) {
             file.mkdirs();
        }
        realPath = realPath + excelUrl + fileName;
        // 建立工作薄并写表头
        try {
	         WritableWorkbook wwb = Workbook.createWorkbook(new File(realPath),settings);
	         WritableSheet sheet = wwb.createSheet("Sheet1", 0);// 建立工作簿
	         // 写表头
	         sheet.mergeCells(0, 0, 3, 0);
	         jxl.write.Label labelTitle = new jxl.write.Label(0, 0, "用户信息表");
	         sheet.addCell(labelTitle);// 放入工作簿
	         Label label1 = new Label(0,1,"UID");
	         Label label2 = new Label(1,1,"姓名");
		     Label label3 = new Label(2,1,"手机号");
		     Label label4 = new Label(3,1,"地址");
		     sheet.addCell(label1);
		     sheet.addCell(label2);
		     sheet.addCell(label3);
		     sheet.addCell(label4);
	         // 写入信息
		     int i = 0;
	         for(Integer uid : rsMap.keySet()){  
        	    Map<String,String> map = rsMap.get(uid);
        	    for(String paramKey : map.keySet()){
        	    	String paramValue = map.get(paramKey);
        	    	Label label5 = new Label(0,i+2,uid.toString());
        	    	sheet.addCell(label5);
        	    	if(paramKey.equals(Constants.UserCommunicationInfo.USER_NAME)){
        	    		Label label6 = new Label(1,i+2,paramValue);
        	    		sheet.addCell(label6);
        	    	}
        	    	if(paramKey.equals(Constants.UserCommunicationInfo.USER_MOBILE)){
        	    		Label label7 = new Label(2,i+2,paramValue);
        	    		sheet.addCell(label7);
        	    	}
        	    	if(paramKey.equals(Constants.UserCommunicationInfo.USER_ADDR)){
        	    		Label label8 = new Label(3,i+2,paramValue);
        	    		sheet.addCell(label8);
        	    	}
        	    }
        	    i++;
	         }
	         // 写入Exel工作表
	         wwb.write();
	         // 关闭Excel工作薄对象
	         wwb.close();
        }catch(Exception e){
        	log.error("生成Excel失败"+e);
        	e.printStackTrace();
        }
        downloadExcel(response,fileName,realPath);
	}
	
	/**
	 * 下载Excel
	 * @since 2015-2-6
	 * @param response
	 * @param fileName
	 * @param realPath
	 */
	public void downloadExcel(HttpServletResponse response,String fileName,String realPath){
		try{
			response.setHeader("Content-Disposition", "attachment; filename=" + new String(fileName.getBytes("utf-8"),"iso-8859-1"));
			response.setContentType("application/vnd.ms-excel; charset=utf-8");
			response.setCharacterEncoding("utf-8"); 
			OutputStream out = response.getOutputStream();
			InputStream in = new FileInputStream(realPath);
			byte[] buffer = new byte[1024];
			int i = -1;
			while ((i = in.read(buffer)) != -1) {
				out.write(buffer, 0, i);
			}
			in.close();
			out.flush();
			out.close();
        }catch(Exception e){
        	log.error("下载Excel失败"+e);
        	e.printStackTrace();
        }
    }

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值