第1章 概述
在页面中查看报表时,可以将报表导出为多种文件格式,包括Excel、Pdf、Csv、Rtf、Ods等。此外UniEAP Report还为报表导出提供了Java API,可以在后台直接生成各种格式的文件。
第2章 利用API实现后台导出
【注意】使用导出API的前提是:报表引擎已经启动并成功初始化。
各种导出方式的执行步骤基本相同,下面仅以Excel为例进行说明。
1.创建报表引擎实例
ReportEngine engine = ReportManager.create();
2.创建上下文对象
Map context = new HashMap(3);
3.新建环境对象
Environment env = new ThreadLocalEnvironment();
4.将必要参数保存到上下文对象中
a) 环境对象
ReportContextUtil.setContext(context, ReportContext.ENV, env);
b) 缓存ID,此参数非必须
ReportContextUtil.setContext(context, ReportContext.CACHEID,cachedId);
5.构造报表参数Map对象,假设报表所需参数为a和b,参数值分别为1和“2”
Map params = new HashMap();
params.put(“a”, “1”);
params.put(“b”, “2”);
注意参数值必须是String类型,无论报表定义中参数的类型是什么。
6.取得报表
Report report = engine.getReport(reportId, params, context);
reportId :报表ID
params : 报表参数的键值对
context :上下文对象
7.指定目标文件,构造输出流
File file = new File(“target.xls”);
OutputStream os = new FileOutputStream(file);
8.导出报表
report.exportToExcel(os, context);
如果是导出Pdf,则为report.exportToPdf(os, context),其他格式同理。