相信不少朋友在實際工做中都會要將數據導出成Excel的需求,一般這樣的作法有兩種。
一是採用JXL來生成Excel,以後保存到服務器,而後在生成頁面以後下載該文件。
二是使用POI來生成Excel,以後使用Stream的方式輸出到前臺直接下載(ps:固然也能夠生成到服務器中再下載。)
。這裏咱們討論第二種。
至於兩種方式的優缺點請自行百度。java
一般我會將已經生成好的HSSFWorkbook
放到一個InputStream
中,而後再到xml配置文件中將返回結果更改成stream
的方式。以下:git
private void responseData(HSSFWorkbook wb) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); wb.write(baos); baos.flush(); byte[] aa = baos.toByteArray(); excelStream = new ByteArrayInputStream(aa, 0, aa.length); baos.close(); }
配置文件:github
<action name="exportXxx" class="xxxAction" method="exportXxx"> <result name="exportSuccess" type="stream"> <param name="inputName">excelStream</param> <param name="contentType">application/vnd.ms-excel</param> <param name="contentDisposition">attachment;filename="Undefined.xls"</param> </result> </action>
這樣便可達到點擊連接便可直接下載文件的目的。spring
先貼代碼:服務器
@RequestMapping("/exportXxx.action") public void exportXxx(HttpServletRequest request, HttpServletResponse response, @RequestParam(value="scheduleId", defaultValue="0")int scheduleId){ HSSFWorkbook wb = createExcel(scheduleId) ; try { response.setHeader("Content-Disposition", "attachment; filename=appointmentUser.xls"); response.setContentType("application/vnd.ms-excel; charset=utf-8") ; OutputStream out = response.getOutputStream() ; wb.write(out) ; out.flush(); out.close(); } catch (IOException e) { e.printStackTrace(); } }
其實springMVC和Struts2的原理上是同樣的,只是Struts2是纔去配置文件的方式。首先是使用createExcel()
這個方法來生成Excel並返回,最後利用rresponse
便可向前臺輸出Excel,這種方法是通用的,也能夠試用與Servlet、Struts2等
。咱們只須要在response
的頭信息中設置相應的輸出信息便可。app
無論是使用Struts2
,仍是使用SpringMVC
究其根本都是使用的response
,因此只要咱們把response
理解透了無論是下載圖片、world、Excel仍是其餘什麼文件都是同樣的。spa
相信不少朋友在實際工做中都會要將數據導出成Excel的需求,一般這樣的作法有兩種。
一是採用JXL來生成Excel,以後保存到服務器,而後在生成頁面以後下載該文件。
二是使用POI來生成Excel,以後使用Stream的方式輸出到前臺直接下載(ps:固然也能夠生成到服務器中再下載。)
。這裏咱們討論第二種。
至於兩種方式的優缺點請自行百度。excel
一般我會將已經生成好的HSSFWorkbook
放到一個InputStream
中,而後再到xml配置文件中將返回結果更改成stream
的方式。以下:code
private void responseData(HSSFWorkbook wb) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); wb.write(baos); baos.flush(); byte[] aa = baos.toByteArray(); excelStream = new ByteArrayInputStream(aa, 0, aa.length); baos.close(); }
配置文件:xml
<action name="exportXxx" class="xxxAction" method="exportXxx"> <result name="exportSuccess" type="stream"> <param name="inputName">excelStream</param> <param name="contentType">application/vnd.ms-excel</param> <param name="contentDisposition">attachment;filename="Undefined.xls"</param> </result> </action>
這樣便可達到點擊連接便可直接下載文件的目的。
先貼代碼:
@RequestMapping("/exportXxx.action") public void exportXxx(HttpServletRequest request, HttpServletResponse response, @RequestParam(value="scheduleId", defaultValue="0")int scheduleId){ HSSFWorkbook wb = createExcel(scheduleId) ; try { response.setHeader("Content-Disposition", "attachment; filename=appointmentUser.xls"); response.setContentType("application/vnd.ms-excel; charset=utf-8") ; OutputStream out = response.getOutputStream() ; wb.write(out) ; out.flush(); out.close(); } catch (IOException e) { e.printStackTrace(); } }
其實springMVC和Struts2的原理上是同樣的,只是Struts2是纔去配置文件的方式。首先是使用createExcel()
這個方法來生成Excel並返回,最後利用rresponse
便可向前臺輸出Excel,這種方法是通用的,也能夠試用與Servlet、Struts2等
。咱們只須要在response
的頭信息中設置相應的輸出信息便可。
無論是使用Struts2
,仍是使用SpringMVC
究其根本都是使用的response
,因此只要咱們把response
理解透了無論是下載圖片、world、Excel仍是其餘什麼文件都是同樣的。
我的博客地址:http://crossoverjie.top。
GitHub地址:https://github.com/crossoverJie。
簡書地址:簡書