| 圖示ireport 中使用javabean 作數(shù)據(jù)源開發(fā) 基于 jasperreports 報(bào)表 過程 ——學(xué)習(xí)筆記系列之ireport起步 xmlin 本文不講原理,因?yàn)榫W(wǎng)上的資源很多,本文以一個(gè)簡單的日銷售報(bào)表為例,記錄在 ireport 中使用 javabe an 作數(shù)據(jù)源開發(fā) 基于 jasperreports 報(bào)表 過程 . 一.準(zhǔn)備工作
 二.創(chuàng)建 javabean
 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 ; public DailySales(String productNo, String productName, int number, int money) { this . productNo = productNo; this . productName = productName; this . number = number; this . money = money; } public String getProductNo() { return productNo ; } public void setProductNo(String productNo) { this . productNo = productNo; } public String getProductName() { return productName ; } public void setProductName(String productName) { this . productName = productName; } public int getNumber() { return number ; } public void setNumber( int number) { this . number = number; } public int getMoney() { return money ; } public void setMoney( int money) { this . money = money; } public int getId() { return id ; } public void setId( int id) { this . id = id; } } 2 .生成數(shù)據(jù)源 import java.util.Arrays; import java.util.Collection; /** * 簡單工廠類,取得數(shù)據(jù) * @author xmlin * */ public class DailySalesFactory { // 為了在 ireport 中能夠測試,必須使用 static 方法 public static Collection<DailySales> getBeanCollection() { // 數(shù)據(jù)源的生成,通常調(diào)用 dao 操作 List<DailySales> data = new ArrayList<DailySales>(); for ( int i = 0; i < 100; i++) { data.add( new DailySales( " 貨號 " + i, " 產(chǎn)品 " + i, i, i * 100)); } return data; } } 三.使用 ireport 開發(fā)報(bào)表樣式1.新建一個(gè)項(xiàng)目。 2.設(shè)置類路徑,在菜單“ options” 中選擇 Classpath, 點(diǎn)擊在彈出框中的 add folder ,填寫 javabean 編譯成的 .class 文件存放的路徑 . 點(diǎn) save Classpath 完成。如圖 
 4. 設(shè)置活動(dòng)連接.在菜單" Data "中選擇” Set Active Connection”. 5. Report Query , 在菜單" Data "中選擇” Report Query”, 填寫 javabean, 即我們創(chuàng)建的 VO bean. 如圖 6 . 設(shè)計(jì)報(bào)表.  設(shè)計(jì)填充字段 , 如圖  設(shè)計(jì)頁數(shù)字段如圖 設(shè)計(jì)好的報(bào)表樣式如圖 點(diǎn)菜單" build "中的" compile "進(jìn)行編譯,然后再” execute with connection datasource” 就可以看到報(bào)表的結(jié)果了.  四.生成報(bào)表
 import java.awt.Dimension; import java.util.HashMap; import java.util.Map; import net.sf.jasperreports.engine.JRException; import net.sf.jasperreports.engine.JasperFillManager; import net.sf.jasperreports.engine.JasperPrint; import net.sf.jasperreports.engine.JasperReport; import net.sf.jasperreports.engine.util.JRLoader; import net.sf.jasperreports.view.JasperViewer; public class TestReport { public static void main(String[] args) { TestReport. showReport (); } private static void showReport() { String reportPath = "D:\\dailySales.jasper" ; Map parameters = new HashMap(); // 如果報(bào)表中有用到變量,在這里給它賦值. //parameters.put("ReportTitle", " 報(bào)表標(biāo)題 "); try { JasperReport jasperReport = (JasperReport) JRLoader. loadObject (reportPath); JasperPrint jasperPrint = JasperFillManager. fillReport (jasperReport, parameters, newJRBeanCollectionDataSource(getBeanCollection() ) ); JasperViewer jrview = new JasperViewer(jasperPrint); jrview.setPreferredSize( new Dimension(200,100)); jrview.setVisible( true ); } catch (JRException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } } 運(yùn)行生成的結(jié)果如圖  生成各種格式的報(bào)表文件 : public static void main(String[] args) { try { // 編譯報(bào)表,并 jrxml 所在的目錄中生成 jasper 文件 JasperCompileManager. compileReportToFile ( "d:\\test.jrxml" ); Map param = new HashMap (); JasperPrint jasperPrint = JasperFillManager. fillReport ( "d:\\test.jasper" , param, new JRBeanCollectionDataSource(DailySalesFactory. getBeanCollection ())); // 生成 html 和 pdf 也可以使用 JRExporter 來生成 JasperExportManager. exportReportToHtmlFile (jasperPrint, "d:\\test.html" ); JasperExportManager. exportReportToPdfFile (jasperPrint, "d:\\test.pdf" ); // 使用 JRExporter 來生成 XLS, 很多參數(shù)可以查 api 或 ireport 的屬性窗口 JRExporter xlsExporter = new JRXlsExporter(); ByteArrayOutputStream xlsOut = new ByteArrayOutputStream(); xlsExporter.setParameter(JRExporterParameter. JASPER_PRINT , jasperPrint); xlsExporter.setParameter(JRExporterParameter. OUTPUT_STREAM , xlsOut); xlsExporter.setParameter(JRXlsExporterParameter. IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS , Boolean. TRUE ); // 刪除記錄最下面的空行 xlsExporter.setParameter(JRXlsExporterParameter. IS_ONE_PAGE_PER_SHEET , Boolean. FALSE ); // 刪除多余的 ColumnHeader xlsExporter.setParameter(JRXlsExporterParameter. IS_WHITE_PAGE_BACKGROUND , Boolean. FALSE ); // 顯示邊框 xlsExporter.exportReport(); FileOutputStream out = new FileOutputStream( new File( "d:\\test.xls" )); out.write(xlsOut.toByteArray()); // 使用 JRExporter 來生成 html , 很多參數(shù)可以查 api 或 ireport 的屬性窗口 JRExporter htmlExporter = new JRHtmlExporter(); ByteArrayOutputStream htmlOut = new ByteArrayOutputStream(); htmlExporter.setParameter(JRHtmlExporterParameter. JASPER_PRINT , jasperPrint); htmlExporter.setParameter(JRHtmlExporterParameter. OUTPUT_STREAM , htmlOut); htmlExporter.setParameter(JRHtmlExporterParameter. CHARACTER_ENCODING , "utf-8" ); htmlExporter.setParameter(JRHtmlExporterParameter. IS_USING_IMAGES_TO_ALIGN , Boolean. FALSE ); htmlExporter.exportReport(); FileOutputStream out2 = new FileOutputStream( new File( "d:\\test1.html" )); out2.write(htmlOut.toByteArray()); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } | 
|  |