【发布时间】:2015-09-29 08:11:37
【问题描述】:
我在导出包含大量行数的 HTML 表格时遇到问题。正如我在标题中指出的那样,它有 3000 多行和大约 6 列。
这个我试过了,How to export html table to excel using javascript
工作正常!好吧,至少在小桌子上。但是当我将它与要转换的表绑定时,它会加载一段时间然后停止。
有人可以帮我吗?
PS,我不想使用 ActiveXObject,因为我不想仅限于使用 IE。
提前致谢!
编辑:代码
var tableToExcel = (function() {
var uri = 'data:application/vnd.ms-excel;base64,'
, template = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"><head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body><table>{table}</table></body></html>'
, base64 = function(s) { return window.btoa(unescape(encodeURIComponent(s))) }
, format = function(s, c) { return s.replace(/{(\w+)}/g, function(m, p) { return c[p]; }) }
return function(table, name) {
if (!table.nodeType) table = document.getElementById(table)
var ctx = {worksheet: name || 'Worksheet', table: table.innerHTML}
window.location.href = uri + base64(format(template, ctx))
}
})()
适用于 IE 和 Firefox,但不适用于 chrome。这是为什么呢?
【问题讨论】:
-
@JunchaoGu,我没有这样的东西。他有一个弹出式“下载”,但不是 XLS 格式,但我没有这样的。
-
@GergelyPolonkai,我试图避免使用 ActiveXObject,因为它限制我的用户只能将 IE 用于他的浏览器。
-
您应该重新考虑您的要求。表是如何生成的?服务器端?然后使用服务器端生成 xls。 在 url 上传递工作表总是会在某些时候失败,并且取决于浏览器。阅读本文了解更多信息:What is the maximum length of a URL in different browsers? (SO)
-
当你说“它加载一段时间然后停止” - 你的意思是它构建数据一段时间然后 脚本停止 或者脚本工作正常但是 不是全部加载到excel中?
标签: javascript html excel