1
1
/*
2
- * jQuery table2excel - v1.0.1
2
+ * jQuery table2excel - v1.0.2
3
3
* jQuery plugin to export an .xls file in browser from an HTML table
4
4
* https://github.com/rainabba/jquery-table2excel
5
5
*
8
8
*/
9
9
//table2excel.js
10
10
; ( function ( $ , window , document , undefined ) {
11
- var pluginName = "table2excel" ,
12
- defaults = {
13
- exclude : ".noExl" ,
14
- name : "Table2Excel"
15
- } ;
16
-
17
- // The actual plugin constructor
18
- function Plugin ( element , options ) {
19
- this . element = element ;
20
- // jQuery has an extend method which merges the contents of two or
21
- // more objects, storing the result in the first object. The first object
22
- // is generally empty as we don't want to alter the default options for
23
- // future instances of the plugin
24
- this . settings = $ . extend ( { } , defaults , options ) ;
25
- this . _defaults = defaults ;
26
- this . _name = pluginName ;
27
- this . init ( ) ;
28
- }
11
+ var pluginName = "table2excel" ,
12
+
13
+ defaults = {
14
+ exclude : ".noExl" ,
15
+ name : "Table2Excel"
16
+ } ;
17
+
18
+ // The actual plugin constructor
19
+ function Plugin ( element , options ) {
20
+ this . element = element ;
21
+ // jQuery has an extend method which merges the contents of two or
22
+ // more objects, storing the result in the first object. The first object
23
+ // is generally empty as we don't want to alter the default options for
24
+ // future instances of the plugin
25
+ //
26
+ this . settings = $ . extend ( { } , defaults , options ) ;
27
+ this . _defaults = defaults ;
28
+ this . _name = pluginName ;
29
+ this . init ( ) ;
30
+ }
31
+
32
+ Plugin . prototype = {
33
+ init : function ( ) {
34
+ var e = this ;
35
+
36
+ e . template = {
37
+ head : "<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>" ,
38
+ sheet : {
39
+ head : "<x:ExcelWorksheet><x:Name>" ,
40
+ tail : "</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet>"
41
+ } ,
42
+ mid : "</x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body>" ,
43
+ table : {
44
+ head : "<table>" ,
45
+ tail : "</table>"
46
+ } ,
47
+ foot : "</body></html>"
48
+ } ;
49
+
50
+ e . tableRows = [ ] ;
29
51
30
- Plugin . prototype = {
31
- init : function ( ) {
32
- var e = this ;
33
- e . 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>" ;
34
- e . template += "<x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions>" ;
35
- e . template += "<x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body><table>{table}</table></body></html>" ;
36
- e . tableRows = "" ;
37
-
38
- // get contents of table except for exclude
39
- $ ( e . element ) . find ( "tr" ) . not ( this . settings . exclude ) . each ( function ( i , o ) {
40
- e . tableRows += "<tr>" + $ ( o ) . html ( ) + "</tr>" ;
52
+ // get contents of table except for exclude
53
+ $ ( e . element ) . each ( function ( i , o ) {
54
+ var tempRows = "" ;
55
+ $ ( o ) . find ( "tr" ) . not ( e . settings . exclude ) . each ( function ( i , o ) {
56
+ tempRows += "<tr>" + $ ( o ) . html ( ) + "</tr>" ;
41
57
} ) ;
42
- this . tableToExcel ( this . tableRows , this . settings . name ) ;
43
- } ,
44
- tableToExcel : function ( table , name ) {
45
- var e = this ;
46
- e . uri = "data:application/vnd.ms-excel;base64," ;
47
- e . base64 = function ( s ) {
48
- return window . btoa ( unescape ( encodeURIComponent ( s ) ) ) ;
49
- } ;
50
- e . format = function ( s , c ) {
51
- return s . replace ( / { ( \w + ) } / g, function ( m , p ) {
52
- return c [ p ] ;
53
- } ) ;
54
- } ;
55
- e . ctx = {
56
- worksheet : name || "Worksheet" ,
57
- table : table
58
- } ;
59
- window . location . href = e . uri + e . base64 ( e . format ( e . template , e . ctx ) ) ;
60
- }
61
- } ;
58
+ e . tableRows . push ( tempRows ) ;
59
+ } ) ;
60
+
61
+ e . tableToExcel ( e . tableRows , e . settings . name ) ;
62
+ } ,
62
63
63
- $ . fn [ pluginName ] = function ( options ) {
64
- this . each ( function ( ) {
65
- if ( ! $ . data ( this , "plugin_" + pluginName ) ) {
66
- $ . data ( this , "plugin_" + pluginName , new Plugin ( this , options ) ) ;
67
- }
64
+ tableToExcel : function ( table , name ) {
65
+ var e = this , fullTemplate = "" , i ;
66
+
67
+ e . uri = "data:application/vnd.ms-excel;base64," ;
68
+ e . base64 = function ( s ) {
69
+ return window . btoa ( unescape ( encodeURIComponent ( s ) ) ) ;
70
+ } ;
71
+ e . format = function ( s , c ) {
72
+ return s . replace ( / { ( \w + ) } / g, function ( m , p ) {
73
+ return c [ p ] ;
68
74
} ) ;
75
+ } ;
76
+ e . ctx = {
77
+ worksheet : name || "Worksheet" ,
78
+ table : table
79
+ } ;
80
+
81
+ fullTemplate = e . template . head ;
82
+
83
+ if ( $ . isArray ( table ) ) {
84
+ for ( i in table ) {
85
+ //fullTemplate += e.template.sheet.head + "{worksheet" + i + "}" + e.template.sheet.tail;
86
+ fullTemplate += e . template . sheet . head + "Table" + i + "" + e . template . sheet . tail ;
87
+ }
88
+ }
89
+
90
+ fullTemplate += e . template . mid ;
91
+
92
+ if ( $ . isArray ( table ) ) {
93
+ for ( i in table ) {
94
+ fullTemplate += e . template . table . head + "{table" + i + "}" + e . template . table . tail ;
95
+ }
96
+ }
97
+
98
+ fullTemplate += e . template . foot ;
99
+
100
+ for ( i in table ) {
101
+ e . ctx [ "table" + i ] = table [ i ] ;
102
+ }
103
+ delete e . ctx . table ;
104
+
105
+ window . location . href = e . uri + e . base64 ( e . format ( fullTemplate , e . ctx ) ) ;
106
+ }
107
+ } ;
108
+
109
+ $ . fn [ pluginName ] = function ( options ) {
110
+ var e = this ;
111
+ e . each ( function ( ) {
112
+ if ( ! $ . data ( e , "plugin_" + pluginName ) ) {
113
+ $ . data ( e , "plugin_" + pluginName , new Plugin ( this , options ) ) ;
114
+ }
115
+ } ) ;
69
116
70
- // chain jQuery functions
71
- return this ;
72
- } ;
117
+ // chain jQuery functions
118
+ return e ;
119
+ } ;
73
120
74
121
} ) ( jQuery , window , document ) ;
0 commit comments