@GetMapping("exportExcel")
public void exportExcel (HttpServletResponse response ,String day) throws IOException{
if((day!=null&&day.trim().equals(""))||day==null) day = "0";
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("信息表");
List<SbView> sbViewList = sopBorrowLogService.findAllSbView();
String fileName = "sop_borrow" + ".xls";//设置要导出的文件的名字
//新增数据行,并且设置单元格数据
int rowNum = 1;
//headers表示excel表中第一行的表头
String[] headers = { "客户","机种","品名","料号","借用人工号","借用人", "借用人部门","借出确认者工号","借出时间","已借天数"};
//在excel表中添加表头
HSSFRow row = sheet.createRow(0);
for(int i=0;i<headers.length;i++){
HSSFCell cell = row.createCell(i);
HSSFRichTextString text = new HSSFRichTextString(headers[i]);
cell.setCellValue(text);
}
//在表中存放查询到的数据放入对应的列
for(SbView sbView:sbViewList){
if(sbView.getReturnTime()!=null) continue; //已经归还了
//借阅时间没超过7天的
LocalDateTime localDateTime = LocalDateTime.now();
LocalDateTime borrowTime = sbView.getBorrowTime();
Duration duration = Duration.between(borrowTime,localDateTime);
long d = duration.toDays();
if(duration.toDays()<Integer.parseInt(day)) continue;
HSSFRow row1 = sheet.createRow(rowNum);
row1.createCell(0).setCellValue(sbView.getSop().getCustomer());
row1.createCell(1).setCellValue(sbView.getSop().getMachineName());
row1.createCell(2).setCellValue(sbView.getSop().getProduction());
row1.createCell(3).setCellValue(sbView.getSop().getPart());
row1.createCell(4).setCellValue(sbView.getBorrowJobNo());
row1.createCell(5).setCellValue(sbView.getBorrower());
row1.createCell(6).setCellValue(sbView.getBorrowerDept());
row1.createCell(7).setCellValue(sbView.getLentJobNo());
Date date = Date.from( borrowTime.atZone( ZoneId.systemDefault()).toInstant());
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
row1.createCell(8).setCellValue(sdf.format(date));
row1.createCell(9).setCellValue(duration.toDays());//已借天书
rowNum++;
}
response.setContentType("application/octet-stream");
response.setHeader("Content-disposition", "attachment;filename=" + fileName);
response.flushBuffer();
workbook.write(response.getOutputStream());
}