java的導出Excel表格

導出Excel表格

public void export(HttpServletRequest request, HttpServletResponse response){
		try {
		Integer totalRow1 = 0,totalRow2 = 0,totalRow3 = 0,totalRow4= 0;
		List<String> deptName_list= new ArrayList<String>();
		List<String> ylist=new ArrayList<String>();
		List<String> tlist=new ArrayList<String>();
		List<String> zlist=new ArrayList<String>();
		List<String> yblist=new ArrayList<String>();
		System.out.println(list.size());
		for(Map<String,String> map:list){
			String deptName=map.get("deptname");
			deptNamelist.add(deptName);
			String total=map.get("total");
			ylist.add(total);
			totalRow1 += Integer.parseInt(map.get("total"));
			String tg=map.get("tg");
			tlist.add(tg);
			totalRow2 += Integer.parseInt(map.get("tg"));
			String zbl=map.get("zbl");
			zlist.add(zbl);
			totalRow3 += Integer.parseInt(map.get("zbl"));
			String ybl=map.get("ybj");
			yblist.add(ybl);
			totalRow4 += Integer.parseInt(map.get("ybj"));
		}
		
		deptName_list.add("總計");
		yc_list.add(totalRow1.toString());
		tg_list.add(totalRow2.toString());
		zbl_list.add(totalRow3.toString());
		ybj_list.add(totalRow4.toString());
		
		String startTime=request.getParameter("startTime");
		String endTime=request.getParameter("endTime");
		String start_time="";
		if(startTime!=null&&startTime.contains("-")){
			start_time=startTime.replace("-", "");
		}
		String end_time="";
		if(endTime!=null&&endTime.contains("-")){
			end_time=endTime.replace("-", "");
		}
		if(deptName_list!=null&&!deptName_list.isEmpty()){
			response.setContentType("application/ms-excel;charset=GB2312");
			response.setHeader("Content-Disposition", "attachment; filename="+start_time+"-"+end_time+new String("理詳情".getBytes("gb2312"), "ISO-8859-1")+".xls");
			ServletOutputStream os=response.getOutputStream();
			String[] titleContent={"部門名稱","常","過","理","結"};
			//創建工作薄
			WritableWorkbook workbook = Workbook.createWorkbook(os);
			//創建新的一頁
			WritableSheet sheet = workbook.createSheet("First Sheet", 0);
			//構造標題
			sheet.mergeCells(0, 0, titleContent.length-1, 0);//添加合併單元格,第一個參數是起始列,第二個參數是起始行,第三個參數是終止列,第四個參數是終止行
			WritableFont bold = new WritableFont(WritableFont.ARIAL,16,WritableFont.BOLD);//設置字體種類和黑體顯示,字體爲Arial,字號大小爲10,採用黑體顯示
			WritableCellFormat titleFormate = new WritableCellFormat(bold);//生成一個單元格樣式控制對象
			titleFormate.setAlignment(jxl.format.Alignment.CENTRE);//單元格中的內容水平方向居中
			titleFormate.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);//單元格的內容垂直方向居中BLACK
			titleFormate.setBorder(Border.ALL,BorderLineStyle.THIN,Colour.RED);
			Label title = new Label(0,0,"督查處理詳情",titleFormate);
			sheet.setRowView(0, 800, false);//設置第一行的高度
			sheet.addCell(title);
			
			//構造表頭
			for (int i = 0; i < titleContent.length; i++) {
				WritableFont titleContentBlod = new WritableFont(WritableFont.ARIAL,12,WritableFont.BOLD);//設置字體種類和黑體顯示,字體爲Arial,字號大小爲10,採用黑體顯示
				WritableCellFormat titleContentFormate = new WritableCellFormat(titleContentBlod);//生成一個單元格樣式控制對象
				titleContentFormate.setAlignment(jxl.format.Alignment.CENTRE);//單元格中的內容水平方向居中
				titleContentFormate.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);//單元格的內容垂直方向居中
				titleContentFormate.setBorder(Border.ALL,BorderLineStyle.THIN,Colour.RED);
				Label lable = new Label(i,1,titleContent[i],titleContentFormate);
				sheet.addCell(lable);
			}
			
			WritableFont content = new WritableFont(WritableFont.ARIAL,12,WritableFont.NO_BOLD);//設置字體種類和黑體顯示,字體爲Arial,字號大小爲10,採用黑體顯示
			WritableCellFormat contentFormate = new WritableCellFormat(content);
			contentFormate.setBorder(Border.ALL,BorderLineStyle.THIN,Colour.BLACK);//添加邊框
			for(int i=0;i<deptName_list.size();i++){
				Label deptNameLable = new Label(0,i+2,deptName_list.get(i),contentFormate);
				sheet.addCell(deptNameLable);
				Label ycNumLable = new Label(1,i+2,yc_list.get(i),contentFormate);
				sheet.addCell(ycNumLable);
				Label tgNumLable = new Label(2,i+2,tg_list.get(i),contentFormate);
				sheet.addCell(tgNumLable);
				Label zblNumLable = new Label(3,i+2,zbl_list.get(i),contentFormate);
				sheet.addCell(zblNumLable);
				Label ybjNumLable = new Label(4,i+2,ybj_list.get(i),contentFormate);
				sheet.addCell(ybjNumLable);
			}
			workbook.write();
			workbook.close();
			os.close();
			}
	    }catch(Exception e){
			e.printStackTrace();
		}
	}


發佈了96 篇原創文章 · 獲贊 15 · 訪問量 11萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章