To export an Excel file, you only need to point to this page and set several parameters to export it.

Source: Internet
Author: User
<% @ Page contenttype = "application/vnd. MS-Excel; charset = GBK "%> <% @ page import =" com. isoftstone. util. poi. * "%> <% @ page import =" Java. util. * "%> <% @ page import =" org. apache. poi. hssf. usermodel. * "%> <% @ page import =" Java. io. * "%> <% excelparameterinterface excelparam = (excelparameterinterface) request. getsession (). getattribute ("excelparam"); If (excelparam = NULL) {Throw new runtimeexception ("Operation error. ");} response. reset (); response. setcontenttype ("application/vnd. MS-Excel; charset = GBK "); string filename = excelparam. getfilename (); response. setheader ("content-disposition", "attachment; filename =" + filename + ". xls "); outputstream httpout = response. getoutputstream (); List datatitle = excelparam. getdatahead (); List datadetail = excelparam. getdatadetail (); try {hssfworkbook WB = excelsimple. createworkbook (); excelsimple = new excelsimple (); excelsimple. insertrowsonesheel (WB, datatitle, datadetail); WB. write (httpout); httpout. close ();} catch (exception ex) {response. setcontenttype ("text/html"); out. println ("<HTML>"); out. println ("

The above is the code in JSP
 

|

|

|

|

Below is the code in action

List resultlist = new vector (); // result set. The element in the result set is a row of list titlelist = new vector (); // header row // point to the specified page to export excelparameterinterface excelparam = new excelparam ("exportreport", titlelist, resultlist); Request. getsession (). setattribute ("excelparam", excelparam); Request. setattribute ("reportList", reportList); response. sendredirect (request. getcontextpath () + "/excelexport. JSP "); Return Mapping. findforward ("");} else {request. setattribute ("wrongprompt1", "data that does not meet the conditions"); // null, returned to the page display} Return Mapping. findforward ("success ");

 

Related Article

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.