jsp导出word等

将一个jsp中的表格导出到excel/word
很多时候需要从jsp(或xslt的查询结果)导出到excel或word,一个简单的方法是采用jsp实现,具体实现方式(以导出到excel为例)
first.jsp(此jsp用来显示查询结果,上面有一个按钮,点击下载):
<script language="javascript">
function doExport(){
document.all.form1.action="export.jsp";
var str = document.getElementById("table1").outerHTML;
document.all.excelText.value= str;
document.all.form1.submit();
}
<script>
<form name="form1" method="post" action="">
<input type="hidden" name="excelText" id="excelText" >
<input name="exportBtn" type="button" οnclick="doExport()" class="button" value="导出">
</form>

<table width="100%" id="table1" border="1" cellpadding="2" cellspacing="1" bordercolordark="#FFFFFF">
.............................这里面是具体需要导出去的数据
</table>
export.jsp(执行导出操作)
<%@page contentType="text/html;charset=GB2312"%>
<%
String fileName="fileName";//随便定义,也可不定义
response.setContentType( "Application;charset=GB2312");
response.setHeader("Content-disposition","attachment;filename=\"" + fileName + "\";");
java.io.PrintWriter bos = response.getWriter();
String html = request.getParameter("excelText");
bos.write(html);
bos.close();
%>
到此,文件导出操作完成,点击“导出”按钮即可出现保存对话框。
在做的过程中偶然碰到了一个问题,点击一次导出,可以顺利保存,点击第二次时出现脚本错误。
解决方法是在doExport()方法中指定document.all.form1.target = "_blank";这样可以顺利保存,但打开了一个新窗口。
<URL>转于http://blog.csdn.net/feiliu010/archive/2006/08/08/1038994.aspx<URL>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值