发布网友 发布时间:2022-04-23 22:55
共2个回答
热心网友 时间:2022-04-27 21:24
这个是我用的时候查到的代码,我放到jsp页面了,你看一下,挺好懂的!
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'testTable.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript">
function table2xls()
{ //重写String的Trim()方法
function String.prototype.Trim() {
return this.replace(/(^\s*)|(\s*$)/g,"");
}
try
{
var e=document.getElementById("myTable");
var s="";
for(var j=0;j<e.rows.length;j++)
{
if(e.rows[0].cells[0].innerText.Trim()=="")
{
for(var i=1;i<e.rows[j].cells.length;i++)
s+=e.rows[j].cells[i].innerText.Trim()+"\t";
}
else
{
for(var i=0;i<e.rows[j].cells.length;i++)
s+=e.rows[j].cells[i].innerText.Trim()+"\t";
}
s+="\r\n";
}
//var xlsWindow =window.open("","_blank","width=100,height=100");
// xlsWindow.document.write(s);
// xlsWindow.document.close();
//xlsWindow.document.execCommand('Saveas',true,'%homeDrive%\\Data.xls')
//xlsWindow.close();
document.execCommand('Saveas',true,'%homeDrive%\\Data.xls');
}catch(e){}
}
</script>
</head>
<body>
<table name="myTable" id="myTable">
<tr>
<td>12</td>
<td>aa</td>
</tr>
<tr>
<td>ww
</td>
<td>bb</td>
</tr>
</table>
<input type="button" value="导出到Excel" onclick="table2xls();">
</body>
</html>
热心网友 时间:2022-04-27 22:42
保存成:
行1列1, 行1列2
行2列2, 行2列2
的形式,在将文件的扩展名改为.csv就可以了