JSP 导出Excel,Word文档

Excel:
<%
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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值