Convert the created Excel document to a stream that requires output: either the file is banished to the hard disk or the output stream is output to the browser for download. File stream: FileOutputStream
1 New FileOutputStream ("F://workbook.xls"); Workbook.write (FOS); 3 Fos.close ();
Output stream: Response.getoutputstream ()
Response.reset (); 2 response.setcontenttype ("APPLICATION/VND.MS-EXCEL;CHARSET=GBK"); 3 response.setheader ("Content-disposition", "Attachment;filename=account.xls"); 4 OutputStream out = response.getoutputstream (), workbook.write (out);
Tip:
*response.reset (); Clear the header blank line
* GetResponse's Getwriter () method outputs the stream to the page twice in a row, and the second stream includes the first stream,
* So you can use the Response.reset or Resetbuffer method.
* The difference between the Resetbuffer method and the Reset method is that the header and status code are not cleared.
If you find that the browser side does not pop up "save file as" to select the path window after this setting, check whether the preceding code appears:
1.response.setcontenttype ("Text/html;charset=utf-8");//Set encoding format
2.PrintWriter out = Response.getwriter (), resulting in an inability to determine the output stream
3, Excel file name is Chinese when garbled or unknown file type error, consider using Urlencoder to transcode the file name
1 String name = Java.net.URLEncoder.encode (FileName, "Utf-8"); 2 response.setcontenttype ("Application/vnd.ms-excel;charset=utf-8"); 3 response.setheader ("Content-disposition", "Attachment;filename=" +name.tostring () + ". xls"4 outputstream out = response.getoutputstream (), workbook.write (out);
JSP page Download file