├── .gitignore ├── LICENSE ├── README.md ├── bower.json ├── dist ├── tableExport.js └── tableExport.min.js ├── examples ├── example_1.html ├── example_2.html ├── main.js └── style.css ├── gulpfile.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | .idea 3 | yarn* 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | All source code included in this jQuery Plugin 2 | is, unless otherwise specified, released under the MIT licence as 3 | follows: 4 | 5 | ----- begin license block ----- 6 | 7 | Copyright 2013-2014 Deux Huit Huit 8 | 9 | Permission is hereby granted, free of charge, to any person obtaining a copy 10 | of this software and associated documentation files (the "Software"), to deal 11 | in the Software without restriction, including without limitation the rights 12 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 | copies of the Software, and to permit persons to whom the Software is 14 | furnished to do so, subject to the following conditions: 15 | 16 | The above copyright notice and this permission notice shall be included in 17 | all copies or substantial portions of the Software. 18 | 19 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 25 | THE SOFTWARE. 26 | 27 | ----- end license block ----- 28 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## jQuery Table Exporter 2 | 3 | This is a simple jQuery plug-in that allows exporting html tables to _CSV_, 4 | _XLS_, _TXT_, _SQL_, _JSON_. 5 | 6 | ![Example work](http://archakov.im/uploads/tableExport-1.gif) 7 | 8 | ## Usage 9 | 10 | Import the script before ``: 11 | 12 | ```html 13 | 14 | ``` 15 | 16 | And add this code to your javascript file: 17 | 18 | ```javascript 19 | $('.table').tableExport({ 20 | filename: 'table', 21 | format: 'csv', 22 | }); 23 | ``` 24 | 25 | `.table` - The class name of the exported table. 26 | 27 | (or using options): 28 | 29 | ```javascript 30 | $('.table').tableExport({ 31 | filename: 'table_%DD%-%MM%-%YY%', // the filename prefix + date format (the extension is automatic) 32 | format: 'xls', // type of your export file: csv, xls, txt, sql, json 33 | cols: '2,3,4', // export of specified columns 34 | }); 35 | ``` 36 | 37 | ### Exclude columns 38 | 39 | ```javascript 40 | $('.table').tableExport({ 41 | filename: 'table_%DD%-%MM%-%YY%', 42 | format: 'csv', 43 | excludeCols: '1,5', 44 | }); 45 | ``` 46 | 47 | ### Call events 48 | 49 | ```javascript 50 | $('.table').tableExport({ 51 | filename: 'table_%DD%-%MM%-%YY%', 52 | onbefore: function() { 53 | alert('The export of tables begins!'); 54 | }, 55 | onafter: function() { 56 | alert('Export complete :)'); 57 | }, 58 | }); 59 | ``` 60 | 61 | ## Date format 62 | * **%DD%** — day 63 | * **%MM%** — month 64 | * **%YY%** — year 65 | * **%hh%** — hours 66 | * **%mm%** — minutes 67 | * **%ss%** — seconds 68 | 69 | ## Options 70 | 71 | * **filename** — the name of the exported file (without extension) 72 | * **format** — the export file format (only: csv, xls, txt, sql, json) 73 | * **cols** — select specific columns for export 74 | * **excludeCols** — excludes specified columns when exporting 75 | * **head_delimiter** — separator for titles when exporting 76 | * **column_delimiter** — separator for column when exporting 77 | * **onbefore(this)** — Function to call before trigger is called 78 | * **onafter(this)** — Function to call after trigger is called 79 | 80 | ## Demos 81 | 82 | More demos in the examples folder. Or on the links below :) 83 | 84 | * **[Example #1](https://htmlpreview.github.io/?https://github.com/Archakov06/jQuery.tableExport/blob/master/examples/example_1.html)** 85 | * **[Example #2](https://htmlpreview.github.io/?https://github.com/Archakov06/jQuery.tableExport/blob/master/examples/example_2.html)** 86 | 87 | ## To Do 88 | 89 | * [x] Parsing date 90 | * [x] Exporting SQL 91 | * [x] Support Safari browser 92 | 93 | #### 30.11.2017 94 | 95 | * UPD: JSON Exporting 96 | * Bug fixes 97 | 98 | ## Contacts 99 | * **E-Mail**: 100 | * **Website**: 101 | 102 | ## License 103 | 104 | Now licensed under the MIT License: 105 | -------------------------------------------------------------------------------- /bower.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "jquery-tableExport", 3 | "description": "This is a simple jQuery plug-in that allows exporting html tables to: CSV, XLS, TXT, SQL.", 4 | "main": "jquery.tableExport.js", 5 | "authors": [ 6 | "Archakov Dennis (http://archakov.im/)" 7 | ], 8 | "license": "MIT", 9 | "keywords": [ 10 | "jquery-plugin", 11 | "ecosystem:jquery", 12 | "jquery", 13 | "tableexport", 14 | "table", 15 | "csv", 16 | "xls", 17 | "sql", 18 | "txt", 19 | "export", 20 | "table", 21 | "exporting", 22 | "html", 23 | "html5" 24 | ], 25 | "repository": { 26 | "type": "git", 27 | "url": "git+https://github.com/Archakov06/jQuery-tableExport.git" 28 | }, 29 | "moduleType": [], 30 | "homepage": "http://archakov.im/post/jquery-tableexport.html", 31 | "ignore": [ 32 | "node_modules", 33 | "bower_components", 34 | "test", 35 | "tests", 36 | "img", 37 | "gulpfile.js", 38 | "package.json" 39 | ] 40 | } -------------------------------------------------------------------------------- /dist/tableExport.js: -------------------------------------------------------------------------------- 1 | (function($) { 2 | $.fn.tableExport = function(options) { 3 | var options = $.extend( 4 | { 5 | filename: 'table', 6 | format: 'csv', 7 | cols: '', 8 | excludeCols: '', 9 | head_delimiter: ',', 10 | column_delimiter: ',', 11 | quote: true, 12 | onBefore: function(t) {}, 13 | onAfter: function(t) {}, 14 | }, 15 | options 16 | ); 17 | 18 | var $this = $(this); 19 | 20 | var cols = options.cols ? options.cols.split(',') : []; 21 | var excludeCols = options.excludeCols ? options.excludeCols.split(',') : []; 22 | 23 | var result = ''; 24 | 25 | var data_type = { 26 | csv: 'text/csv', 27 | txt: 'text/plain', 28 | xls: 'application/vnd.ms-excel', 29 | json: 'application/json', 30 | }; 31 | 32 | if ( 33 | typeof options.onBefore != 'function' || 34 | typeof options.onAfter != 'function' || 35 | !options.format || 36 | !options.head_delimiter || 37 | !options.column_delimiter || 38 | !options.filename 39 | ) { 40 | console.error('One of the parameters is incorrect.'); 41 | return false; 42 | } 43 | 44 | function getHeaders() { 45 | var th = $this.find('thead th'); 46 | var arr = []; 47 | 48 | th.each(function(i, e) { 49 | if (cols.length) { 50 | cols.forEach(function(c) { 51 | if (c == i + 1) { 52 | arr.push(e.innerText); 53 | } 54 | }); 55 | } else { 56 | if (excludeCols.indexOf((i + 1).toString()) == -1) { 57 | arr.push(e.innerText); 58 | } 59 | } 60 | }); 61 | 62 | return arr; 63 | } 64 | 65 | function getItems() { 66 | var tr = $this.find('tbody tr'); 67 | var arr = []; 68 | 69 | tr.each(function(i, e) { 70 | var s = []; 71 | 72 | if (cols.length) { 73 | cols.forEach(function(c) { 74 | s.push( 75 | $(e) 76 | .find('td:nth-child(' + c + ')') 77 | .text() 78 | ); 79 | }); 80 | arr.push(s); 81 | } else { 82 | var td = $(e).find('td'); 83 | td.each(function(i, t) { 84 | if (excludeCols.indexOf((i + 1).toString()) == -1) { 85 | s.push(t.innerText); 86 | } 87 | }); 88 | arr.push(s); 89 | } 90 | }); 91 | 92 | return arr; 93 | } 94 | 95 | function download(data, filename, format) { 96 | var a = document.createElement('a'); 97 | var blob = new Blob(['\ufeff', data], { type: data_type[format] }); 98 | a.href = URL.createObjectURL(blob); 99 | 100 | var now = new Date(); 101 | var time_arr = [ 102 | 'DD:' + now.getDate(), 103 | 'MM:' + (now.getMonth() + 1), 104 | 'YY:' + now.getFullYear(), 105 | 'hh:' + now.getHours(), 106 | 'mm:' + now.getMinutes(), 107 | 'ss:' + now.getSeconds(), 108 | ]; 109 | 110 | time_arr.forEach(function(item) { 111 | var key = item.split(':')[0]; 112 | var val = item.split(':')[1]; 113 | var regexp = new RegExp('%' + key + '%', 'g'); 114 | filename = filename.replace(regexp, val); 115 | }); 116 | 117 | a.download = filename + '.' + format; 118 | a.click(); 119 | 120 | if (!navigator.userAgent.toLowerCase().match(/firefox/)) { 121 | a.remove(); 122 | } 123 | } 124 | 125 | options.onBefore($this); 126 | 127 | switch (options.format) { 128 | case 'csv': 129 | var headers = getHeaders(); 130 | var items = getItems(); 131 | 132 | if (options.quote === true) { 133 | var quote = options.quote === true ? "\"" : null; 134 | 135 | headers.forEach(function (item, i) { 136 | headers[i] = quote + item + quote; 137 | }); 138 | 139 | items.forEach(function (item, i) { 140 | item.forEach(function (cell, j){ 141 | item[j] = quote + cell + quote; 142 | }); 143 | }); 144 | } 145 | 146 | result += headers.join(options.head_delimiter) + '\n'; 147 | 148 | items.forEach(function(item, i) { 149 | result += item.join(options.column_delimiter) + '\n'; 150 | }); 151 | 152 | break; 153 | 154 | case 'txt': 155 | var headers = getHeaders(); 156 | var items = getItems(); 157 | 158 | result += headers.join(options.head_delimiter) + '\n'; 159 | 160 | items.forEach(function(item, i) { 161 | result += item.join(options.column_delimiter) + '\n'; 162 | }); 163 | 164 | break; 165 | 166 | case 'xls': 167 | var headers = getHeaders(); 168 | var items = getItems(); 169 | template = 170 | '%thead%%tbody%
'; 171 | 172 | var res = ''; 173 | headers.forEach(function(item, i) { 174 | res += '' + item + ''; 175 | }); 176 | template = template.replace('%thead%', res); 177 | 178 | res = ''; 179 | items.forEach(function(item, i) { 180 | res += ''; 181 | item.forEach(function(td, i) { 182 | res += '' + td + ''; 183 | }); 184 | res += ''; 185 | }); 186 | template = template.replace('%tbody%', res); 187 | 188 | result = template; 189 | break; 190 | 191 | case 'sql': 192 | var headers = getHeaders(); 193 | var items = getItems(); 194 | 195 | items.forEach(function(item, i) { 196 | result += 197 | 'INSERT INTO table (' + 198 | headers.join(',') + 199 | ") VALUES ('" + 200 | item.join("','") + 201 | "');\n"; 202 | }); 203 | 204 | break; 205 | 206 | case 'json': 207 | var headers = getHeaders(); 208 | var items = getItems(); 209 | 210 | result = JSON.stringify({ header: headers, items: items }); 211 | 212 | break; 213 | } 214 | 215 | download(result, options.filename, options.format); 216 | 217 | options.onAfter($this); 218 | }; 219 | })(jQuery); 220 | -------------------------------------------------------------------------------- /dist/tableExport.min.js: -------------------------------------------------------------------------------- 1 | /*! 2 | * jQuery Table Export v1.0.1 3 | * https://github.com/Archakov06/jQuery-tableExport 4 | * Released under the MIT License. 5 | */ 6 | !function(e){e.fn.tableExport=function(t){function n(){var e=r.find("thead th"),t=[];return e.each(function(e,n){i.length?i.forEach(function(o){o==e+1&&t.push(n.innerText)}):-1==c.indexOf((e+1).toString())&&t.push(n.innerText)}),t}function o(){var t=r.find("tbody tr"),n=[];return t.each(function(t,o){var a=[];if(i.length)i.forEach(function(t){a.push(e(o).find("td:nth-child("+t+")").text())}),n.push(a);else{e(o).find("td").each(function(e,t){-1==c.indexOf((e+1).toString())&&a.push(t.innerText)}),n.push(a)}}),n}function a(e,t,n){var o=document.createElement("a"),a=new Blob(["\ufeff",e],{type:l[n]});o.href=URL.createObjectURL(a);var r=new Date;["DD:"+r.getDate(),"MM:"+(r.getMonth()+1),"YY:"+r.getFullYear(),"hh:"+r.getHours(),"mm:"+r.getMinutes(),"ss:"+r.getSeconds()].forEach(function(e){var n=e.split(":")[0],o=e.split(":")[1],a=new RegExp("%"+n+"%","g");t=t.replace(a,o)}),o.download=t+"."+n,o.click(),navigator.userAgent.toLowerCase().match(/firefox/)||o.remove()}var t=e.extend({filename:"table",format:"csv",cols:"",excludeCols:"",head_delimiter:",",column_delimiter:",",quote:!0,onBefore:function(e){},onAfter:function(e){}},t),r=e(this),i=t.cols?t.cols.split(","):[],c=t.excludeCols?t.excludeCols.split(","):[],f="",l={csv:"text/csv",txt:"text/plain",xls:"application/vnd.ms-excel",json:"application/json"};if(!("function"==typeof t.onBefore&&"function"==typeof t.onAfter&&t.format&&t.head_delimiter&&t.column_delimiter&&t.filename))return console.error("One of the parameters is incorrect."),!1;switch(t.onBefore(r),t.format){case"csv":var u=n(),s=o();if(!0===t.quote){var h=!0===t.quote?'"':null;u.forEach(function(e,t){u[t]=h+e+h}),s.forEach(function(e,t){e.forEach(function(t,n){e[n]=h+t+h})})}f+=u.join(t.head_delimiter)+"\n",s.forEach(function(e,n){f+=e.join(t.column_delimiter)+"\n"});break;case"txt":var u=n(),s=o();f+=u.join(t.head_delimiter)+"\n",s.forEach(function(e,n){f+=e.join(t.column_delimiter)+"\n"});break;case"xls":var u=n(),s=o();template="%thead%%tbody%
";var d="";u.forEach(function(e,t){d+=""+e+""}),template=template.replace("%thead%",d),d="",s.forEach(function(e,t){d+="",e.forEach(function(e,t){d+=""+e+""}),d+=""}),template=template.replace("%tbody%",d),f=template;break;case"sql":var u=n(),s=o();s.forEach(function(e,t){f+="INSERT INTO table ("+u.join(",")+") VALUES ('"+e.join("','")+"');\n"});break;case"json":var u=n(),s=o();f=JSON.stringify({header:u,items:s})}a(f,t.filename,t.format),t.onAfter(r)}}(jQuery); -------------------------------------------------------------------------------- /examples/example_1.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | Example #1 10 | 11 | 12 | 13 | 14 | 18 | 19 | 20 | 21 | 22 |
23 | 24 |
25 |

Export Table: Example #1

26 | 27 |
28 |
29 | 30 | 37 |
38 |
39 | 40 |
41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 |
#First NameLast NameE-MailNumber
1Delphasiliciophitecircumflex@masterwork.net997300858
2PaulaAusiellobemirrorment@moy.edu779213455
3GaynellSalguerosmoothpate@podalgia.edu999908414
4OteliaNittahispanophile@auditorship.edu947377435
5DarrenMalteztoxotidae@tut.net902590424
6LarraineZelascolanciers@unpleasantish.co.uk668639791
96 |
97 | 98 |
99 | 100 |
101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | -------------------------------------------------------------------------------- /examples/example_2.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | Example #2 10 | 11 | 12 | 13 | 14 | 18 | 19 | 20 | 21 | 22 |
23 | 24 |
25 |

Export Table: Example #2

26 | 27 |
28 |
29 | 30 | 37 |
38 |
39 | 40 |
41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 |
#First NameLast NameE-MailNumber
1Delphasiliciophitecircumflex@masterwork.net997300858
2PaulaAusiellobemirrorment@moy.edu779213455
3GaynellSalguerosmoothpate@podalgia.edu999908414
4OteliaNittahispanophile@auditorship.edu947377435
5DarrenMalteztoxotidae@tut.net902590424
6LarraineZelascolanciers@unpleasantish.co.uk668639791
96 |
97 | 98 |
99 | 100 |
101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | -------------------------------------------------------------------------------- /examples/main.js: -------------------------------------------------------------------------------- 1 | 2 | function exportTo(type) { 3 | 4 | $('.table').tableExport({ 5 | filename: 'table_%DD%-%MM%-%YY%', 6 | format: type, 7 | cols: '2,3,4' 8 | }); 9 | 10 | } 11 | 12 | function exportAll(type) { 13 | 14 | $('.table').tableExport({ 15 | filename: 'table_%DD%-%MM%-%YY%-month(%MM%)', 16 | format: type 17 | }); 18 | 19 | } -------------------------------------------------------------------------------- /examples/style.css: -------------------------------------------------------------------------------- 1 | @import url('https://fonts.googleapis.com/css?family=Open+Sans:400,300,600&subset=latin,cyrillic,cyrillic-ext'); 2 | 3 | body { 4 | font-family: 'Open Sans'; 5 | padding-top: 20px; 6 | padding-bottom: 20px; 7 | } 8 | 9 | .container.home { 10 | margin-top: 50px; 11 | } 12 | 13 | .container.home h1 { 14 | margin-bottom: 30px; 15 | } 16 | 17 | .container.home table { 18 | font-size: 16px; 19 | } 20 | 21 | .top-panel { 22 | margin-bottom: 30px; 23 | } 24 | 25 | #firm_copy_table { 26 | display: none; 27 | } 28 | -------------------------------------------------------------------------------- /gulpfile.js: -------------------------------------------------------------------------------- 1 | const gulp = require('gulp'); 2 | const banner = require('gulp-banner'); 3 | const uglify = require('gulp-uglifyjs'); 4 | const package = require('./package.json'); 5 | 6 | var header = [ 7 | '/*!', 8 | ' * ' + package.title + ' v' + package.version, 9 | ' * ' + package.homepage, 10 | ' * Released under the MIT License.', 11 | ' */\n' 12 | ].join('\n'); 13 | 14 | gulp.task('default', function(){ 15 | gulp.src(package.main) 16 | .pipe(uglify( package.main.split('/')[2].replace('.js', '.min.js') )) 17 | .pipe(banner(header, { 18 | pkg: package 19 | })) 20 | .pipe(gulp.dest('dist')); 21 | }); -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "jQuery.tableExport", 3 | "title": "jQuery Table Export", 4 | "description": "This is a simple jQuery plug-in that allows exporting html tables to: CSV, XLS, TXT, SQL.", 5 | "version": "1.0.1", 6 | "main": "./dist/tableExport.js", 7 | "homepage": "https://github.com/Archakov06/jQuery-tableExport", 8 | "license": "MIT", 9 | "licenses": [ 10 | { 11 | "type": "MIT", 12 | "url": "https://raw.githubusercontent.com/mar10/fancytree/master/LICENSE.txt" 13 | } 14 | ], 15 | "author": { 16 | "name": "Archakov Dennis", 17 | "email": "hello@archakov.im", 18 | "url": "http://archakov.im" 19 | }, 20 | "bugs": { 21 | "url": "https://github.com/Archakov06/jQuery-tableExport/issues" 22 | }, 23 | "repository": { 24 | "type": "git", 25 | "url": "git+https://github.com/Archakov06/jQuery-tableExport.git" 26 | }, 27 | "keywords": [ 28 | "jquery-plugin", 29 | "ecosystem:jquery", 30 | "jquery", 31 | "tableexport", 32 | "table", 33 | "csv", 34 | "xls", 35 | "sql", 36 | "txt", 37 | "export", 38 | "table", 39 | "exporting", 40 | "html", 41 | "html5", 42 | "table exporter", 43 | "jquery table", 44 | "jquery table exporter", 45 | "export table", 46 | "jquery.table", 47 | "jquery.tableExport" 48 | ], 49 | "devDependencies": { 50 | "gulp": "^3.9.1", 51 | "gulp-banner": "^0.1.3", 52 | "gulp-uglifyjs": "^0.6.2", 53 | "webpack": "^1.14.0" 54 | } 55 | } 56 | --------------------------------------------------------------------------------