public void dataExportExcel(HttpServletResponse response){
String fileName = "xxx.xls";
response.setCharacterEncoding("utf-8");
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment;fileName="+fileName);
// 创建一个webbook,对应一个Excel文件
HSSFWorkbook wb = new HSSFWorkbook();
// 在webbook中添加一个sheet,对应Excel文件中的sheet
HSSFSheet sheet = wb.createSheet("班级名单");
// 在sheet中添加表头第0行
HSSFRow row = sheet.createRow(0);
// 创建单元格,添加excel样式
HSSFCellStyle style = wb.createCellStyle();
style.setWrapText(true);
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//水平居中
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直居中
sheet.setDefaultColumnWidth(20);//列宽
//添加字体颜色
Font font = null;
font = wb.createFont();
font.setColor(wb,HSSFColor.RED.index);
style.setFont(font);
//添加表头
HSSFCell cell = row.createCell(0);
cell.setCellValue("序号");
cell.setCellStyle(style);
cell = row.createCell(1);
cell.setCellValue("学员");
cell.setCellStyle(style);
row = sheet.createRow(1);//第一行数据
row.setHeight((short) 500);//行高
HSSFCell cell = row.createCell(0);
cell.setCellValue("1");
cell.setCellStyle(style);
cell = row.createCell(1);
cell.setCellValue(李四");
cell.setCellStyle(style);
wb.write(response.getOutputStream());
}