1
1
/*
2
- * ExcellentExport
2
+ * ExcellentExport.
3
+ * A client side Javascript export to Excel.
4
+ *
5
+ * @autor : Jordi Burgos ([email protected] )
3
6
*
4
7
* Based on:
5
8
* https://gist.github.com/insin/1031969
6
9
* http://jsfiddle.net/insin/cmewv/
7
10
*
8
11
*/
9
12
window . ExcellentExport = ( function ( ) {
10
- var uri = 'data:application/vnd.ms-excel;base64,' ;
11
- var 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><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>' ;
13
+ var version = "1.1" ;
14
+ var uri = { excel : 'data:application/vnd.ms-excel;base64,' , csv : 'data:application/csv;base64,' } ;
15
+ var template = { excel : '<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><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>' } ;
12
16
var base64 = function ( s ) {
13
17
return window . btoa ( unescape ( encodeURIComponent ( s ) ) ) ;
14
18
} ;
@@ -18,16 +22,31 @@ window.ExcellentExport = (function() {
18
22
} ) ;
19
23
} ;
20
24
25
+ var get = function ( element ) {
26
+ if ( ! element . nodeType ) {
27
+ return document . getElementById ( element ) ;
28
+ }
29
+ return element ;
30
+ } ;
31
+
32
+ var tableToCSV = function ( table ) {
33
+ return "COL1,COL2,COL3\n100,200,300\n400,500,600" ;
34
+ } ;
35
+
21
36
var ee = {
22
37
excel : function ( anchor , table , name ) {
23
- if ( ! table . nodeType ) {
24
- table = document . getElementById ( table ) ;
25
- }
38
+ table = get ( table ) ;
26
39
var ctx = { worksheet : name || 'Worksheet' , table : table . innerHTML } ;
27
- var hrefvalue = uri + base64 ( format ( template , ctx ) ) ;
40
+ var hrefvalue = uri . excel + base64 ( format ( template . excel , ctx ) ) ;
28
41
anchor . href = hrefvalue ;
29
42
// Return true to allow the link to work
30
43
return true ;
44
+ } ,
45
+ csv : function ( anchor , table ) {
46
+ table = get ( table ) ;
47
+ var csvData = tableToCSV ( table ) ;
48
+ var hrefvalue = uri . csv + base64 ( csvData ) ;
49
+ anchor . href = hrefvalue ;
31
50
}
32
51
} ;
33
52
0 commit comments