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