Excel 생성 및 다운로드

3983 단어 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();
        }
    }

 

좋은 웹페이지 즐겨찾기