1. 程式人生 > >iReport用javabean建立資料來源

iReport用javabean建立資料來源

1、建立一個javabean物件

import java.io.Serializable;
public class GoodPrint implements Serializable{
/**

*/
private static final long serialVersionUID = 1L;
private String goodName;
private String goodNo;
private String barcode;
private java.math.BigDecimal quantity;
private String quantityUnit;
public GoodPrint(){
}
public GoodPrint(String goodName,String goodNo,String barcode,java.math.BigDecimal quantity,String quantityUnit){
this.goodName=goodName;
this.goodNo=goodNo;
this.barcode=barcode;
this.quantity=quantity;
this.quantityUnit=quantityUnit;
}
public String getGoodName() {
return goodName;
}
public void setGoodName(String goodName) {
this.goodName = goodName;
}
public String getGoodNo() {
return goodNo;
}
public void setGoodNo(String goodNo) {
this.goodNo = goodNo;
}
public String getBarcode() {
return barcode;
}
public void setBarcode(String barcode) {
this.barcode = barcode;
}
public java.math.BigDecimal getQuantity() {
return quantity;
}
public void setQuantity(java.math.BigDecimal quantity) {
this.quantity = quantity;
}
public String getQuantityUnit() {
return quantityUnit;
}
public void setQuantityUnit(String quantityUnit) {
this.quantityUnit = quantityUnit;

}


2、工具--》選項--》classpath,匯入\WebContent\WEB-INF\classes ,並打鉤該選項。



3、建立域欄位,如下:


一、用ireport建立資料來源,如下:

建立一個工廠類,如下:

public class GoodsFactory {
private static GoodPrint[] data={new GoodPrint("GOO1","A","GOO1A",new BigDecimal(10),"M"),
new GoodPrint("GOO2","B","GOO2B",new BigDecimal(20),"PCS")}; 
public static Object[] getBeanArray() {
     return data;
}
public static Collection<?> getBeanCollection() {
        return Arrays.asList(data);
    }
}

匯入工廠類


建立Text報表,如下:



二、後臺建立資料來源如下,如下:

                           String root_path=request.getRootPath();
  String reportFilePath = root_path + "report/Test.jasper";
  List goodList=new ArrayList<Goods>();
  goodList.add(new GoodPrint("GOO1","A","GOO1A",new BigDecimal(10),"M"));
  goodList.add(new GoodPrint("GOO2","B","GOO2B",new BigDecimal(20),"PCS"));
//  JRDataSource dataSource = this.createDataSource();
  JRDataSource dataSource = new JRBeanCollectionDataSource(goodList);;
//  Map<String, Object> parameters = new HashMap<String, Object>();
//  List dataList =new ArrayList();
//  parameters.put("dataList", dataList);
  JasperReport report = (JasperReport)JRLoader.loadObject(reportFilePath);
  JasperPrint jasperPrint = JasperFillManager.fillReport(report, null, dataSource);
  String repTpl="123";
  JasperExportManager.exportReportToPdfFile(jasperPrint,root_path+"report/123.pdf");
//JasperRunManager.runReportToHtmlFile(root_path+"report/123.pdf",null,null);
//如果建立報表成功,則轉向該報表,其實可以把報表套在框架內,這樣實現比較有意義的報表格式。
response.sendRedirect("report2.html");
//  JasperFillManager.fillReport(report, parameters, dataSource);
//OutputStream ouputStream = response.getOutputStream();  
//response.setContentType("application/pdf");
//response.setCharacterEncoding("UTF-8");  
//String repName=URLEncoder.encode("物料報表", "UTF-8");
//response.setHeader("Content-Disposition", "attachment; filename=\""+repName+"\".pdf"); 
//             
//        // 使用JRPdfExproter匯出器匯出pdf  
//        JRPdfExporter exporter = new JRPdfExporter();  
//        exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
//        exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, ouputStream);  
//        exporter.exportReport();
//ouputStream.close(); 

預覽結果,如下: