Refer to the following code to add a method to the action:
1 // export Excel 2 Public String Excel () throws exception {3 stringbuffer excelbuf = new stringbuffer (); 4 excelbuf. append ("waybill number "). append ("\ t "). append ("Origin Site "). append ("\ t "). append ("Target Site "). append ("\ n"); 5 excelbuf. append ("112-00100100 "). append ("\ t "). append ("pek "). append ("\ t "). append ("Sha "). append ("\ n"); 6 excelbuf. append ("112-00100111 "). append ("\ t "). append ("PVG "). append ("\ t "). append ("xiy "). append ("\ n"); 7 string excelstring = excelbuf. tostring (); 8 excelstream = new bytearrayinputstream (excelstring. getbytes (), 0, excelstring. length (); 9 return "Excel"; 10}
It is essentially a formatted CVS text file, but all Excel/WPS can recognize this format. This method is most convenient when the exported data volume is small.
Struts2 configuration file:
1 <package name = "CBA _index"...> 2... 3 <action name = "index _ *" method = "{1}" class = "cbaaction"> 4 <result name = "success">/mu-reservation/CBA/Index. JSP </result> 5 <! -- Export Excel --> 6 <result name = "Excel" type = "stream"> 7 <Param name = "contenttype"> application/vnd. MS-Excel </param> <! -- Note contenttype --> 8 <Param name = "inputname"> excelstream </param> <! -- It must be the same as the variable name in the action --> 9 <Param name = "contentdisposition"> filename = "download.xls" </param> <! -- Download file name --> 10 <Param name = "buffersize"> 10240 </param> <! -- Download file size --> 11 </result> 12 </Action> 13... 14 </package>
Page
1 <a href = "index_excel.do" target = "_ blank"> export an Excel example </a>