Tabrer

Transform copied cells from excel, word, access and much others to a nice html bootstrap table.

by Dhanck

HTML

<p>

  Paste excel data here:</p>
<textarea name="excel_data" style="height:150px;" class="form-control"></textarea>
<br>
<input type="button" onclick="javascript:generateTable()" value="Genereate Table" class="btn btn-primary" />
<input type="button" onclick="javascript:exportToExcel()" value="Export to Excel" class="btn btn-primary" />

<label><input type="checkbox" id="excelName">Name excel document</label>

<br>
<hr>
<div id="excel_table"></div>

CSS

body {
  margin: 20px;
}

table {
  border-collapse: collapse;
}

table td {}

table tr:first-child {
  font-weight: bold;
}

JavaScript

function generateTable() {
  var data = $('textarea[name=excel_data]').val();
  console.log(data);
  var rows = data.split("\n");

  var table = $('<table class="table table-condensed table-hover table-striped table-bordered" />');

  for (var y in rows) {
    var cells = rows[y].split("\t");
    var row = $('<tr />');
    for (var x in cells) {
      row.append('<td>' + cells[x] + '</td>');
    }
    table.append(row);
  }


  // Insert into DOM
  $('#excel_table').html(table);

  // Clean empty rows
  $(function() {
    $('tr').each(function() {
      var _hide = true;
      $(this).find('td:not(.requirementRight)').each(function() {
        if ($(this).text().trim() != '') {
          _hide = false;
        }
      });

      if (_hide) {
        $(this).remove();
      }
    });
  })

}


function exportToExcel(){
var htmls = "";
            var uri = 'data:application/vnd.ms-excel;base64,';
            var template = $("#excel_table").html(); 
            var base64 = function(s) {
                return window.btoa(unescape(encodeURIComponent(s)))
            };

            var format = function(s, c) {
                return s.replace(/{(\w+)}/g, function(m, p) {
                    return c[p];
                })
            };

            htmls = "YOUR HTML AS TABLE"

            var ctx = {
                worksheet : 'Worksheet',
                table : htmls
            }


            var link = document.createElement("a");
            link.download = "export.xls";
            link.href = uri + base64(format(template, ctx));
            link.click();
}