我想将动态表导出为excel。以下代码适用于Google Chrome浏览器,但不适用于Firefox。
在firefox中,会出现一个弹出问题,即我是否要下载Excel工作表,但是我可以下载一个空的Excel工作表。此外,它说Excel工作表已损坏。
对此我是陌生的,所以请耐心等待。非常感谢。
<%@taglib uri="http://displaytag.sf.net" prefix="display"%><%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ page import="java.util.*,com.ibm.pojo.*, com.ibm.form.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<script language="javascript" type="text/javascript">
window.history.backward();
</script>
</head>
<%
List<WrongAnsQuestions> l=
(List<WrongAnsQuestions>)request.getAttribute("wrongAnsQuesList");
%>
<body>
<script type="text/javascript">
function submitThisForm(){
document.forms[0].submit();
}
</script>
<html:form action="/logOut.do">
<table align="center" id="tblData" bordercolor="red" border="1" width="700">
<%-- <tr>
<th><%=l.get(0).getExamUser()%></th></tr> --%>
<tr bgcolor="#ADD8E6">
<td align="center"><b>QUESTION STATEMENT</b></td>
<td align="center"><b>SELECTED ANSWER</b></td>
</tr>
<%
for (Iterator iterator = l.iterator(); iterator.hasNext();) {
WrongAnsQuestions r = (WrongAnsQuestions) iterator.next();
%>
<tr>
<th><%=r.getQuestion_Stmt()%></th>
<td><%=r.getSelected_Ans() %></td>
</tr>
<%
}
%>
</table>
<script>
function exportTableToExcel(tableID, filename){
var downloadLink;
var dataType = 'application/vnd.ms-excel';
var tableSelect = document.getElementById(tableID);
var tableHTML = tableSelect.outerHTML.replace(/ /g, '%20');
alert(filename);
// Specify file name
filename = filename?filename+'.xls':'excel_data.xls';
// Create download link element
downloadLink = document.createElement("a");
document.body.appendChild(downloadLink);
if(navigator.msSaveOrOpenBlob){
var blob = new Blob(['\ufeff', tableHTML], {
type: dataType
});
navigator.msSaveOrOpenBlob( blob, filename);
}else{
// Create a link to the file
downloadLink.href = 'data:' + dataType + ', ' + tableHTML;
// Setting the file name
downloadLink.download = filename;
//triggering the function
downloadLink.click();
}
}
</script>
</br>
<div align="left"><html:button property="anything" onclick="exportTableToExcel('tblData', 'User Exam Report')">Export Table Data to Excel</html:button></div>
<div align="right"><html:button property="logOut" onclick="submitThisForm();">Sign Out</html:button></div>
</html:form>
</body>
</html>