function tableToExcel(table, fileName, sheet){

 var uri = 'data:application/vnd.ms-excel;base64,'

   , 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]--><meta http-equiv="content-type" content="text/plain; charset=UTF-8"/></head><body><table border=1>{table}</table></body></html>'

   , base64 = function(s) { return window.btoa(unescape(encodeURIComponent(s))) }

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

   

var ctx = {worksheet: sheet || 'Worksheet', table: $("#" + table).html()}

  var link = document.createElement("a");

  link.download = fileName + ".xls";

  link.href = uri + base64(format(template, ctx));

  link.click();

}

저작자 표시
신고

'javascript' 카테고리의 다른 글

json stringify  (0) 2017.11.18
더미 데이타 만들기  (0) 2017.09.13
tableToExcel  (0) 2017.08.18
promise  (0) 2017.03.12
json data roop  (0) 2012.02.29
DOM  (0) 2012.02.17

+ Recent posts