Excel:
word:
注意的是,导出的文件命名用中文出错,对word排版最好用div,用table即使border=0在word上还是有虚框出来。
demo(直接在ie运行就可以):
<%
response.setHeader("Content-disposition","inline; filename=test.xls");
//让前端浏览器以为接收到一个excel档 ,并设定传送到前端浏览器时的档名为test.xls
或者:
response.setHeader("Content-disposition","attachment; filename=test.xls");
//attachment是以附件下载的形式,inline是以线上浏览的形式。当点击“保存”的时候都可以下载,当点击“打开”的时候attachment是在Excel里打开,inline是在浏览器里打开。
%>
头部加入:<%@ page contentType="application/msexcel; charset=gb2312" %> //不是必须,不加貌似也可以……
word:
<%
response.setHeader("Content-disposition","inline; filename=test.doc");
或者:
response.setHeader("Content-disposition","attachment; filename=test.doc");
%>
头部加入:word为<%@ page contentType="application/vnd.ms-word; charset=gb2312" %>
//不是必须,不加貌似也可以……
注意的是,导出的文件命名用中文出错,对word排版最好用div,用table即使border=0在word上还是有虚框出来。
demo(直接在ie运行就可以):
<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %>
<%
String fileName = "最简单的导出表格.xls";
fileName = new String(fileName.getBytes("GBK"),"ISO8859_1");
response.setHeader("Content-Disposition",
"attachment;filename="+fileName);
response.setDateHeader("Expires", 0);
response.setHeader("Cache-Control", "no-cache");
response.setHeader("Pragma", "no-cache");
%>
<HTML>
<HEAD>
<META http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta content="">
<TITLE>最简单的导出表格</TITLE>
</HEAD>
<BODY>
<TABLE >
<TBODY>
<TR >
<TH nowrap="nowrap" style="TEXT-ALIGN: center" >代理商ID</TH>
<TH nowrap="nowrap" style="TEXT-ALIGN: center" >代理商名称</TH>
<TH nowrap="nowrap" style="TEXT-ALIGN: center" >联系手机</TH>
</TR>
<TR height="30">
<TD align="center" nowrap="nowrap"> July</TD>
<TD align="center" nowrap="nowrap"> 男</TD>
<TD align="center" nowrap="nowrap"> 羽毛球</TD>
</TR>
<TR height="30">
<TD align="center" nowrap="nowrap"> July</TD>
<TD align="center" nowrap="nowrap"> 男</TD>
<TD align="center" nowrap="nowrap"> 羽毛球</TD>
</TR>
<TR height="30">
<TD align="center" nowrap="nowrap"> July</TD>
<TD align="center" nowrap="nowrap"> 男</TD>
<TD align="center" nowrap="nowrap"> 羽毛球</TD>
</TR>
</TBODY>
</TABLE>
</BODY>
</HTML>