Java write simple Excel first line is the directory and then the foreground download

Source: Internet
Author: User

Page: <form action= "${path}/xxx/xxx.do"  method= "get" >          form Download: <input type= "Submit" value= "Download" >      </form>

  

@RequestMapping ("/download") public void Download (HttpServletRequest request, httpservletresponse response, HttpSession session) throws Exception {arraylist<string> ListTitle = new arraylist<string> (); Listtitle.add ("a"); Listtitle.add ("B"); Listtitle.add ("C"); Listtitle.add ("D"); Listtitle.add ("E");//CREATE TABLE and its first row title bar Hssfworkbook WB = new Hssfworkbook (); Hssfsheet sheet = wb.createsheet ("Sheet1"); Hssfrow row0 = sheet.createrow (0); for (int j = 0; J < 5; J + +) {//5 columns Hssfcell cell = Row0.createcell ((short) j); Cell.set Cellvalue (Listtitle.get (j). ToString ());} String maker = Systemutil.getcureenuser (session); list<mainfestitem> list = Transportservice.getlist (maker); System.out.println (List.size ());//Enter for in the second line for (int i = 1; I <= list.size (); i++) {//a few rows hssfrow row = Sheet.createrow (i) ; Hssfcell cell0 = Row.createcell ((short) 0);//N+1 Cell0.setcellvalue (List.get (i-1). GetId (). toString ()); Hssfcell cell1 = Row.createcell ((short) 1); Cell1.setcellvalue (List.get (i-1). Getcontainerid ().ToString ()); Hssfcell cell2 = Row.createcell ((short) 2), Cell2.setcellvalue (List.get (i-1). Getisocode (). toString ());} Bytearrayoutputstream OS = new Bytearrayoutputstream (); wb.write (OS); byte[] content = Os.tobytearray (); InputStream is = New Bytearrayinputstream (content);//Set response parameter, you can open the download page response.reset (); Response.setcontenttype ("application/ Vnd.ms-excel;charset=utf-8 "); Response.setheader (" Content-disposition "," attachment;filename= "+ New String (" box "+ ". xls"). GetBytes (), "iso-8859-1")); Servletoutputstream out = Response.getoutputstream (); Bufferedinputstream bis = null; Bufferedoutputstream bos = null;try {bis = new Bufferedinputstream (IS), BOS = new Bufferedoutputstream (out); byte[] buff = n EW byte[2048];int bytesread;//Simple read/write loop.while ( -1! = (bytesread = bis.read (buff, 0, buff.length))) {Bos.writ E (Buff, 0, bytesread);}} catch (Exception e) {//Todo:handle exceptione.printstacktrace ();} finally {if (bis! = null) bis.close (); if (bos! = NULL) b Os.close ();}}

  

Java write simple Excel first line is the directory and then the foreground download

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.