response.setContentType("octets/stream");
response.addHeader("Content-Disposition","attachment;filename=test.xls");
// 第一步,创建一个webbook,对应一个Excel文件
HSSFWorkbook wb = new HSSFWorkbook();
// 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
HSSFSheet sheet = wb.createSheet("飞DAY调查结果");
// 第四步,创建单元格,并设置值表头 设置表头居中
HSSFCellStyle style = wb.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式
//创建第一行
HSSFRow row = sheet.createRow(0);
//创建第一行的第一个单元格
HSSFCell cells= row.createCell(0);
//设置单元格的值
cells.setCellValue("飞DAY调查结果");
cells.setCellStyle(style);
//合并单元格 0,0,0,3 第一行开始,第一行结束,第一列开始,3表示多少列结束
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 3));
//创建标题栏
row = sheet.createRow(1);
HSSFCell cell = row.createCell(0);
cell.setCellValue("学号");
cell.setCellStyle(style);
cell = row.createCell(1);
cell.setCellValue("姓名");
cell.setCellStyle(style);
cell = row.createCell(2);
cell.setCellValue("年龄");
cell.setCellStyle(style);
cell = row.createCell(3);
cell.setCellValue("生日");
cell.setCellStyle(style);
// 第五步,写入实体数据 实际应用中这些数据从数据库得到,
List<Student> list = new ArrayList<Student>();
list.add(new Student(1, "张三", 20, new Date()));
list.add(new Student(1, "张三", 20, new Date()));
list.add(new Student(1, "张三", 20, new Date()));
list.add(new Student(1, "张三", 20, new Date()));
list.add(new Student(1, "张三", 20, new Date()));
for (int i = 0; i < list.size(); i++)
{
row = sheet.createRow((int) i + 1);
Student stu = (Student) list.get(i);
// 第四步,创建单元格,并设置值
HSSFCell cell1=row.createCell(0);
HSSFCell cell2=row.createCell(1);
HSSFCell cell3=row.createCell(2);
cell1.setCellStyle(style);
cell1.setCellValue((double) stu.getId());
cell2.setCellStyle(style);
cell2.setCellValue(stu.getName());
cell3.setCellStyle(style);
cell3.setCellValue((double) stu.getAge());
cell = row.createCell(3);
cell.setCellValue(new SimpleDateFormat("yyyy-mm-dd").format(stu
.getBirth()));
cell.setCellStyle(style);
}
// 第六步,将文件存到指定位置
try
{
//Java 服务器端
wb.write(response.getOutputStream());
//客户端版
FileOutputStream os = new FileOutputStream("D:\\report\\workbook.xls");
wb.write(os);
os.close();
}
catch (Exception e)
{
e.printStackTrace();
}finally{
}
return null;
}
response.addHeader("Content-Disposition","attachment;filename=test.xls");
// 第一步,创建一个webbook,对应一个Excel文件
HSSFWorkbook wb = new HSSFWorkbook();
// 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
HSSFSheet sheet = wb.createSheet("飞DAY调查结果");
// 第四步,创建单元格,并设置值表头 设置表头居中
HSSFCellStyle style = wb.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式
//创建第一行
HSSFRow row = sheet.createRow(0);
//创建第一行的第一个单元格
HSSFCell cells= row.createCell(0);
//设置单元格的值
cells.setCellValue("飞DAY调查结果");
cells.setCellStyle(style);
//合并单元格 0,0,0,3 第一行开始,第一行结束,第一列开始,3表示多少列结束
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 3));
//创建标题栏
row = sheet.createRow(1);
HSSFCell cell = row.createCell(0);
cell.setCellValue("学号");
cell.setCellStyle(style);
cell = row.createCell(1);
cell.setCellValue("姓名");
cell.setCellStyle(style);
cell = row.createCell(2);
cell.setCellValue("年龄");
cell.setCellStyle(style);
cell = row.createCell(3);
cell.setCellValue("生日");
cell.setCellStyle(style);
// 第五步,写入实体数据 实际应用中这些数据从数据库得到,
List<Student> list = new ArrayList<Student>();
list.add(new Student(1, "张三", 20, new Date()));
list.add(new Student(1, "张三", 20, new Date()));
list.add(new Student(1, "张三", 20, new Date()));
list.add(new Student(1, "张三", 20, new Date()));
list.add(new Student(1, "张三", 20, new Date()));
for (int i = 0; i < list.size(); i++)
{
row = sheet.createRow((int) i + 1);
Student stu = (Student) list.get(i);
// 第四步,创建单元格,并设置值
HSSFCell cell1=row.createCell(0);
HSSFCell cell2=row.createCell(1);
HSSFCell cell3=row.createCell(2);
cell1.setCellStyle(style);
cell1.setCellValue((double) stu.getId());
cell2.setCellStyle(style);
cell2.setCellValue(stu.getName());
cell3.setCellStyle(style);
cell3.setCellValue((double) stu.getAge());
cell = row.createCell(3);
cell.setCellValue(new SimpleDateFormat("yyyy-mm-dd").format(stu
.getBirth()));
cell.setCellStyle(style);
}
// 第六步,将文件存到指定位置
try
{
//Java 服务器端
wb.write(response.getOutputStream());
//客户端版
FileOutputStream os = new FileOutputStream("D:\\report\\workbook.xls");
wb.write(os);
os.close();
}
catch (Exception e)
{
e.printStackTrace();
}finally{
}
return null;
}