excel下载
来源:互联网 发布:湖南理工学院网络 编辑:程序博客网 时间:2024/05/29 19:22
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