Java + jasperreports + ireport

Source: Internet
Author: User
Tags print object

If you encounter report problems in the project, record the main processes and problems and forget them.

1. Environment

Jdk1.5, jasperreports-1.1.1, iReport-1.1.0

2. Chinese problems

A, remember to itext-1.3.1.jar, itextasian. Jar these two packages in the iReport-1.1.0/lib directory, the two packages to see, probably can be seen is used to define the font style and other functions;

B. In ireport, set the following parameters correctly, as shown in figure

I forgot the exception information in the bottom column and thought it was a packet problem. It took a long time.

Net. SF. jasperreports. Engine. jrexception: cocould not load the following Font:
Pdffontname: stsong-light
Pdfencoding: cp1250
Ispdfembedded: True

3. Prepare reports

A, first use ireport to design the report style, variables, compiled into the. Jasper file, the existence of the Directory: F:/jasperreports-1.1.1-project/iReport-1.1.0/result.

B. Load the file in the program, fill in the data, and output the. PFD file to the directory above. The Code is as follows:

Import net. SF. jasperreports. Engine. jasperexportmanager;
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;

....................

Try ...{
Connection conn = Dao. getconnection ();
File jasperfile = new file ("F:/jasperreports-1.1.1-project/iReport-1.1.0/result/lbssreport1.jasper ");
Hashmap Param = new hashmap ();
Param. Put ("ID", "reportid"); // pass the parameter;
Param. Put ("strhql", "select loginname, lastname from t_user t ");
// Directly export the document generated during the filling process to the PDF Format
// Jasperrunmanager. runreporttopdffile ("F:/jasperreports-1.1.1-project/iReport-1.1.0/result/lbssreport1.jasper", Param, Conn );

// Standard process: generate the intermediate Print Object and export it to another format.
// Load object
Jasperreport report = (jasperreport) jrloader. loadobjectfromlocation ("F:/jasperreports-1.1.1-project/iReport-1.1.0/result/lbssreport1.jasper ");
// Fill in data
Jasperprint print = jasperfillmanager. fillreport (report, Param, Conn );
// Export to PDF (the itextasian. jar package must be included );
Jasperexportmanager. exportreporttopdffile (print, "F:/jasperreports-1.1.1-project/iReport-1.1.0/result/lbssreport1.pdf ");
} Catch (exception e )...{
Logger. Error ("report generation error:" + E. getmessage ());
Throw new exception ("report generation error:" + E. getmessage ());
}

To put the jasperreports-1.1.1.jar, itext-1.3.1.jar, itextasian. jar package under the project Lib, jasperreports contains all the relevant classes, in the program can be called directly.

In the code, Param. put ("ID", "reportid"); // pass the parameter; Param. put ("strhql", "select loginname, lastname from t_user t"); "ID" and "strhql" are external variables in the report. Corresponds to the port, as shown below:

Okay, it's almost done. Check the running PDF file.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.