Code for converting the Jasper file to report output

Source: Internet
Author: User
Tags export class

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

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.