excel下载


function downloadUser(){
	var status = Number($("input:radio[name=status]:checked").val());
	var orderSort = $("input:radio[name=sort]:checked").val();
	var downloadHref = "../../app/plugin/vote/downloadUser.json?appId="+g_appId+"&activityId="+g_activityId+"&pageNo=1"
					+"&pageSize=2&totalCount=-1&status="+status+"&orderSort="+orderSort+"&sortNum="+$("#searchSortNum").val();
					+"&name="+$("#searchname").val()+"&searchTicketNum="+$("#searchTicketNum").val();
	window.open(downloadHref);
					
}



@RequestMapping(value = "/plugin/vote/downloadUser.json")
	public  void downloadUser(
			@RequestParam String appId, 
			@RequestParam int activityId,
			@RequestParam int pageNo,
			@RequestParam int pageSize, 
			@RequestParam int totalCount,
			@RequestParam int status,
			@RequestParam(required = false) Integer sortNum,
			@RequestParam(required = false) String name,
			@RequestParam(required = false) Integer searchTicketNum,
			@RequestParam(required = false) Integer orderSort, 
			HttpServletRequest request, HttpServletResponse response){
		VoteActivity activity = voteConfigService.getActivity(activityId);
		if(activity != null && activity.getAppId().equals(appId)){
			List<String> titles = new ArrayList<String>();
			titles.add("姓名");
			titles.add("票数");
			titles.add("序号");
			titles.add("状态");
			Page page = voteConfigService.pageUser(pageNo, pageSize, totalCount, activityId, status, name, searchTicketNum, orderSort, sortNum);
			List<List<Map<String, Object>>> list = new ArrayList<>();
			list.add(page.getData());
			for(int i = pageNo+1; i <= page.getTotalPageCount(); i++){
				page = voteConfigService.pageUser(i, pageSize, totalCount, activityId, status, name, searchTicketNum, orderSort, sortNum);
				list.add(page.getData());
			}
			HSSFWorkbook workbook = new HSSFWorkbook();
			HSSFSheet sheet = workbook.createSheet("选手信息");
			HSSFRow row = sheet.createRow(0);
			for (int i = 0; i < titles.size(); i++)
			{
				HSSFCell cl0 =row.createCell(i);
				cl0.setCellValue(titles.get(i));
			}
			int i = 1;
			for (int j = 0; j< list.size();j++) {
				for (Map<String, Object> map : list.get(j)) {
					HSSFRow row1 = sheet.createRow(i);
					row1.createCell(0);
					row1.createCell(1);
					row1.createCell(2);
					row1.createCell(3);
					row1.createCell(4);
					if(map.get("name") != null)
						row1.getCell(0).setCellValue(map.get("name").toString());
					if(map.get("ticket_num") != null)
						row1.getCell(1).setCellValue(map.get("ticket_num").toString());
					if(map.get("sort") != null)
						row1.getCell(2).setCellValue(map.get("sort").toString());
					String sta = map.get("status").toString();
					if(sta != null && !"".equals(sta)){
						Integer s = Integer.parseInt(sta);
						row1.getCell(3).setCellValue((s==1?"显示":(s==2?"隐藏":"")));
					}
					i++;
				}
			}
			
			 try {
				ByteArrayOutputStream os = new ByteArrayOutputStream();
				    workbook.write(os);
				    byte[] content = os.toByteArray();
				    InputStream is = new ByteArrayInputStream(content);
				    // 设置response参数,可以打开下载页面
				    response.reset();
				    String fileName = activity.getTitle() +"选手信息";
					response.setContentType("application/vnd.ms-excel;charset=utf-8");
					response.setHeader("Content-Disposition", "attachment;filename="+ new String((fileName+".xls").getBytes("UTF-8"),"ISO-8859-1"));
				    ServletOutputStream out = response.getOutputStream();
				    BufferedInputStream bis = null;
				    BufferedOutputStream bos = null;
 
				    try {
				        bis = new BufferedInputStream(is);
				        bos = new BufferedOutputStream(out);
				        byte[] buff = new byte[2048];
				        int bytesRead;
				        while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
				            bos.write(buff, 0, bytesRead);
				        }
				    } catch (Exception e) {
				        e.printStackTrace();
				    } finally {
				    	if(is != null)
				    		is.close();
				    	if(workbook != null)
				    		workbook.close();
				        if (bis != null)
				            bis.close();
				        if (bos != null)
					    bos.close();
					if(out != null)
					{
					    out.flush();
					    out.close();
					}	
				    }
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值