gpt4 book ai didi

javascript - HTML 表格转 Excel,浏览器卡住

转载 作者:行者123 更新时间:2023-12-02 18:30:25 25 4
gpt4 key购买 nike

我已经下载了用于将 HTML 表格转换为 Excel 的 Javascript 函数。它将把页面中的所有表格转换为Excel,实际上我只需要将一个特定的表格转换为Excel,该表格也在按钮单击中。但下载的 javascript 代码执行页面加载本身。有人可以指导我如何将其作为函数以及特定表(给定表 ID)

    <table id="testTable">
<thead>
<tr>
<th>Name</th>
<th>ACP</th>
<th>OEMCP</th>
<th>Unix<br>
NT 3.1</th>
<th>Unix<br>
NT 3.51</th>
<th>Unix<br>
95</th>
</tr>
</thead>
</table>
<table id="testTable1">
<thead>
<tr>
<th>Name</th>
<th>ACP</th>
<th>OEMCP</th>
<th>Windows<br>
NT 3.1</th>
<th>Windows<br>
NT 3.51</th>
<th>Windows<br>
95</th>
</tr>
</thead>
</table>
<script type="text/javascript">
(function(){
var cache = {};

this.tmpl = function tmpl(str, data){
// Figure out if we're getting a template, or if we need to
// load the template - and be sure to cache the result.
var fn = !/\W/.test(str) ?
cache[str] = cache[str] ||
tmpl(document.getElementById(str).innerHTML) :

// Generate a reusable function that will serve as a template
// generator (and which will be cached).
new Function("obj",
"var p=[],print=function(){p.push.apply(p,arguments);};" +

// Introduce the data as local variables using with(){}
"with(obj){p.push('" +

// Convert the template into pure JavaScript
str.replace(/[\r\t\n]/g, " ")
.split("{{").join("\t")
.replace(/((^|}})[^\t]*)'/g, "$1\r")
.replace(/\t=(.*?)}}/g, "',$1,'")
.split("\t").join("');")
.split("}}").join("p.push('")
.split("\r").join("\\'")
+ "');}return p.join('');");

// Provide some basic currying to the user
return data ? fn( data ) : fn;
};
})();

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>{{for(var i=0; i<tables.length;i++){ }}<table>{{=tables[i]}}</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(tableList, name) {
if (!tableList.length>0 && !tableList[0].nodeType) table = document.getElementById(table)
var tables = [];
for(var i=0; i<tableList.length; i++){
tables.push(tableList[i].innerHTML);
}
var ctx = {worksheet: name || 'Worksheet', tables: tables};
window.location.href = uri + base64(tmpl(template, ctx))
}
})();
tableToExcel(document.getElementsByTagName("table"),"one");
</script>

从上面的代码中,我只需要“testTable”excel 导出。另外,当我执行此 JavaScript 时,浏览器会卡住

最佳答案

tableToExcel(document.getElementsByTagName("table"),"one");

这是您的关键行!

您为其提供表格列表和新工作表的名称。

如果您只想要一个:

function expTable(tbl)
{
var tables = [];
tables.push(tbl);
tableToExcel(tables,"myTableExcelWorkbook");
}

使用expTable(document.getElementById(tableId));运行它

关于javascript - HTML 表格转 Excel,浏览器卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17854415/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com