How to Use ireport and Jasper to generate reports (SQL parameters and Chinese file names)

Source: Internet
Author: User

1. First download the required tools and racks. I will not talk about it here.

2. Here we will only introduce several difficulties.

1) after creating the jrxml file with ireport, find the corresponding Jasper file in the installation directory of ireport and put it in a directory of the project. (Since it is a waste of time for the program to compile jrxml every time, it is not easy to change the template. We recommend that you compile jrxml only once)

2) use the following code:

Session session = hibernateutil. currentsession ();
Connection con = session. Connection (); // get the link
Statement stmt = con. createstatement ();
Resultset rs = stmt.exe cutequery ("select * From applications_list where app_id = '" + appid + "'"); // use a dataset to input an SQL statement with Parameters
System. Out. println ("select * From applications_list where appl_list_id = '" + appid + "'");
String apppath = request. getsession (). getservletcontext (). getrealpath ("/"); // obtain the Jasper file for convenience.
If (apppath. lastindexof ("//")! = Apppath. Length ()-1) apppath + = "//";
String reportfilename = apppath + "Jasper // untitled_report_1.jasper ";
Map parameters = new hashmap ();
Parameters. Put ("title", "Device Application List"); // you can specify multiple parameters.
Jasperprint = jasperfillmanager. fillreport (reportfilename, parameters, new jrresultsetdatasource (RS ));
Jrrtfexporter exporter = NULL;
Exporter = new jrrtfexporter ();
Response. setcontenttype ("text/html; charset = UTF-8 ");
Response. setcontenttype ("application/MSWord ");
String applicatiosname = toutf8.toutf8string ("Device Application Form"); // process Chinese characters of downloaded file names
Response. setheader ("content-disposition", "attachment; filename =" + applicatiosname + ". Doc ");
Exporter. setparameter (jrexporterparameter. character_encoding, "UTF-8 ");
Exporter. setparameter (jrexporterparameter. jasper_print, jasperprint );
Exporter. setparameter (jrexporterparameter. output_stream, response. getoutputstream ());
Exporter. exportreport ();

 

 

 

* Returns the character encoding UTF-8 */
Public static string toutf8string (string s ){
Stringbuffer sb = new stringbuffer ();
For (INT I = 0; I <S. Length (); I ++ ){
Char c = S. charat (I );
If (C> = 0 & C <= 255 ){
SB. append (C );
} Else {
Byte [] B;
Try {
B = character. tostring (c). getbytes ("UTF-8 ");
} Catch (exception ex ){
// System. Out. println (Ex );
B = new byte [0];
}
For (Int J = 0; j <B. length; j ++ ){
Int K = B [J];
If (k <0)
K + = 256;
SB. append ("%" + integer. tohexstring (k). touppercase ());
}
}
}
Return sb. tostring ();
}

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.