因參與公司南非的項目,需要應用在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;}
未完.