导出到Excel功能不起作用
Somsone 建议我使用此函数将数据表导出到 Excel,但它导出的是 HTML,而不仅仅是表中的数据。如何让它仅导出数据和格式(宽度、颜色...)?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script>
function ExportHTMLTableToExcel()
{
var thisTable = document.getElementById("table").innerHTML;
window.clipboardData.setData("Text", thisTable);
var objExcel = new ActiveXObject ("Excel.Application");
objExcel.visible = true;
var objWorkbook = objExcel.Workbooks.Add;
var objWorksheet = objWorkbook.Worksheets(1);
objWorksheet.Paste;
alert('test');
}
</script>
<title>Java Friends</title>
</head>
<body>
<table id="table" style="font-weight: bold">
<tr style="background-color:red"><td>a</td><td>b</td></tr>
<tr><td>1</td><td>2</td></tr>
<tr>
<td colspan="2">
<button onclick="ExportHTMLTableToExcel()">
Get as Excel spreadsheet
</button>
</td>
</tr>
</table>
</body>
</html>
注意:此功能仅在安全选项下载未签名的 ActiveX 控件
和下载签名的 ActiveX 控件
设置为“启用”时在 IE 中起作用
Somsone have recommanded i use this function to export my data table to excel but it exports the HTML not just the data in the table. How can I make it export the data and the formatting(width, colour...) only?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script>
function ExportHTMLTableToExcel()
{
var thisTable = document.getElementById("table").innerHTML;
window.clipboardData.setData("Text", thisTable);
var objExcel = new ActiveXObject ("Excel.Application");
objExcel.visible = true;
var objWorkbook = objExcel.Workbooks.Add;
var objWorksheet = objWorkbook.Worksheets(1);
objWorksheet.Paste;
alert('test');
}
</script>
<title>Java Friends</title>
</head>
<body>
<table id="table" style="font-weight: bold">
<tr style="background-color:red"><td>a</td><td>b</td></tr>
<tr><td>1</td><td>2</td></tr>
<tr>
<td colspan="2">
<button onclick="ExportHTMLTableToExcel()">
Get as Excel spreadsheet
</button>
</td>
</tr>
</table>
</body>
</html>
Note: this function only works in IE if the security options download unsigned activex control
and download signed activex control
are set to 'enable'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请使用此代码,它对我来说工作正常
希望这有帮助。
Please use this code, it is working fine for me
Hope this help.
jQuery DataTables TableTools 插件可让您轻松地将 HTML 表格导出到 Excel,但它不会处理格式设置。
您可以在此处查看运行插件的示例: http://www.datatables.net/extras /tabletools/
它可能无法处理格式,但它确实使导出 HTML 表格变得非常容易。
如果绝对需要格式化,那么您需要考虑将数据发送回服务器,然后让服务器将 HTML 处理为 Excel。如果您使用 ASP.net,那么这实际上很容易做到。
The jQuery DataTables TableTools plug-in will let you very easily export an HTML table to Excel but it won't handle the formatting.
You can see an example of the plug-in running here: http://www.datatables.net/extras/tabletools/
It may not handle formatting but it does make exporting the HTML table super easy.
If formatting is absolutely required then you'll need to look into sending the data back to the server and then having the server process the HTML into Excel. If you're using ASP.net then it is actually quite easy to do.