Advertisement
Guest User

Table2Excel

a guest
Jan 19th, 2018
474
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  *  jQuery table2excel - v1.0.2
  3.  *  jQuery plugin to export an .xls file in browser from an HTML table
  4.  *  https://github.com/rainabba/jquery-table2excel
  5.  *
  6.  *  Made by rainabba
  7.  *  Under MIT License
  8.  */
  9. //table2excel.js
  10. ;(function ( $, window, document, undefined ) {
  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.             var utf8Heading = "<meta http-equiv=\"content-type\" content=\"application/vnd.ms-excel; charset=UTF-8\">";
  37.             e.template = {
  38.                 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\">" + utf8Heading + "<head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets>",
  39.                 sheet: {
  40.                     head: "<x:ExcelWorksheet><x:Name>",
  41.                     tail: "</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet>"
  42.                 },
  43.                 mid: "</x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body>",
  44.                 table: {
  45.                     head: "<table>",
  46.                     tail: "</table>"
  47.                 },
  48.                 foot: "</body></html>"
  49.             };
  50.  
  51.             e.tableRows = [];
  52.  
  53.             // get contents of table except for exclude
  54.             $(e.element).each( function(i,o) {
  55.                 var tempRows = "";
  56.                 $(o).find("tr").not(e.settings.exclude).each(function (i,o) {
  57.                     if(e.settings.exclude_img){
  58.                         $(o).find('img').remove();
  59.                     }
  60.                     if(e.settings.exclude_links){
  61.                         $(o).find('a').contents().unwrap();
  62.                     }
  63.                     tempRows += "<tr>" + $(o).html() + "</tr>";
  64.                 });
  65.                 e.tableRows.push(tempRows);
  66.             });
  67.  
  68.             e.tableToExcel(e.tableRows, e.settings.name, e.settings.sheetName);
  69.         },
  70.  
  71.         tableToExcel: function (table, name, sheetName) {
  72.             var e = this, fullTemplate="", i, link, a;
  73.  
  74.             e.uri = "data:application/vnd.ms-excel;base64,";
  75.             e.base64 = function (s) {
  76.                 return window.btoa(unescape(encodeURIComponent(s)));
  77.             };
  78.             e.format = function (s, c) {
  79.                 return s.replace(/{(\w+)}/g, function (m, p) {
  80.                     return c[p];
  81.                 });
  82.             };
  83.  
  84.             sheetName = typeof sheetName === "undefined" ? "Sheet" : sheetName;
  85.  
  86.             e.ctx = {
  87.                 worksheet: name || "Worksheet",
  88.                 table: table,
  89.                 sheetName: sheetName,
  90.             };
  91.  
  92.             fullTemplate= e.template.head;
  93.  
  94.             if ( $.isArray(table) ) {
  95.                 for (i in table) {
  96.                       //fullTemplate += e.template.sheet.head + "{worksheet" + i + "}" + e.template.sheet.tail;
  97.                       fullTemplate += e.template.sheet.head + sheetName + i + e.template.sheet.tail;
  98.                 }
  99.             }
  100.  
  101.             fullTemplate += e.template.mid;
  102.  
  103.             if ( $.isArray(table) ) {
  104.                 for (i in table) {
  105.                     fullTemplate += e.template.table.head + "{table" + i + "}" + e.template.table.tail;
  106.                 }
  107.             }
  108.  
  109.             fullTemplate += e.template.foot;
  110.  
  111.             for (i in table) {
  112.                 e.ctx["table" + i] = table[i];
  113.             }
  114.             delete e.ctx.table;
  115.  
  116.             if (typeof msie !== "undefined" && msie > 0 || !!navigator.userAgent.match(/Trident.*rv\:11\./))      // If Internet Explorer
  117.             {
  118.                 if (typeof Blob !== "undefined") {
  119.                     //use blobs if we can
  120.                     fullTemplate = [fullTemplate];
  121.                     //convert to array
  122.                     var blob1 = new Blob(fullTemplate, { type: "text/html" });
  123.                     window.navigator.msSaveBlob(blob1, getFileName(e.settings) );
  124.                 } else {
  125.                     //otherwise use the iframe and save
  126.                     //requires a blank iframe on page called txtArea1
  127.                     txtArea1.document.open("text/html", "replace");
  128.                     txtArea1.document.write(e.format(fullTemplate, e.ctx));
  129.                     txtArea1.document.close();
  130.                     txtArea1.focus();
  131.                     sa = txtArea1.document.execCommand("SaveAs", true, getFileName(e.settings) );
  132.                 }
  133.  
  134.             } else {
  135.                 link = e.uri + e.base64(e.format(fullTemplate, e.ctx));
  136.                 a = document.createElement("a");
  137.                 a.download = getFileName(e.settings);
  138.                 a.href = link;
  139.  
  140.                 document.body.appendChild(a);
  141.  
  142.                 a.click();
  143.  
  144.                 document.body.removeChild(a);
  145.             }
  146.  
  147.             return true;
  148.         }
  149.     };
  150.  
  151.     function getFileName(settings) {
  152.         return ( settings.filename ? settings.filename : "table2excel" ) +
  153.                ( settings.fileext ? settings.fileext : ".xlsx" );
  154.     }
  155.  
  156.     $.fn[ pluginName ] = function ( options ) {
  157.         var e = this;
  158.             e.each(function() {
  159.                 if ( !$.data( e, "plugin_" + pluginName ) ) {
  160.                     $.data( e, "plugin_" + pluginName, new Plugin( this, options ) );
  161.                 }
  162.             });
  163.  
  164.         // chain jQuery functions
  165.         return e;
  166.     };
  167.  
  168. })( jQuery, window, document );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement