Java Export page data or database data to Excel file and download, using JXL technology, small demo (servlet implementation)

Source: Internet
Author: User

 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)

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.