1.依赖
org.apache.poi
poi
3.17
org.apache.poi
poi-ooxml
3.17
2.后端代码
@GetMapping("/writeExcel")
public Result writeExcel() throws IOException {
List list = borrowService.list(null);
for (Borrow thisBorrow : list) {
thisBorrow.setBook(bookService.getById(thisBorrow.getBid()));
thisBorrow.setUser(userService.getById(thisBorrow.getUid()));
}
String fileName = "C:\Users\26913\Desktop\报表"+new Date().getTime()+".xls";
//创建一个簿
Workbook workbook=new HSSFWorkbook();
//创建一张表
Sheet sheet = workbook.createSheet("报表");
Row head = sheet.createRow(0);
Cell cell = head.createCell(0);
cell.setCellValue("序号");
cell = head.createCell(1);
cell.setCellValue("借阅人编号");
cell = head.createCell(2);
cell.setCellValue("借阅人姓名");
cell = head.createCell(3);
cell.setCellValue("借阅人电话");
cell = head.createCell(4);
cell.setCellValue("借阅人性别");
cell = head.createCell(5);
cell.setCellValue("图书编号");
cell = head.createCell(6);
cell.setCellValue("图书名称");
cell = head.createCell(7);
cell.setCellValue("借阅时间");
cell = head.createCell(8);
cell.setCellValue("归还时间");
for (int i = 0; i < list.size(); i++) {
Row row = sheet.createRow(i+1);
row.createCell(0).setCellValue(i+1);
row.createCell(1).setCellValue(list.get(i).getUid());
row.createCell(2).setCellValue(list.get(i).getUser().getName());
row.createCell(3).setCellValue(list.get(i).getUser().getPhone());
row.createCell(4).setCellValue(list.get(i).getUser().getSex());
row.createCell(5).setCellValue(list.get(i).getBid());
row.createCell(6).setCellValue(list.get(i).getBook().getName());
row.createCell(7).setCellValue(list.get(i).getBorrowTime());
row.createCell(8).setCellValue(list.get(i).getStillTime());
}
FileOutputStream outputStream = new FileOutputStream(fileName);
workbook.write(outputStream);
//关闭流
outputStream.close();
return Result.ok().message("导出成功");
}
3.效果展示