天天看點

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 ,并打鈎該選項。

iReport用javabean建立資料源
iReport用javabean建立資料源

3、建立域字段,如下:

iReport用javabean建立資料源
iReport用javabean建立資料源

一、用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);

    }

}

導入工廠類

iReport用javabean建立資料源

建立Text報表,如下:

iReport用javabean建立資料源
iReport用javabean建立資料源

二、背景建立資料源如下,如下:

                           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(); 

預覽結果,如下:

iReport用javabean建立資料源
iReport用javabean建立資料源
iReport用javabean建立資料源