导出为excel,csv,txt最简便的方法
1 导出为excel:
<%@ page language="java" contentType="application/vnd.ms-excel; charset=GB2312" pageEncoding="GB2312"%>
<%
response.setHeader("content-disposition", "attachment;filename="+new String(("导出文件".getBytes("GB2312"), "ISO8859_1") + ".xls");
%>
注:不能引用外部样式,如果要设置样式只有在该页设置
2 导出为CSV
<%@ page language="java" contentType="application/csv; charset=GB2312" pageEncoding="GB2312"%>
<%
response.setHeader("content-disposition", "attachment;filename="+new String(("导出文件".getBytes("GB2312"), "ISO8859_1") + ".csv");
3 导出为txt
<%@ page language="java" contentType="application/txt; charset=GB2312" pageEncoding="GB2312"%>
<%
response.setHeader("content-disposition", "attachment;filename="+new String(("导出文件".getBytes("GB2312"), "ISO8859_1") + ".txt);
在导出的时候注意会输出空行。所以在JSP2.1以下版本就把代码全都整成一行。如果2.1以上则可以用相关方法去掉空行。
在JSP引入:<%@ page trimDirectiveWhitespaces="true" %>