HSSFWorkbook使用

有关这方面知识和用法网上一搜一大堆。

啥也不多说,直接上代码:

自己运行通过。

		response.setContentType("APPLICATION/OCTET-STREAM"); 
		response.setHeader("Content-Disposition",  "attachment; filename=\"" + "excel(excelName.xls" + "\""); 
		    
		HSSFSheet sheet = wb.createSheet("sheetName");
			
		int cols = 40; //因为科室不定,so长度不定,暂设为40
		HSSFCellStyle sheetStyle = wb.createCellStyle();  // Sheet样式 
		//设置边线
	    	sheetStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
	    	sheetStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
	    	sheetStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
	    	sheetStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
	    	// 设置列的样式  
		    for (int i = 0; i < cols; i++) {  
		    	sheet.setDefaultColumnStyle((short) i, sheetStyle); 
		    	//设置列宽
		    	if(i < 2){
		    		sheet.setColumnWidth(i, 2000);
		    	}else if(i == 2){
		    		sheet.setColumnWidth(i, 6000);
		    	}else{
		    		sheet.setColumnWidth(i, 12000);
		    	}
		    	sheet.setDefaultRowHeight((short)500);
		    }
	    	
			int index = 1;
			int count = 0;
			int deptIndex = 0;
			
			//循环
			for(ZyyBaseVO base : baseList){
				
				int i = 0;
				HSSFRow row = sheet.createRow(deptIndex + count);	//科室列表createRow包括人员数和科室数
				
				row.createCell(i++).setCellValue("序号");
				row.createCell(i++).setCellValue("基地");
				row.createCell(i++).setCellValue("姓名");
				
				//基地下科室
				for(ZyyDeptVO deptVO : base.getDeptList()){
					row.createCell(i++).setCellValue(deptVO.getDeptName());
				}
				//基地下人员
				for(ZyyUserExtendVO user : base.getUserList()){
					HSSFRow r = sheet.createRow(index + count);	//人员数
					int j = 0;
					r.createCell(j++).setCellValue(index);
					
					StringBuffer userInfo = new StringBuffer();
					userInfo.append(user.getRealName());
					userInfo.append("(");
					userInfo.append(user.getUserYear());
					userInfo.append("级/");
					userInfo.append(user.getUserDeptName());
					userInfo.append(")");
					r.createCell(j++).setCellValue(base.getAliasName());
					r.createCell(j++).setCellValue(userInfo.toString());
					
					for(ZyyDeptVO dept : base.getDeptList()){
						List<ZyyCycleTableResiCycleVO> deptList = user.getResiCycleMap().get(dept.getDeptName());
						String dateString = getCycleDate4export(deptList);
						r.createCell(j++).setCellValue(dateString);
					}
					index ++;
					deptIndex ++;
				}
				count ++;
			}
		}
	
		//开始导出
		try{
			ServletOutputStream stream=response.getOutputStream();
			wb.write(stream);
		}catch(Exception e){
			
		}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值