ireport backstage pass parameters to print pdf

First; start with mass participation mode, a total of two

JasperRunManager.runReportToPdfStream (IS, ServletOutputStream, map, DATA2); 

// . 1; the above parameter map, corresponding to the pdf parameters Parmeter 

// 2; Datasouce, the fourth parameter corresponding pdf parameters 2, fields 

// the Map is similar to the official explanation sql query conditions, DataSource is a specific query results data

 

 

Second; the form of specific data parameters

map, is an ordinary hashmap can

// . 1; Map, is the normal can hashmap 
the HashMap Map = new new the HashMap (); 

// parameter passing in a corresponding manner 
Map ( "", XXX)
 // XXX may be a database, may be List, may be a single data 

// database
 // to datasouce incoming type, can be used when acquiring 
use 
in ireport assembly in list 
$ {P} REPORT_PARAMETERS_MAP .get ( "XXX" ) directly acquired
 // expression filled, Figure IV 

list 
// incoming list, cycle time convenient to use format conversion datasouce corresponding
 // to create a list field, and then change the field attributes, Figure III is the first I chose 
to use a way to 
use ireport component list, due to creation when the list, automatically comes subDataSource 
it was added in the appropriate fields subDataSource field in which the incoming 
// the following is the incoming list <map> type 
newnet.sf.jasperreports.engine.data.JRMapCollectionDataSource (List $ {P})
 // expression filled, Figure IV 
use two 
self-loop uses 


// single data
 // added directly to the corresponding elements in the Parameter, page use $ using P {}

 

datasouce

@ 2; the DataSource, database type
 // depending on the data type of transmission parameters are primarily used in several forms
     @ A; This is the list <bean> can be used 
    List <Bean> List = null ; 
    instance of JRDataSource Data = new new the JRBeanCollectionDataSource (List); 

    // B; this is the Map [] can be used, this data map [0] is mainly used in the fields 2 map of 
    the HashMap [] = reportRows new new the HashMap [. 1 ]; 
    the HashMap Map = new new the HashMap (); 
    reportRows [ 0] = Map;
     // this is the Map [] may be used 
    instance of JRDataSource DATA2 = new new JRMapArrayDataSource (reportRows); 
    
    // C; this is the list [Map] may be used
    List<Map<>> list = null;
    JRDataSource data2 = new JRMapCollectionDataSource(reportRows);

 

 Third, the business code

the try {
     // obtain an output stream of 
    the ServletOutputStream ServletOutputStream = response.getOutputStream ();
     // file name Location 
    String pathName = "/report/reportallotment.jasper" ;
     // Get Resource 
    the Resource Resource = new new a ClassPathResource (pathName);
     // Get File the input stream 
    the InputStream iS = resource.getInputStream ();
     // put parameters 
    JasperRunManager.runReportToPdfStream (iS, ServletOutputStream, Map, DATA2); 
    
    the response.setContentType ( "file application / PDF" ); 
    response.setHeader ( "Content-Disposition" , "attachment; filename = report.pdf");
    servletOutputStream.flush();
    servletOutputStream.close();
    return null;
} catch (Exception e) {
    StringWriter stringWriter = new StringWriter();
    PrintWriter printWriter = new PrintWriter(stringWriter);
    e.printStackTrace(printWriter);
    response.setContentType("text/plain");
    try {
        response.getOutputStream().print(stringWriter.toString());
    } catch (IOException e1) {
        e1.printStackTrace();
    }
}
return null;

Figure I

Figure II

dataset2 no real meaning in the printing process, it has all of the fields can be directly written in the 2 fields,

If you need to report child

Figure III

I figure here list of expressions

Figure IV

 

Note, get () method does not get the results dataset2 database, but I added this parameter passed in the background the map

JRDataSource data = new JRBeanCollectionDataSource(list);map.put("dataset2", data);

bean in list corresponding to the fields dataset2

Guess you like

Origin www.cnblogs.com/ccnewsky/p/11417600.html