org.apache.poi.hssf.usermodel.HSSFWorkbook,
org.apache.poi.hssf.usermodel.HSSFSheet,
org.apache.poi.hssf.usermodel.HSSFRow,
org.apache.poi.hssf.usermodel.HSSFCell"%>
Stringpath=request.getContextPath();StringbasePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%>
">
Listlist=(List)request.getAttribute("ceshiList");StringtitleStr= "导出的excel的名称";Stringtitle= new String(titleStr.getBytes("gb2312"),"iso-8859-1");
response.setHeader("Content-Disposition","attachment;filename=\"" + title +".xls"+"\"");
HSSFWorkbook wb= newHSSFWorkbook();
HSSFFont font=wb.createFont();
font.setFontHeightInPoints((short)14);
font.setBoldweight((short)600);
HSSFSheet sheet=wb.createSheet("sheet1");
sheet.setColumnWidth((short)0,(short)(1500));
sheet.setColumnWidth((short)1,(short)(4500));
sheet.setColumnWidth((short)2,(short)(4500));
sheet.setColumnWidth((short)3,(short)(4500));
HSSFCellStyle style=wb.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
HSSFCellStyle styleFont=wb.createCellStyle();
styleFont.setAlignment(HSSFCellStyle.ALIGN_CENTER);
styleFont.setFont(font);//以下以写表头//表头为第一行
HSSFRow head=sheet.createRow((short)0);
HSSFCell headCell=head.createCell((short)0);
sheet.addMergedRegion(newRegion(0,(short)0,0,(short)3));//合并单元格
HSSFRow row=sheet.createRow((short)1);
HSSFCell cell1=row.createCell((short)0);
HSSFCell cell2=row.createCell((short)1);
HSSFCell cell3=row.createCell((short)2);
HSSFCell cell4=row.createCell((short)3);
headCell.setEncoding(HSSFCell.ENCODING_UTF_16);
headCell.setCellType(HSSFCell.CELL_TYPE_STRING);
headCell.setCellStyle(styleFont);
cell1.setEncoding(HSSFCell.ENCODING_UTF_16);
cell1.setCellType(HSSFCell.CELL_TYPE_STRING);
cell1.setCellStyle(style);
cell2.setEncoding(HSSFCell.ENCODING_UTF_16);
cell2.setCellType(HSSFCell.CELL_TYPE_STRING);
cell2.setCellStyle(style);
cell3.setEncoding(HSSFCell.ENCODING_UTF_16);
cell3.setCellType(HSSFCell.CELL_TYPE_STRING);
cell3.setCellStyle(style);
cell4.setEncoding(HSSFCell.ENCODING_UTF_16);
cell4.setCellType(HSSFCell.CELL_TYPE_STRING);
cell4.setCellStyle(style);//定义表头的内容
headCell.setCellValue(titleStr);
cell1.setCellValue("序号");
cell2.setCellValue("姓名");
cell3.setCellValue("班级");
cell4.setCellValue("生日");
SimpleDateFormat sdf= newSimpleDateFormat("yyyy-MM-dd HH:mm:ss");for(inti=1; i
Ceshi ceshi=list.get(i-1);//定义数据从第二行开始
row=sheet.createRow((short) i+ 1);
cell1=row.createCell((short)0);
cell2=row.createCell((short)1);
cell3=row.createCell((short)2);
cell4=row.createCell((short)3);
headCell.setEncoding(HSSFCell.ENCODING_UTF_16);
headCell.setCellType(HSSFCell.CELL_TYPE_STRING);
headCell.setCellStyle(styleFont);
cell1.setEncoding(HSSFCell.ENCODING_UTF_16);
cell1.setCellType(HSSFCell.CELL_TYPE_STRING);
cell1.setCellStyle(style);
cell2.setEncoding(HSSFCell.ENCODING_UTF_16);
cell2.setCellType(HSSFCell.CELL_TYPE_STRING);
cell2.setCellStyle(style);
cell3.setEncoding(HSSFCell.ENCODING_UTF_16);
cell3.setCellType(HSSFCell.CELL_TYPE_STRING);
cell3.setCellStyle(style);
cell4.setEncoding(HSSFCell.ENCODING_UTF_16);
cell4.setCellType(HSSFCell.CELL_TYPE_STRING);
cell4.setCellStyle(style);//填充内容
cell1.setCellValue(i);if(ceshi.getUserName() != null){
cell2.setCellValue(ceshi.getUserName());
}else{
cell2.setCellValue("");
}if(ceshi.getClassName() != null){
cell3.setCellValue(ceshi.getClassName());
}else{
cell3.setCellValue("");
}if(ceshi.getBirth() != null){
cell4.setCellValue(sdf.format(ceshi.getBirth()));//转换数据库出来的date类型为简单日期格式
}else{
cell4.setCellValue("");
}
}
wb.write(response.getOutputStream());
response.getOutputStream().flush();
out.clear();
response.getOutputStream().close();
out.clear();
out=pageContext.pushBody();%>