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