//service方法
@Override
public HSSFWorkbook exprot() {
// 创建HSSFWorkbook对象(excel的文档对象)
HSSFWorkbook wb = new HSSFWorkbook();
// 建立新的sheet对象(excel的表单)
HSSFSheet sheet = wb.createSheet("验证用户名单表");
// 在sheet里创建第一行,参数为行索引(excel的行),可以是0~65535之间的任何一个
HSSFRow row1 = sheet.createRow(0);
// 创建单元格(excel的单元格,参数为列索引,可以是0~255之间的任何一个
HSSFCell cell = row1.createCell(0);
// 设置单元格内容
cell.setCellValue("验证用户名单");
// 合并单元格CellRangeAddress构造参数依次表示起始行,截至行,起始列, 截至列
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 5));
// 在sheet里创建第二行
HSSFRow row2 = sheet.createRow(1);
// 创建单元格并设置单元格内容
row2.createCell(0).setCellValue("证件类型");
row2.createCell(1).setCellValue("证件号码");
row2.createCell(2).setCellValue("手机号");
row2.createCell(3).setCellValue("卡号");
row2.createCell(4).setCellValue("操作类型");
row2.createCell(5).setCellValue("操作时间");
// 在sheet里创建第三行
List<CardLog> cList = this.findAll();
HSSFRow row;
for (int i = 0; i < cList.size(); i++) {
int j = i + 1;
row = sheet.createRow(j + 1);
row.createCell(0).setCellValue(cList.get(i).getCredentialType().getValue());
row.createCell(1).setCellValue(cList.get(i).getCredentialId());
row.createCell(2).setCellValue(cList.get(i).getPhoneNumber());
row.createCell(3).setCellValue(cList.get(i).getCardId());
row.createCell(4).setCellValue(cList.get(i).getOperateType().getValue());
row.createCell(5).setCellValue(cList.get(i).getOperateTime().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
}
return wb;
}
//查询数据集合
private List<CardLog> findAll() {
// TODO Auto-generated method stub
return null;
}
//controller
@RequestMapping("/export/file")
public void export(HttpServletRequest request, HttpServletResponse response) throws IOException {
// 创建HSSFWorkbook对象(excel的文档对象)
HSSFWorkbook wb = cardLogManager.exprot();
// 输出Excel文件
OutputStream output = response.getOutputStream();
response.reset();
response.setHeader("Content-disposition", "attachment; filename=cardLog.xls");
response.setContentType("application/octet-stream");
wb.write(output);
output.write(wb.getBytes());
output.close();
}