有关这方面知识和用法网上一搜一大堆。
啥也不多说,直接上代码:
自己运行通过。
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){
}