将html转为表格,将HTML表格转换为Excel,浏览器Stucks

这篇博客讨论了一段JavaScript代码,该代码用于将整个HTML页面中的表格转换为Excel文件。然而,实际需求是仅转换具有特定ID的表格,并在按钮点击事件中触发此转换。目前代码在页面加载时执行,导致浏览器卡死。博主寻求指导以修改代码,使其只针对指定的‘TestTable’表格进行Excel导出,并避免浏览器卡顿问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我已经下载了将HTML表格转换为Excel的Javascript函数。它会将页面中的所有表格转换为excel,实际上我只需要一个特定的表格即可转换为excel,也可以在Button Click中使用。但下载的JavaScript代码执行页面加载本身。有人可以指导我如何使它的功能,并且还为特定的表(给定表ID)将HTML表格转换为Excel,浏览器Stucks

NameACPOEMCPUnix

NT 3.1

Unix

NT 3.51

Unix

95

NameACPOEMCPWindows

NT 3.1

Windows

NT 3.51

Windows

95

(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 = '

{{for(var i=0; i{{=tables[i]}}{{ } }}',

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

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");

从上面的代码,我只需要“TestTable的” Excel导出。此外,当我执行这个JavaScript,浏览器中得到stucks

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值