您好,欢迎访问一九零五行业门户网

js中table数据导出excel文件

本文主要和大家分享一些代码,主要和大家分享js中table数据导出excel文件的方法实例,希望能帮助到大家。
表格转换成excel并下载
(document).ready(function () { (“#mybtn”).click(function () { //点击下载按钮,执行方法 createexcel(“mytable”,”test”); }); }); //将table导出到excel var idtmr; function getexplorer() { //返回浏览器类型 var explorer = window.navigator.useragent; //ie if (explorer.indexof(“msie”) >= 0) { return ‘ie’; } //firefox else if (explorer.indexof(“firefox”) >= 0) { return ‘firefox’; } //chrome else if (explorer.indexof(“chrome”) >= 0) { return ‘chrome’; } //opera else if (explorer.indexof(“opera”) >= 0) { return ‘opera’; } //safari else if (explorer.indexof(“safari”) >= 0) { return ‘safari’; } } function createexcel(tableid,filename) { if (getexplorer() == ‘ie’) { var curtbl = document.getelementbyid(tableid); var oxl = new activexobject(“excel.application”); var owb = oxl.workbooks.add(); var xlsheet = owb.worksheets(1); var sel = document.body.createtextrange(); sel.movetoelementtext(curtbl); sel.select(); sel.execcommand(“copy”); xlsheet.paste(); oxl.visible = true; try { var fname = oxl.application.getsaveasfilename(filename + “.xls”, //文件名和文件格式 但尝试改了一下filename 这里并不影响 “excel spreadsheets (.xls), .xls”); } catch (e) { print(“nested catch caught ” + e); } finally { owb.saveas(fname); owb.close(savechanges = false); oxl.quit(); oxl = null; idtmr = window.setinterval(“cleanup();”, 1); } } else { tabletoexcel(tableid,filename) //调用tabletoexcex table的id 和 要生成的文件名 } } function cleanup() { window.clearinterval(idtmr); collectgarbage(); } var tabletoexcel = (function () { var uri = ‘data:application/vnd.ms-excel;base64,’, template = ‘
{table}
’, 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)) a = document.createelement(“a”); a.download = name; a.href = uri + base64(format(template, ctx)); document.body.appendchild(a); a.click(); document.body.removechild(a); } })()
下载/download excel
上下文表格布局产品付款日期状态
产品1 23/11/2013 待发货
产品2 10/11/2013 发货中
产品3 20/10/2013 待确认
产品4 20/10/2013 已退货
相关推荐:
js导出excel表格超出26位英文字符的解决方法es6
php使用原生的方法导出excel实例分享
php实现导出excel数据的类库用法示例
以上就是js中table数据导出excel文件的详细内容。
其它类似信息

推荐信息