赞
踩
有两种方法来实现这个功能。一种呢,是javascript直接将table导出到excel供用户下载
- 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><meta charset="utf-8" /><!--[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, filename) {
- if (!table.nodeType) table = document.getElementById(table)
- var ctx = { worksheet: name || 'Worksheet', table: table.innerHTML }
-
- document.getElementById("dlink").href = uri + base64(format(template, ctx));
- // document.getElementById("dlink").href = uri + ctx;
- document.getElementById("dlink").download = filename;
- document.getElementById("dlink").click();
- }
- })(),
不过上面这种方法不支持IE浏览器,标签<a>中哦那个的download属性是html5中的新属性,很遗憾不支持IE。需要对IE进行特别的操作。
- TableToExcelForIE: function (tableID) {
- this.tableBorder = -1; //边框类型,-1没有边框 可取1/2/3/4
- this.backGround = 0; //背景颜色:白色 可取调色板中的颜色编号 1/2/3/4....
- this.fontColor = 1; //字体颜色:黑色
- this.fontSize = 10; //字体大小
- this.fontStyle =
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。