Public classExportexcelservletextendsHttpServlet {/** * */ Private Static Final LongSerialversionuid = 1L; @Overrideprotected voiddoget (httpservletrequest req, HttpServletResponse resp)throwsservletexception, IOException {doPost (req, resp); } @Overrideprotected voidDoPost (httpservletrequest request, httpservletresponse response)throwsservletexception, IOException {Try{HttpSession session=request.getsession (); List<ScUser> users =NewArraylist<scuser>(); Users= (list<scuser>) session.getattribute ("Users"); OutputStream OS=Response.getoutputstream (); Response.setcontenttype ("Application/vnd.ms-excel"); Response.setcharacterencoding ("Utf-8"); Writableworkbook Book=workbook.createworkbook (OS); Writablesheet sheet= Book.createsheet ("User Management table", 0); String title[]= {"Number", "department", "User", "Password", "IP Address", "MAC Address", "status" }; for(inti = 0; i < title.length; i++) {Label Labe=NewLabel (i, 0, Title[i]); Sheet.addcell (Labe); } for(intI=0;i<users.size (); i++) {Scuser Scuser=Users.get (i); LongId=Scuser.getid (); String Dept=scuser.getdept (); String name=Scuser.getusername (); String pwd=Scuser.getpassword (); String IP=Scuser.getuserip (); String mac=Scuser.getmac (); String Status=scuser.getstatus () + ""; if(Status.equals ("F") ) {Status= "Enabled"; }Else{Status= "Disabled"; } string[] Contents={id+ "", dept,name,pwd,ip,mac,status}; for(intj=0;j<contents.length;j++) {Label Labe=NewLabel (j,i+1, Contents[j]); Sheet.addcell (Labe); }} book.write (); Book.close (); Os.flush (); } Catch(Exception e) {e.printstacktrace (); } }}
Hint: Need JXL jar package and sample program (Baidu net Disk): Link: http://pan.baidu.com/s/1dKvV4 Password: io6w
Java Export page data or database data to Excel file and download, using JXL technology, small demo (servlet implementation)