jxl 實現導出excel

public void exportRegCountBySource() throws IOException{
		regRateList = new ArrayList<Map<String,Object>>();
		DecimalFormat df=new DecimalFormat("0.00");//小數點格式化
		List<Map<String,Object>> otherRegCountList = this.regRateService.queryRegCountOfOtherProvinces(); // 外省註冊人數
		List<Map<String,Object>> otherStuCountList = this.regRateService.queryStuCountOfOtherProvinces(); // 外省招生人數
		List<Map<String,Object>> cityList = this.regRateService.queryCityByProvince(); // 本省城市列表
		int otherRegCount = 0; // 外省註冊數
		int otherStuCount = 0; // 外省招生數
		if (otherStuCountList.size() > 0) {
			otherStuCount = Integer.parseInt(otherStuCountList.get(0).get("count").toString());
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("name", "外省");
			map.put("stuNum", otherStuCount);
			if (otherRegCountList.size() > 0) {
				otherRegCount = Integer.parseInt(otherRegCountList.get(0).get("count").toString());
				map.put("regNum", otherRegCount);
				if (otherStuCount != 0) {
					Double tmpDou = (Double.parseDouble(String.valueOf(otherRegCount))/Double.parseDouble(String.valueOf(otherStuCount))) * 100;
					map.put("regRate", String.valueOf(df.format(tmpDou))+"%"); // 註冊率
				} else {
					map.put("regRate", 0);
				}
			} else {
				map.put("regNum", 0);
				map.put("regRate", 0);
			}
			regRateList.add(map);
		}
		if (cityList.size() > 0) {
			for (int i = 0; i < cityList.size(); i++) {
				String cityName = cityList.get(i).get("ccodename").toString(); // 城市名稱
				String cityCode = cityList.get(i).get("ccodevalue").toString(); // 城市編碼
				List<Map<String,Object>> cityStuCountList = this.regRateService.queryStuCountByCity(Integer.parseInt(cityCode)); // 城市對應招生人數
				List<Map<String,Object>> cityRegCountList = this.regRateService.queryRegCountByCity(Integer.parseInt(cityCode)); // 城市對應註冊人數
				if (cityStuCountList.size() > 0) {
					int cityStuNum = 0;
					int cityRegNum = 0;
					cityStuNum = Integer.parseInt(cityStuCountList.get(0).get("count").toString());
					Map<String, Object> map = new HashMap<String, Object>();
					map.put("name", cityName);
					map.put("stuNum", cityStuNum);
					if (cityRegCountList.size() > 0) {
						cityRegNum = Integer.parseInt(cityRegCountList.get(0).get("count").toString());
						map.put("regNum", cityRegNum);
						if (cityStuNum != 0) {
							Double tmpDou = (Double.parseDouble(String.valueOf(cityRegNum))/Double.parseDouble(String.valueOf(cityStuNum))) * 100;
							map.put("regRate", String.valueOf(df.format(tmpDou))+"%"); // 註冊率
						} else {
							map.put("regRate", 0);
						}
					} else {
						map.put("regNum", 0);
						map.put("regRate", 0);
					}
					regRateList.add(map);
				}
				
			}
		}
		
// 上面是數據源,下面是導出實現
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			String filename = "按生源地統計註冊率報表"+sdf.format(new Date());
			response.setHeader("Content-Disposition","attachment;filename="+new String(filename.getBytes("GB2312"),"ISO8859_1")+".xls");
			response.setContentType("application/octet-stream");	
			OutputStream os = response.getOutputStream();
			jxl.write.WritableWorkbook wwb = Workbook.createWorkbook(os);
		    jxl.write.WritableSheet ws = wwb.createSheet("按生源地統計註冊率", 0);
		    WritableFont font1=new WritableFont(WritableFont.TIMES,12,WritableFont.BOLD); 
		    WritableCellFormat titleFormat=new WritableCellFormat(font1);
			titleFormat.setAlignment(jxl.format.Alignment.CENTRE);
			titleFormat.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
			titleFormat.setBorder(Border.ALL, BorderLineStyle.THIN,Colour.BLACK);
			WritableCellFormat contentFormat=new WritableCellFormat();
			contentFormat.setBorder(Border.ALL, BorderLineStyle.THIN,Colour.BLACK);    
			contentFormat.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
			ws.setColumnView(0,30); // 將第一列的寬度設爲30
			for (int i = 1; i < 4; i++) {
				ws.setColumnView(i,20); // 將第一列的寬度設爲30
			}
			String[] cols = {"城市","招生數","已註冊","註冊率"};
			for (int j=0;j<cols.length;j++){
				ws.addCell(new jxl.write.Label(j, 0, cols[j],titleFormat));								
			}
		    int rowIndex = 1;
			int i = 0;
			if (regRateList.size() > 0) {
				for (i=0;i<regRateList.size();i++) {
		    		ws.addCell(new jxl.write.Label(0, rowIndex+i,regRateList.get(i).get("name").toString(),contentFormat));
		    		ws.addCell(new jxl.write.Label(1, rowIndex+i,regRateList.get(i).get("stuNum").toString(),contentFormat));
		    		ws.addCell(new jxl.write.Label(2, rowIndex+i,regRateList.get(i).get("regNum").toString(),contentFormat));
		    		ws.addCell(new jxl.write.Label(3, rowIndex+i,regRateList.get(i).get("regRate").toString(),contentFormat));
				}
			}
		    wwb.write();
		    wwb.close();
		    os.close();
		} catch (WriteException e) {
			e.printStackTrace();
		}
	}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章