2013-07-08 126 views
1

如何直接使用java代碼將BIRT報告導出爲ex​​cel。 我無法連接到數據庫。我打開了Excel表單,但沒有從數據庫中獲取內容。以excel格式導出BIRT報告

這裏是我的代碼:

public void openExcelReport() throws IOException, ServletException { 

    String reportFile = "testreport.rptdesign"; 

    IReportEngine birtReportEngine = null; 
    EngineConfig conf = null; 

    HttpServletResponse resp = ServletActionContext.getResponse(); 
    request = ServletActionContext.getRequest(); 
    resp.setContentType("application/vnd.ms-excel"); 
    String fileName = "BE-Mechanical-2010-11"; 
    resp.setHeader("Content-disposition", "attachment; filename=" + fileName); 
    ServletContext sc = request.getSession().getServletContext(); 

    try { 
    conf = new EngineConfig(); 
    conf.setEngineHome("ReportEngine"); 
    conf.setLogConfig(null, Level.FINE); 
    IReportEngineFactory factory = (IReportEngineFactory) Platform 
     .createFactoryObject(IReportEngineFactory.EXTENSION_REPORT_ENGINE_FACTORY); 
    birtReportEngine = factory.createReportEngine(conf); 

    IReportRunnable design = null; 
    // Open report design 
    design = birtReportEngine.openReportDesign(sc.getRealPath("/birt_files/" 
     + reportFile)); 
    // create task to run and render report 
    IRunAndRenderTask task = birtReportEngine.createRunAndRenderTask(design); 

    // set output options 
    HTMLRenderOption options = new HTMLRenderOption(); 
    options.setOutputFormat("html"); 

    options.setOutputStream(resp.getOutputStream()); 
    task.setRenderOption(options); 
    // run report 
    task.run(); 
    task.close(); 

    } catch (Exception e) { 
    e.printStackTrace(); 
    throw new ServletException(e); 
    } 
} 
+0

是否要生成Excel文件?爲什麼在這種情況下設置'HTMLRenderOption'? – Claude

回答

0

我不知道它可以幫助或沒有,但.. 在這種情況下,你需要你的XML文件,是什麼意思reportFileFullPath如果 你XML顯示記錄,您將在excel上也可以看到記錄。

def reportFileFullPath = "${reportHome}${File.separator}${reportName}.rptdesign" 

     if(File.separator.equals("/")) 
      reportFileFullPath = reportFileFullPath.replace("\\", File.separator) 
     else 
      reportFileFullPath = reportFileFullPath.replace("/", File.separator) 

     def IReportRunnable reportDesign = reportEngine.openReportDesign(reportFileFullPath)