首页 > 代码库 > jsexcel导出插件

jsexcel导出插件

 

ExcelTable.js

技术分享
 1 /*
 2  * author:wenluanlai
 3  */
 4 (function ($) {
 5    Date.prototype.Format = function (fmt) {
 6       var o = {
 7          "M+": this.getMonth() + 1, //月份 
 8          "d+": this.getDate(), //
 9          "h+": this.getHours(), //小时 
10          "m+": this.getMinutes(), //
11          "s+": this.getSeconds(), //
12          "q+": Math.floor((this.getMonth() + 3) / 3), //季度 
13          "S": this.getMilliseconds() //毫秒 
14       };
15       if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
16       for (var k in o)
17          if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
18       return fmt;
19    }        
20 
21    $.fn.ExportExcel = function (tab_id, options) {
22       var defaults = {
23          height: ‘24px‘,
24          ‘line-height‘: ‘24px‘,
25          margin: ‘0 5px‘,
26          padding: ‘0 11px‘,
27          color: ‘#000‘,
28          background: ‘#02bafa‘,
29          border: ‘1px #26bbdb solid‘,
30          ‘border-radius‘: ‘3px‘,
31          /*color: #fff;*/
32          display: ‘inline-block‘,
33          ‘text-decoration‘: ‘none‘,
34          ‘font-size‘: ‘12px‘,
35          outline: ‘none‘,
36          cursor: ‘pointer‘
37       }
38       var options = $.extend(defaults, options);
39       return this.each(function () {
40          var currentObject = $(this); //获取当前对象 
41          currentObject.css(defaults);
42          currentObject.onmouseover = function () {
43             $(this).css(‘cursor‘, ‘hand‘);
44          };
45 
46          currentObject.click(function () {
47             //From:jsfiddle.net/h42y4ke2/16/
48             var tab_text = ‘<html xmlns:x="urn:schemas-microsoft-com:office:excel">‘;
49             tab_text = tab_text + ‘<head><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet>‘;
50 
51             tab_text = tab_text + ‘<x:Name>Test Sheet</x:Name>‘;
52 
53             tab_text = tab_text + ‘<x:WorksheetOptions><x:Panes></x:Panes></x:WorksheetOptions></x:ExcelWorksheet>‘;
54             tab_text = tab_text + ‘</x:ExcelWorksheets></x:ExcelWorkbook></xml></head><body>‘;
55 
56             tab_text = tab_text + "<table border=‘1px‘>";
57             tab_text = tab_text + $(‘#‘ + tab_id).html();
58             tab_text = tab_text + ‘</table></body></html>‘;
59 
60             var data_type = ‘data:application/vnd.ms-excel‘;
61 
62             var timeStr = new Date().Format(‘yyyyMMddhhmmss‘);
63             $(this).attr(‘href‘, data_type + ‘, ‘ + encodeURIComponent(tab_text));
64             $(this).attr(‘download‘, ‘数据报表‘ + timeStr + ‘.xls‘);
65          });
66       })
67    }
68 })(jQuery);
View Code

 

 

非常便捷的一种excel导出插件,不用配置复杂的参数。

 js调用

技术分享
1 <script type="text/javascript">
2 
3    $(function () {
4       $(‘#downloadExcel‘).ExportExcel(‘table_a‘);  //table_a为table的id,downloadEcecl为a标签。
5    })
6 
7 </script>
View Code

 

jsexcel导出插件