JasperReport有如下幾個主要的Manager:
JasperCompileManager
|
將jrxml文件編譯成jasper文件或JasperReport對象
|
JasperFillManager
|
從數據源中獲取數據并填充jasper文件或JasperReport對象,產生JasperPrint對象
|
JasperPrintManager
|
將JasperPrint對象直接輸出到打印機打印
|
JasperExportManager
|
將JasperPrint對象導出成PDF或HTML文件(或流)
|
JasperRunManager
|
運行一個jasper文件或JasperReport對象并輸出到PDF或Html文件(或流)中,我的理解是等于集成了JasperFillManager和JasperExportManager的功能
|
對于上述對象的詳細說明請參閱JasperReport的文檔。
例程:
例程:
public static void main(String[] args) {
try {
Class.forName("org.gjt.mm.mysql.Driver");
Connection con = DriverManager.getConnection(
"jdbc:mysql://wes:3306/umpdb?useUnicode=true&characterEncoding=GBK", "root", "elite");
File f1 = new File("Account.jrxml");
File f2 = new File("Contact.jrxml");
JasperReport main = JasperCompileManager.compileReport(f1
.getAbsolutePath());
JasperReport report = JasperCompileManager.compileReport(f2
.getAbsolutePath());
Map props = new HashMap();
props.put("subreport", report);
JasperPrint print = JasperFillManager.fillReport(main, props,con);
//JasperPrintManager.printReport(print,true);
JasperExportManager.exportReportToPdfFile(print, "a.pdf");
//JasperRunManager.runReportToPdfFile("Account.jasper", "a.pdf", props, con);
} catch (ClassNotFoundException e) {
// TODO 自動生成 catch 塊
e.printStackTrace();
} catch (SQLException e) {
// TODO 自動生成 catch 塊
e.printStackTrace();
} catch (JRException e) {
// TODO 自動生成 catch 塊
e.printStackTrace();
}
}
3 注意事項
1.iReport中新建中文報表的時候記得要輸入XML編碼GB2312。
2.iReport中定義中文字體的時候,要選擇PDF Font Name為STSong-Light(宋體)、MHei-Medium(黑體)、MSung-Light,在PDF Encoding中選擇GB-UCS2-H(Chinese Simplified),否則輸出到PDF時會亂碼。
3.用Report SQL查詢之后,可能會出現某些字段沒有自動建立Field,就需要在Field中手動定義了。