将一个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" onclick="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.youkuaiyun.com/feiliu010/archive/2006/08/08/1038994.aspx<URL>
很多时候需要从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" onclick="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.youkuaiyun.com/feiliu010/archive/2006/08/08/1038994.aspx<URL>