如何使用java代码直接将BIRT报告导出到excel。我无法连接到数据库。我打开了 excel 表,但我没有从数据库中获取其中的内容。
这是我的代码:
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);
}
}