做导出excel文件操作

		response.setContentType("application/octet-stream");
		response.setHeader("Content-Disposition","attachment; filename=" + java.net.URLEncoder.encode("业绩目标列表.xls","UTF-8"));
		WritableWorkbook book = Workbook.createWorkbook(response.getOutputStream());
		// sheet
		WritableSheet sheet = book.createSheet("业绩目标列表", 0);
		// 内容项
		// 行头label
        WritableFont titlefont = new WritableFont(WritableFont.createFont("宋体"), 14, WritableFont.BOLD);
        WritableFont labelfont = new WritableFont(WritableFont.createFont("宋体"), 12, WritableFont.BOLD);
        WritableFont textfont = new WritableFont(WritableFont.createFont("宋体"), 10, WritableFont.NO_BOLD);
        
        WritableCellFormat titleformat = new WritableCellFormat(titlefont);
        WritableCellFormat labelformat = new WritableCellFormat(labelfont);
        //文本格式
        WritableCellFormat textformat = new WritableCellFormat(NumberFormats.TEXT);
        
        labelformat.setAlignment(Alignment.CENTRE);
        labelformat.setVerticalAlignment(VerticalAlignment.CENTRE); 
        
        textformat.setAlignment(Alignment.LEFT);
        textformat.setVerticalAlignment(VerticalAlignment.CENTRE);	       
        
//        sheet.setRowView(0, 400);
        
        sheet.setColumnView(0, 20);
        sheet.setColumnView(1, 20);
        sheet.setColumnView(2, 20);
        sheet.setColumnView(3, 20); 
        sheet.setColumnView(4, 20);
        sheet.setColumnView(5, 20); 
        sheet.setColumnView(6, 20); 
        sheet.setColumnView(7, 20); 
        sheet.setColumnView(8, 30);
        
        
        Label label = new Label(0, 0, "组别名称",labelformat);
		sheet.addCell(label);
					
		label = new Label(1, 0, "月份",labelformat);
		sheet.addCell(label);
		
		label = new Label(2, 0, "目标1",labelformat);
		sheet.addCell(label);
				
		label = new Label(3, 0, "目标2",labelformat);
		sheet.addCell(label);
		
		label = new Label(4, 0, "目标3",labelformat);
		sheet.addCell(label);
		
		label = new Label(5, 0, "目标4",labelformat);
		sheet.addCell(label);
		
		label = new Label(6, 0, "目标5",labelformat);
		sheet.addCell(label);
		label = new Label(7, 0, "目标6",labelformat);
		sheet.addCell(label);
		label = new Label(8, 0, "每日平均通时目标业绩(分钟)",labelformat);
		sheet.addCell(label);
		
		if(teamDtoList!=null && teamDtoList.size()>0){
			for(int i=0;i<teamDtoList.size();i++){
								
				CustomerDTO niceDto1 = (CustomerDTO)teamDtoList.get(i);
				
				label = new Label(0, i+1, niceDto1.getGroupname(),textformat);			
				sheet.addCell(label);
				
				label = new Label(1, i+1, niceDto1.getInitMonth(),textformat);			
				sheet.addCell(label);
				
				
				if(null==niceDto1.getEndNumberStr()||niceDto1.getEndNumberStr()==""){
					label =new Label(2, i+1,"",textformat);
				}else{
					label =new Label(2, i+1, niceDto1.getEndNumberStr(),textformat);
				}
				sheet.addCell(label);
				
				if(null==niceDto1.getChallengeNumberStr()||niceDto1.getChallengeNumberStr()==""){
					label = new Label(3, i+1,"",textformat);
				}else{
					label = new Label(3, i+1, niceDto1.getChallengeNumberStr(),textformat);
				}
				sheet.addCell(label);
				
				if(null==niceDto1.getThreeNumberStr()||niceDto1.getThreeNumberStr()==""){
					label = new Label(4, i+1,"",textformat);
				}else{
					label = new Label(4, i+1, niceDto1.getThreeNumberStr(),textformat);
				}
				sheet.addCell(label);
				
				if(null==niceDto1.getFourNumberStr()||niceDto1.getFourNumberStr()==""){
					label = new Label(5, i+1,"",textformat);
				}else{
					label = new Label(5, i+1, niceDto1.getFourNumberStr(),textformat);
				}
				sheet.addCell(label);
				
				if(null==niceDto1.getFiveNumberStr()||niceDto1.getFiveNumberStr()==""){
					label = new Label(6, i+1, "",textformat);
				}else{
					label = new Label(6, i+1, niceDto1.getFiveNumberStr(),textformat);
				}
				sheet.addCell(label);
				
				if(null==niceDto1.getSixNumberStr()||niceDto1.getSixNumberStr()==""){
					label = new Label(7, i+1, "",textformat);
				}else{
					label = new Label(7, i+1, niceDto1.getSixNumberStr(),textformat);
				}
				sheet.addCell(label);
				
				if(null==niceDto1.getAveragePassTime()){
					label = new Label(8, i+1,"",textformat);
				}else{
					label = new Label(8, i+1, niceDto1.getAveragePassTime().toString(),textformat);
				}
				sheet.addCell(label);
			}
		}
																				
		book.write();
		book.close();
            	
    	return null;

 

你可能感兴趣的:(Excel)