List data export Excel and download to local

Source: Internet
Author: User

Recently made grid list related data export to Excel function, export to Excel according to the column you choose and download to local. The download location is configured according to the configuration file. Nonsense does not say directly on the key code:

Need to introduce the relevant packages:

Compile ' net.sourceforge.jexcelapi:jxl:2.6.12 ' This is the configuration of Gradle in my project.
  @Override public void Exportexcel (integer[] ids,httpservletresponse response) {list<withdrawdeposit> L        istmsg = new arraylist<> ();        Withdrawdeposit Withdrawdeposit;            for (Integer id:ids) {withdrawdeposit = Settlementdao.getsettlementinfo (ID);        Listmsg.add (Withdrawdeposit);        } simpledateformat format = new SimpleDateFormat ("Yyyy/mm/dd"); String Dirpath = imguploadutil.excel_dir;//The configuration of the location profile exported to disk string fileName = new Date (). GetTime () + ". xls";//Generated EX        Cel file name File dir = new file (Dirpath);        if (!dir.exists ()) {dir.mkdirs (); } Dirpath = Dirpath + "/" + filename;//file path try {//*.xls for the file name to be created Writableworkbook Boo            K = Workbook.createworkbook (new File (Dirpath));            Generate a worksheet named "* * *", with parameter 0 indicating that this is the first page writablesheet ws = Book.createsheet ("Settlement request Record", 0); Write the table header Jxl.write.Label Label1 = new Jxl.write.Label (0, 0, "application number"));            Jxl.write.Label Label2 = new Jxl.write.Label (1, 0, "cash withdrawal amount");            Jxl.write.Label label3 = new Jxl.write.Label (2, 0, "Application Time");            Jxl.write.Label label4 = new Jxl.write.Label (3, 0, "Payment type");            Jxl.write.Label Label5 = new Jxl.write.Label (4, 0, "account");            Jxl.write.Label label6 = new Jxl.write.Label (5, 0, "state");            Ws.addcell (Label1);//Insert Workbook Ws.addcell (Label2);            Ws.addcell (LABEL3);            Ws.addcell (LABEL4);            Ws.addcell (LABEL5);            Ws.addcell (LABEL6); for (int i = 0; i < listmsg.size (); i++) {Label1 = new Jxl.write.Label (0, i + 1, listmsg.get (i). getappl Icationno ());//Establish the first column Label2 = new Jxl.write.Label (1, i + 1, listmsg.get (i). Getamount (). toString (). Trim ());                Establish the second column Label3 = new Jxl.write.Label (2, i + 1, format.format (Listmsg.get (i). Getoptime ()));//Build third column Label4 = new Jxl.write.Label (3, i + 1, listmsg.get (i). Getpaytype ());//establish fourth column Label5 = new Jxl.write.Label (4, i + 1, listmsg.get (i). Getaccountno ());//Build Fifth Lab El6 = new Jxl.write.Label (5, i + 1, listmsg.get (i). GetStatus ());//create sixth column//Insert Workbook Ws.addcell                (Label1);                Ws.addcell (Label2);                Ws.addcell (LABEL3);                Ws.addcell (LABEL4);                Ws.addcell (LABEL5);            Ws.addcell (LABEL6);            }//Write Data book.write ();            Close file Book.close (); SYSTEM.OUT.PRINTLN ("Export successful!!!!!!!!            ");            Download to local Servletoutputstream out = Response.getoutputstream ();            Response.setcontenttype ("Application/x-download"); Response.setheader ("Content-disposition", "attachment;filename=" + New String (Filename.getbytes ("gb2312"),            "Iso8859-1"));            byte[] bytes = new BYTE[0XFFFF];            InputStream is = new FileInputStream (new File (Dirpath)); int b;           while ((b = is.read (bytes, 0, 0xffff)) > 0) {out.write (bytes, 0, b);            } is.close ();        Out.flush ();        } catch (Exception e) {e.printstacktrace (); }    }

After execution, you will get your exported files under the disk on your local computer.

List data export Excel and download to local

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.