2017-02-13 26 views
1

您好我正在嘗試使用@Scheduled自動生成xls報告(cron =「0 0/1 * * * *「)在特定的時間,但它不能正常工作,請您及時誰給你Idea.Thank如何使用@Scheduled(cron =「0 0/1 * * * *」)自動在Spring MVC中生成xls報告

+0

你的問題是不執行的cron?一些例外? – cralfaro

+0

@Scheduled(cron =「0 0/1 * * * *」) \t public void freezeDateTwo(){ \t \t System.out.println(「shudler Working」); \t \t service.updateFreezeOnDateTwoAndSixteen(); \t \t List user = service.getExtUser(); \t \t excelReport(user); \t} private ModelAndView excelReport(List user){ \t \t System.out.println(「excelReport」); \t \t返回新的ModelAndView(「excelView」,「tourPlan」,用戶); \t}這個玉米每分鐘運行成功1分鐘,但最後回報只有它不工作...先生請它有任何解決方案...它不會產生錯誤.. –

+0

我的意思是,當你運行你的應用程序或調試它時,你在日誌中看到一些異常?該應用轉到方法freezeDataTwo()? – cralfaro

回答

1

嗨根據與你正在嘗試做的,我會用這種方法:

  1. 使用cron每天生成報告文件並將其存儲在某個特定位置
  2. 創建網址以便讀取任何報告按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()); 
}