`
luoqiaowen
  • 浏览: 9218 次
  • 性别: Icon_minigender_1
  • 来自: 广州
最近访客 更多访客>>
文章分类
社区版块
存档分类
最新评论

从零开始认识 JasperReport + IReport (JasperReport部分)

 
阅读更多
因参与公司南非的项目,需要应用在Linux平台,所以报表改用JasperReport.国内的项目报表还是用FastReport(看来还是结晶啊).

废话少说,我从delphi平台转到RCP开发才不过两月时间,以前从未接触过JasperReport.(简称为JR)
今天上级要求熟悉JR,为节后国际化报表做准备.以下为我从0开始的一些记录.
目的: 力求理解JasterReport的各个概念及之间的关系.熟悉打印报表流程的来龙去脉,主要的调用方法(如加载报表,打印,预览).
报表设计器的使用.

1. IReport 设计器,就像FastReport一样有这样的设计器.但名字叫IReprot,为安装文件,我安装的版本为
iReport-3.0.0-windows-installer.exe,同事传我的,下载地址百度下下就有了.
2. JasperReport为一个开发Jar包,就是相当于FastReport的报表控件.
3. 初始化JasterReport

private static JasperPrint initJasperReport(String fileName,
Map<String, Object> paramMap, List data) throws IOException,
MalformedURLException, JRException {
...
InputStream is = fullPathString.toURL().openStream(); //这句以上代码都是处理报表文件路径
JasperReport jasperReport = (JasperReport) JRLoader.loadObject(is);
Object[] objArray = data.toArray();
//JasperReport对象 + Map对象 + List 对象 =JasperPrint对象
final JasperPrint jasperPrint = JasperFillManager.fillReport(
jasperReport, paramMap, new JRBeanArrayDataSource(objArray));
return jasperPrint;
}

4.查找默认打印机(打印服务).
public static boolean directPrintByPrintName(final JasperPrint jasperPrint) {
if (jasperPrint != null) {
try {
PrintService[] PSs = PrinterJob.lookupPrintServices(); //java.awt.*包.查找所有打印服务.
PrintService ps = null;
if (PSs != null && PSs.length > 1&& !Assert.isNull(MzTransParam.PrinterOfSyddyj)) {
for (int i = 0; i < PSs.length; i++) {
String sps = PSs[i].toString();
sps = sps.replace("Win32 Printer : ", ""); //$NON-NLS-1$ //$NON-NLS-2$
//MzTransParam.PrinterOfSyddyj 我们系统设置的默认打印机名称.
if (sps.equalsIgnoreCase(MzTransParam.PrinterOfSyddyj)) {
ps = PSs[i];//得到打印服务对象
break;
}
}
}
5.设置打印参数,好多个参数
if (ps != null) {
long start = System.currentTimeMillis();
PrintRequestAttributeSet printRequestAttributeSet = new HashPrintRequestAttributeSet();
printRequestAttributeSet.add(MediaSizeName.ISO_A5);// 处方模板是A5纸 第一个参数对象

PrintServiceAttributeSet printServiceAttributeSet = new HashPrintServiceAttributeSet();
printServiceAttributeSet.add(new PrinterName(ps.getName(),null)); //第二个参数对象

final JRPrintServiceExporter exporter = new JRPrintServiceExporter(); //关键的对象,其它的对象都是为他服务的
//以下为设置参数
exporter.setParameter(JRExporterParameter.JASPER_PRINT,jasperPrint);
exporter.setParameter(JRPrintServiceExporterParameter.PRINT_REQUEST_ATTRIBUTE_SET,
printRequestAttributeSet);
exporter.setParameter(JRPrintServiceExporterParameter.PRINT_SERVICE_ATTRIBUTE_SET,
printServiceAttributeSet);
exporter.setParameter(JRPrintServiceExporterParameter.DISPLAY_PAGE_DIALOG,
Boolean.FALSE);
exporter.setParameter(JRPrintServiceExporterParameter.DISPLAY_PRINT_DIALOG,
Boolean.FALSE);
6.关键的出场,在线程里导出报表.(打印)
Thread thread = new Thread(new Runnable() {
public void run() {
try {
exporter.exportReport(); //就这么一句.exporter对象导出报表.
} catch (Exception ex) {
System.err.println(ex.getLocalizedMessage());
}
}
});
thread.start();
7.采用默认打印.
} else { //此处的else接的是5条的if
Thread thread = new Thread(new Runnable() {
public void run() {
try {
//jasperPrint 对象就是JasperPrintManager生成的.参考上面的代码.
JasperPrintManager.printReport(jasperPrint,false); //这一句应该是默认打印.
} catch (Exception ex) {
}
}
});
thread.start();
}
} catch (Exception ex) {
return false;
}

}
return true;}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics