Jasper Custom Data Source
Asked by Maciek1983 in Java Programming Language,
New to Java Programming
Tags: jasper, custom, datasource, data, source
I'm a newbie to programming and I'm trying to set up a custom data source for a Jasper Report and I'm having 2 problems.:
1. PrintVendorParametersBean
public class PrintVendorLabelsParameterBean
{
public String vendorName;
public String vendorAddress;
public printVendorLabelsParameterBean(String nm,String addr)
{
this.vendorName = nm;
this.vendorAddress = addr;
}
public String getVendorAddress()
{
return vendorAddress;
}
public void setVendorAddress(String vendorAddress)
{
this.vendorAddress = vendorAddress;
}
public String getVendorName()
{
return vendorName;
}
public void setVendorName(String vendorName)
{
this.vendorName = vendorName;
}
}
2. printVendorDataSource
public class printVendorDataSource implements JRDataSource {
// hard coded, needs to use a list later, just print one Bean for now
private int counter=0;
private ArrayList list = new ArrayList();
public printVendorDataSource (ArrayList l) {
this.list = l;
}
public Object getFieldValue(JRField jrField) throws JRException {
String fieldName = jrField.getName();
Object value = "";
if(fieldName.equals("vendorName"))
{ printVendorLabelsParameterBean pvb = (printVendorLabelsParameterBean)list.get(counter);
value = pvb.getVendorName();
}
return value;
}
public boolean next() throws JRException {
counter++;
if(counter>list.size())
return false;
return true;
}
}
2. printVendorLabelsDSProvider
public class printVendorLabelsDSProviderextends JRAbstractBeanDataSourceProvider
{
public WiObjectList objlist = null;
public printVendorLabelsDSProvider(WiObjectList obj) {
super(printVendorLabelsParameterBean.class);
objlist = obj;
}
public JRDataSource create(JasperReport report) throws JRException {
ArrayList list = new ArrayList();
try
{
boolean more = objlist.open();
while(more)
{
list.add(new printVendorLabelsParameterBean(objlist.getFieldValue("vendor_name"),objlist.getFieldValue("vendor_name")));
more = objlist.next();
}
}
catch(Exception e)
{
}
return new printVendorDataSource(list);
}
public void dispose(JRDataSource dataSource) throws JRException {
// nothing to dispose
}
}
4. printVendorLabelsService
public class printVendorLabelsService
{
public void getReport(WiSession ses, WiObjectList objList,PrintWriter out)
{
printVendorLabelsBean jrp = new printVendorLabelsBean(objList);
HashMap reportParams = null;
File rptFile = null;
FileInputStream fin = null;
try
{
rptFile = new File(System.getProperty("webapp.root")+ "glap\\reports\\printVendorLabels.jasper");
JasperReport htmlReport = (JasperReport) JRLoader.loadObject(rptFile.getPath());
JasperPrint htmlPrint = null;
htmlPrint = JasperFillManager.fillReport(htmlReport,reportParams ,jrp.create(htmlReport));
JRHtmlExporter exporter = new JRHtmlExporter();
// set exporter input file and out stream
exporter.setParameter(JRExporterParameter.JASPER_PRINT, htmlPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_WRITER, out);
exporter.setParameter(JRHtmlExporterParameter.BETWEEN_PAGES_HTML, "
");
exporter.setParameter(JRHtmlExporterParameter.IS_WRAP_BREAK_WORD, Boolean.TRUE);
exporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN, Boolean.FALSE);
exporter.setParameter(JRHtmlExporterParameter.SIZE_UNIT, JRHtmlExporterParameter.SIZE_UNIT_PIXEL);
exporter.exportReport();
}
catch(Exception ex)
{
}
....nothing else relvant after
沒有留言:
張貼留言