<% @ 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 ");