非常直白,请君自行饮用
//下载excel
@RequestMapping("/downloadAllAppoint")
@ResponseBody
public String reprotRecord(HttpServletResponse response) throws IOException {
// 文件名称
String fileName = URLEncoder.encode("刷卡记录.xlsx", "utf-8");
// 通过response设置Content-Type、Content-Disposition
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition",
"attachment;filename*=utf-8'zh_cn'" + fileName);
//生成workBook
// HSSFWorkbook workbook = createWorkbook();
OutputStream outputStream = null;
HSSFWorkbook workBook = null;
try {
// 获取输出流
outputStream = response.getOutputStream();
// 生成workBook
workBook = downloadAllAppoint();
workBook.write(outputStream);
} catch (IOException e) {
e.printStackTrace();
} finally {
//关闭
if (outputStream != null) {
outputStream.close();
}
}
return null;
}
public HSSFWorkbook downloadAllAppoint() throws IOException {
///创建个空白的workbook[name:workbook]
HSSFWorkbook workbook = new HSSFWorkbook();
//创建个空白的sheet[name:appoint]
HSSFSheet sheet = workbook.createSheet("appoint");
//行
HSSFRow row;
//单元格
HSSFCell cell;
//创建行循环标签
int i = 0;
//创建行
row = sheet.createRow(i);
//列名标柱
cell = row.createCell(0);
cell.setCellValue("预约编号");
cell = row.createCell(1);
cell.setCellValue("预约用户账号");
cell = row.createCell(2);
cell.setCellValue("用户名");
cell = row.createCell(3);
cell.setCellValue("手机号");
cell = row.createCell(4);
cell.setCellValue("预约项目");
cell = row.createCell(5);
cell.setCellValue("预约时间");
cell = row.createCell(6);
cell.setCellValue("预约状态");
//查询所有预约信息
List<Appoint> appointList = appointService.selectAllAppoint();
//开始循环
for (Appoint appoint : appointList) {
i++;
row = sheet.createRow(i);
cell = row.createCell(0);
cell.setCellValue(appoint.getAppoint_id());
cell = row.createCell(1);
cell.setCellValue(appoint.getAppoint_userid());
cell = row.createCell(2);
cell.setCellValue(appoint.getAppoint_username());
cell = row.createCell(3);
cell.setCellValue(appoint.getAppoint_phonenum());
cell = row.createCell(4);
cell.setCellValue(appoint.getAppoint_project());
cell = row.createCell(5);
cell.setCellValue(appoint.getAppoint_time());
cell = row.createCell(6);
cell.setCellValue(appoint.getAppoint_status());
}
System.out.println("orderCondition.xls--[status:success]!");
return workbook;
}