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