一。 下載ireport( 官方網站:http://ireport.sourceforge.net),本人下載ireport3.7.0。
二。 搭建ireport+jasperreports java開發平臺。將ireport3.7.0安裝路徑下
D:\iReport-3.7.0 \ireport\modules\ext下的(jasperreports-3.7.jar,
iText- 2.1.0.jar,iTextAsian.jar,commons-logging-1.1.jar,commons-collections-3.2.1.jar,
commons- digester-1.7.jar,poi-3.2-FINAL-20081019.jar,groovy-all-1.5.5.jar)的jar包加入到
java classPath下。其中各jar包的版本以你下載的ireport的\ireport\modules\ext路徑下jar爲準。
各jar包作用如下:
jasperreports-3.7.jar:該包的作用完成ireport中Preview按扭功能。通過該包可以通過java來ireport生成摸班
(.jrxml或.jasper)填充數據源並導出pdf,excel,html等格式文件。
iText-2.1.0.jar:生成pdf所依賴的包。
iTextAsian.jar:支持pdf中文字體依賴的包。
groovy-all-1.5.5.jar:將.jrxml編譯成.jasper.如果你不使用java編譯,而使用ireport工具編譯則無須導入該 包。
commons-digester-1.7.jar:解析.jrxml
poi-3.2-FINAL-20081019.jar:生成excel報表所依賴的jar
三。使用javaBean數據源開發子報表例子如下:
第一步:創建主報表javaBean(貨物)如下(set,get方法省列):
public class DailySales implements Serializable {
private static final long serialVersionUID = 1L;
private String productNo;//貨物代號
private String productName;//貨物名稱
private int number;
private int money;//貨物價格
private int id;//貨物唯一標識
private List<DailyZoom> dailyZooms;//貨物銷售地區(子報表)
public DailySales(String productNo, String productName, int number, int money,
List<DailyZoom> dailyZooms) {
this.productNo = productNo;
this.productName = productName;
this.number = number;
this.money = money;
this.dailyZooms = dailyZooms;
}
}
第二步:創建子報表報表javaBean(貨物銷售地區)如下(set,get方法省列):
public class DailyZoom implements Serializable {
private String name;
private BigDecimal amounts;
public DailyZoom(String name, BigDecimal amounts) {
this.name = name;
this.amounts = amounts;
}
}
注意:如果在報表中需要排序則上述類必須實現Comparable接口,並實現其方法。
第三步:創建DailySalesFactory工廠類提供集合數據源如下:
public class DailySalesFactory {
private static DailySales[] data = { new DailySales(" 貨號 1", " 物品1 ", 1, 1000, getDailyZoom()),
new DailySales(" 貨號 2", " 物品 2", 2, 2000, getDailyZoom()),
new DailySales(" 貨號 3", " 物品 3", 3, 3000, getDailyZoom()),
new DailySales(" 貨號 4", " 物品 4", 4, 4000, getDailyZoom()),
new DailySales(" 貨號 5", " 物品 5", 5, 5000, getDailyZoom()),
new DailySales(" 貨號 6", " 物品 6", 6, 6000, getDailyZoom()),
new DailySales(" 貨號 7", " 物品 7", 7, 7000, getDailyZoom()),
new DailySales(" 貨號 8", " 物品 8", 8, 8000, getDailyZoom()),
new DailySales(" 貨號 9", " 物品 9", 9, 9000, getDailyZoom()),
new DailySales(" 貨號 10", " 物品 10", 10, 10000, getDailyZoom()) };
public static Object[] getBeanArray() {
return data;
}
public static Collection getBeanCollection() {
return Arrays.asList(data);
}
public static List getDailyZoom() {
List<DailyZoom> dailyZooms = new ArrayList();
dailyZooms.add(new DailyZoom("廈門", new BigDecimal(123.00)));
dailyZooms.add(new DailyZoom("南昌", new BigDecimal(456.00)));
return dailyZooms;
}
}
將上述創建的類設置到ireport3.7.0的classPath目錄下(工具》選項》classPath),然後配置javaBean數據源如下:
第四步:設定ireport模版如下:
注意:(1)pdf中文設定:首先將iTextAsian.jar設置到classPath下.然後fonts path下勾選iTextAsian.jar路徑和jasperreports-extensions-3.5.3.jar路徑,最後將會顯示中文的文本Pdf Font name is now deprecated:STSong-Light,pdfExcoding:UniGB-UCS2-V (Chinese Simplified)(V是從上往下顯示,H是從左往右顯示)。
第五步:從java中加載ireport做好的模板生成報表如下:
將jrxml編譯成jasper:
生成JasperPrint對象:
生成pdf流對象:JasperExportManager.exportReportToPdfStream(jasperPrint, stream);
調用ireport中JasperViewer查看pdf
JasperViewer jrview = new JasperViewer(jasperPrint);
jrview.setPreferredSize(new Dimension(200, 100));
jrview.setVisible(true);
exporter.setParameter(JRXlsExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRXlsExporterParameter.OUTPUT_STREAM, streamExcel);
exporter.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.FALSE);
exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE);
exporter.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE);
exporter.exportReport();
parameters.put("SUBREPORT_DIR", "C:/Documents and Settings/user/桌面/");
然後數據填充時傳一個參數即可如:
Map parameters = new HashMap();
parameters.put(JRParameter.REPORT_VIRTUALIZER, virtualizer);
<resultname="success"type="jasper">
<paramname="location">/reports/demoReport.jasper</param>
<paramname="dataSource">list</param>
<paramname="format">PDF</param>
<paramname="reportParameters">map</param>
</result>
</action>
</package>
</struts>
location - 提供利用iReport製作編譯好的報表文件。
dataSource - 數據源
format - 報表輸出格式
reportParameters - 報表參數
原因:沒有導入 groovy-all.jar 包,導入 groovy-all.jar 包就可以了。
(3)Excle 應用:
java.lang.NoClassDefFoundError: org/apache/poi/hssf/usermodel/HSSFCellStyle
出現上述錯誤提示加入 poi-3.2-FINAL-20081019.jar
(4)net.sf.jasperreports.engine.JRRuntimeException: Could not load the following font :
pdfFontName : STSong-Light
pdfEncoding : UniGB-UCS2-H
isPdfEmbedded : false
出現上述錯誤提示加入
iTextAsian.jar
(5)java.lang.NoClassDefFoundError: org/apache/commons/digester/Digester
出現上述錯誤提示加入 commons-digester-1.7.jar iReport/lib 功能簡述:解析 xml 文件;
(6)java.lang.NoClassDefFoundError: org/apache/commons/collections/ArrayStack 出現上述錯誤提示加入
commons-beanutils-1.7.jar iReport/lib
(7)java.lang.NoClassDefFoundError: org/apache/commons/collections/ReferenceMap 出現上述錯誤提示加入
commons-collections-2.1.jar iReport/lib
(8)
異常:Found two components for namespacehttp://jasperreports.sourceforge.net/jasperreports/components
解決方法.查看是否在工程目錄添加了外部jar [jasperreports-X.X.X.jar],[jasperreports-X.X.X-javaflow.jar]<XXX爲版本號>
如果有同時添加了這兩個jar.移除[jasperreports-3.5.3-javaflow.jar] 則可.