簡體   English   中英

如何使用@Scheduled(cron = "0 0/1 * * * *") 在Spring MVC中自動生成xls報告

[英]How to using @Scheduled(cron = "0 0/1 * * * *") automatically Generate xls report in Spring MVC

嗨,我正在嘗試在特定時間使用 @Scheduled(cron = "0 0/1 * * * *") 自動生成 xls 報告,但它不起作用。請任何人提供想法。謝謝

嗨,根據您的嘗試,我會使用這種方法:

  1. 每天使用cron生成一個報告文件,存放在某個特定的位置
  2. 創建一個 URL 以通過 dd/MM/yyyy 閱讀任何報告

所以從技術上講是這樣的:

@Scheduled(cron = "0 0/1 * * * *") 
public void freezeDateTwo(){ 
    System.out.println("shudler Working");
    service.updateFreezeOnDateTwoAndSixteen(); 
    List<ExtusrRole> user = service.getExtUser(); 
    excelReport(user); 
    //In this point you should have created a report in some specific location
} 

在您的控制器中

@RequestMapping(value = "/reports/{dd}/{mm}/{yyyy}", method = RequestMethod.GET)
@ResponseBody
private void excelReport(@PathVariable("dd") Integer day,
                         @PathVariable("mm") Integer month,
                         @PathVariable("yyyy") Integer year,
                         HttpServletResponse response) {
    System.out.println("excelReport"); 
    //Search from your specific location the right report
    File file = //Your file  
    String mimeType = URLConnection.guessContentTypeFromName(file.getName());
    response.setContentType(mimeType);
    response.setHeader("Content-Disposition", String.format("attachment; filename=\"%s\"", file.getName()));
    response.setContentLength((int) file.length());
    InputStream inputStream = new BufferedInputStream(new FileInputStream(file));
    FileCopyUtils.copy(inputStream, response.getOutputStream());
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM