`
cn_arthurs
  • 浏览: 322125 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

spring导出excel

阅读更多

导出EXCEL代码

/**
	 * 统计Excel导出
	 * @param request
	 * @param response
	 * @return
	 */
	public ModelAndView exportcheckintotal(HttpServletRequest request,HttpServletResponse response){
		String mid = request.getParameter("meetingid");
		ModelAndView mav = new ModelAndView();
		if(!"".equals(mid)&&mid!=null){
			try {
			Meeting meeting = this.meetingService.getMeeting(mid);
			List rowlist = this.meetingService.getrows(mid);		
			List cList = this.meetingService.getCheckin(mid);
			HSSFWorkbook wb = ExcelUtil.exportExcel(rowlist,cList);  
			response.setContentType("application/vnd.ms-excel"); 
			response.setHeader("Content-disposition", "attachment; filename="+ExcelUtil.toUtf8String(meeting.getTitle())+".xls");
			wb.write(response.getOutputStream());
			} catch (Exception e) {
				e.printStackTrace();
			}	
		}
		return null;
	}

 导出文件名中文处理

public static String toUtf8String(String s){ 
	     StringBuffer sb = new StringBuffer(); 
	       for (int i=0;i<s.length();i++){ 
	          char c = s.charAt(i); 
	          if (c >= 0 && c <= 255){sb.append(c);} 
	        else{ 
	        byte[] b; 
	         try { b = Character.toString(c).getBytes("utf-8");} 
	         catch (Exception ex) { 
	             System.out.println(ex); 
	                  b = new byte[0]; 
	         } 
	            for (int j = 0; j < b.length; j++) { 
	             int k = b[j]; 
	              if (k < 0) k += 256; 
	              sb.append("%" + Integer.toHexString(k).toUpperCase()); 
	              } 
	     } 
	  } 
	  return sb.toString(); 
	}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics