先说一下流程
1.根据条件将数据从数据库读出
2.将数据通过outputStream写入
3.生成excel
如何读取数据库就不说了
下面是依赖
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.9</version>
</dependency>
@RequestMapping("/getTrainExcel")
public void search_archives(@RequestParam("activity_guid") String activity_guid,
HttpServletRequest request, HttpServletResponse response) throws IOException {
//list就是我读出的数据,Cys_activity_detail是我的一个实体类 只有姓名和电话号码两个属性,你可以用你自己的
List<Cys_activity_detail> list = trainActivityService.getOneActivityAll(activity_guid);
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("测试表"); //表的名字 一个sheet
HSSFRow row1=sheet.createRow(0);
HSSFCell cell=row1.createCell(0);
cell.setCellValue("活动报名表"); //设置单元格内容
sheet.addMergedRegion(new CellRangeAddress(0,0,0,3));
HSSFRow row2=sheet.createRow(1);
row2.createCell(0).setCellValue("姓名");
row2.createCell(1).setCellValue("电话号码");
for(int i = 0;i < list.size();i++) {
HSSFRow row3=sheet.createRow(i+2);
row3.createCell(0).setCellValue(list.get(i).getEnrolment_name());
row3.createCell(1).setCellValue(list.get(i).getTEL());
}
//shuchu excel
OutputStream output=response.getOutputStream();
response.reset();
response.setHeader("Content-disposition", "attachment; filename=Student.xls");
response.setContentType("application/msexcel");
wb.write(output);
output.close();
}
你学废了嘛!