JSP is not recommended. It is better to use servlet. I have a process here. You can use it on your own. It has been used for 2 years and no problems have been found.
General Export class, HTML, Excel, and PDF supported
Public class reporttype ...{
Public static int html = 1;
Public static int Excel = 2;
Public static final int PDF = 3;
Public static void Export (httpservletresponse response, jasperprint JP, int type, string filename) throws exception ...{
Jrexporter exporter = NULL;
If (type = html )...{
Exporter = new jrhtmlexporter ();
Exporter. setparameter (jrhtmlexporterparameter. is_using_images_to_align, false );
Exporter. setparameter (jrhtmlexporterparameter. between_pages_html ,"");
// Exporter. setparameter (jrhtmlexporterparameter. is_remove_empty_space_between_rows, Boolean. True );
} Else if (type = Excel )...{
Exporter = new jrxlsexporter ();
Response. setcontenttype ("application/vnd. MS-excel ");
Response. addheader ("content-disposition", new string ("attachment; filename =" + filename + ". xls "). getbytes ("GBK"), "ISO-8859-1 "));
Exporter. setparameter (jrxlsexporterparameter. is_one_page_per_sheet, Boolean. False );
// Exporter. setparameter (jrxlsexporterparameter. is_remove_empty_space_between_rows, Boolean. True );
} Else if (type = pdf )...{
Exporter = new jr1_exporter ();
Response. setcontenttype ("application/pdf ");
Response. addheader ("content-disposition", new string ("attachment; filename =" + filename + ". PDF "). getbytes ("GBK"), "ISO-8859-1 "));
} Else ...{
Return;
}
Exporter. setparameter (jrexporterparameter. character_encoding, "GBK ");
Exporter. setparameter (jrexporterparameter. jasper_print, JP );
Exporter. setparameter (jrexporterparameter. output_stream, response. getoutputstream ());
Exporter. exportreport ();
}
}
Usage
String Jasper = "Jasper/saler/salermonthsummary. Jasper"; // your Jasper file address
String filename = "marketing center clerk" + from + "-" + to + "sales summary"; // file name to be downloaded, such as Excel
// Set various parameters
Map map = new hashmap ();
Map. Put ("yearfrom", from. getyear ());
Map. Put ("monthfrom", from. getmonth ());
Map. Put ("yearto", to. getyear ());
Map. Put ("monthto", to. getmonth ());
// Read Jasper
Jasperreport Jr = (jasperreport) jrloader. loadobjectfromlocation (Jasper );
// Fill in data
Jasperprint JP = jasperfillmanager. fillreport (JR, MAP, null );
// Determine whether the operation is normal
List <jrprintpage> pages = JP. getpages ();
If (pages. Size () = 0 )...{
// No data
Return;
}
Reporttype. Export (response, JP, reporttype. Excel, filename );