Java servlet Exports data to an Excel file

Source: Internet
Author: User

Package Com.lbc.excel.servlet;import Java.io.ioexception;import Java.util.arraylist;import java.util.List;import Javax.servlet.servletexception;import Javax.servlet.servletoutputstream;import Javax.servlet.http.HttpServlet; Import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;import Com.lbc.entity.equipmentlib;import Com.lbc.entity.oausers;import Com.lbc.service.assetservicedao;import Com.lbc.service.assetservicedaoimpl;import youngpackage.db.youngrepository;/** * Servlet Implementation class            Exportexcelservlet */public class Exportexcelservlet extends HttpServlet {private static final long serialversionuid = 1L;        /** * @see httpservlet#httpservlet () */public Exportexcelservlet () {super (); TODO auto-generated Constructor stub}/** * @see Httpservlet#doget (httpservletrequest request, HttpServletResponse R esponse) */protected void doget (HttpServletRequest request, httpservletresponse response) throws ServletexCeption, IOException {this.dopost (request, response);} /** * @see Httpservlet#dopost (httpservletrequest request, httpservletresponse response) */protected void DoPost ( HttpServletRequest request, HttpServletResponse response) throws Servletexception, IOException {System.out.println ("-        --------------------------------exportexcelservlet----------------------------------");        Request.setcharacterencoding ("Utf-8");        Response.setcharacterencoding ("Utf-8");        Youngrepository youngdb = (youngrepository) request.getsession (). getattribute ("oa_db");        Oausers user = (oausers) request.getsession (). getattribute ("Oa_user");        String eaper = (string) request.getsession (). getattribute ("Currentusereapergrade");        if (youngdb = = null) {youngdb = new youngrepository ();        } Assetservicedao ASD = new Assetservicedaoimpl (YOUNGDB);        list<equipmentlib> list = new arraylist<equipmentlib> (); if ("EA1004". Equals (Eaper)) {list = Asd.geteLbyusernameordepartmentormanager (User.getusername (), "", "All", "1"); }else if ("EA1002". Equals (Eaper)) {list = Asd.getelbyusernameordepartmentormanager ("", User.getdepartment (), "", "1        ");  } String fileName = "excel-" + system.currenttimemillis () + ". xls";//sets the exported file name StringBuffer SB = new        StringBuffer (this.gettable (list));//Put table information in memory string contentType = "application/vnd.ms-excel";//String that defines the format of the exported file String recommendedname = new String (Filename.getbytes (), "iso_8859_1");//Set the encoding format of the file name Response.setcontenttype (cont Enttype)///Set Export file format Response.setheader ("Content-disposition", "attachment;        Filename= "+ recommendedname);//Response.resetbuffer ();        Export files with output input stream Servletoutputstream SOS = Response.getoutputstream ();        Sos.write (Sb.tostring (). GetBytes ());        Sos.flush ();    Sos.close ();}    Private String gettable (list<equipmentlib> List) {stringbuffer table = new StringBuffer (); if (LIST! = null && list.size () > 0) {table.append ("<table border= ' 1 ' ><tbody><tr style= ' height:35px ; Background-color: #fff1cc; '            ><th> Serial number </th><th> asset class </th><th> asset name </th><th> asset Count </th> "); Table.append ("<th> Department </th><th> user </th><th> Status </th><th> acquisition date </th            ><th> start using date </th> "); Table.append ("<th> service life </th><th> original value </th><th> brand </th><th> specification model </th>            <th> remarks </th></tr> ");        String temp = "";        for (int i = 0; i < list.size (); i++) {equipmentlib el = list.get (i);        if ((i+1)%2 = = 0) {table.append ("<tr style= ' height:35px; ' bgcolor= ' #EDEDED ' >"); }else{table.append ("<tr style= ' height:35px; '        > ");        } table.append ("<td align= ' center ' >" + (i+1) + "</td>"); Table.append ("<td align= ' center ' >" +el.getfamily () + "&LT;/td> ");        Table.append ("<td align= ' center ' >" +el.getname () + "</td>");        Table.append ("<td align= ' center ' >" +el.getamount () + "</td>"); Table.append ("<td align= ' center ' style= ' color:red; '        > "+el.getdepartment () +" </td> ");        Table.append ("<td align= ' center ' >" +el.getcurrentusername () + "</td>");        if ("2". Equals (El.getstatus ())) {temp = "asset transfer";        }else if ("4". Equals (El.getstatus ())) {temp = "scrap audit";        }else {temp = "normal use";        } table.append ("<td align= ' center ' >" +temp+ "</td>");        Table.append ("<td align= ' center ' >" +el.getpurchasedate () + "</td>");        Table.append ("<td align= ' center ' >" +el.getstartusedate () + "</td>");        Table.append ("<td align= ' center ' >" +el.getuselife () + "</td>");        Table.append ("<td align= ' center ' >" +el.getvalue () + "</td>"); if (el.getbrand () = = null) {table.append ("<td align= ' Center ' ></td> ');        }else{table.append ("<td align= ' center ' >" +el.getbrand () + "</td>");        } if (el.getspecifications () = = null) {table.append ("<td align= ' center ' ></td>");        }else{table.append ("<td align= ' center ' >" +el.getspecifications () + "</td>");        } if (El.getremark () = = null) {table.append ("<td align= ' center ' ></td>");        }else{table.append ("<td align= ' center ' >" +el.getremark () + "</td>");        }} table.append ("</tr></tbody></table>");    } return table.tostring (); }}

  

Java servlet Exports data to an Excel file

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.