jsp导出excel的方法
在Web应用中,很多数据经常要导出成Excel文档。在要求不高的情况,通过简单的html设置就可以实现将页面数据导出为excel。主要原理是通过html来表示excel数据,然后用excel软件打开。
方式一:
(1) 能够导出为excel的jsp页面中最好删除表单元素,值留下纯数据和纯html代码。这样导出的excel格式等相对比较自然。如果有表单元素的话,导出的excel里也会是表单元素形式,如按钮等。
(2) 在该JSP页面头部设置response的ContentType为Excel格式
<% response.setContentType("application/vnd.ms-excel;charset=GBK"); %>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
response.setHeader("Content-disposition","attachment; filename=jclxsp.xls");
<head xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel"><meta http-equiv="Content-Type" content="text/html; charset=GBK"><meta name=ProgId content=Excel.Sheet><meta name=Generator content="Microsoft Excel 11"><link rel=File-List href="Book1.files/filelist.xml"><link rel=Edit-Time-Data href="Book1.files/editdata.mso"><link rel=OLE-Object-Data href="Book1.files/oledata.mso">
response.getWriter().write("<script>window.close();</script>");