今天要來記錄一下HTML的Table如何轉excel

首先:

在head中先加上這行,避免中文亂碼

<head>
    <meta http-equiv="content-type" content="application/vnd.ms-excel; charset=UTF-8" />
 </head>

 

接著是javscript的部分

<script>
function myexcel(){
  var html = '<meta http-equiv="content-type" content="application/vnd.ms-excel; charset=UTF-8" /><title>Excel</title>';
  html += '';
  html += document.getElementById('myTable2').innerHTML + '';
  window.open('data:application/vnd.ms-excel,' + encodeURIComponent(html));
}
</script>

 

以下是HTML部分

<div id="myTable2">
    <table><thead>
    <tr><th>員工編號</th><th>姓名</th></tr>
    </thead><tbody>
    <tr><td>A00001</td><td>小名</td></tr>
    <tr><td>A00002</td><td>真真</td></tr>
    </tbody></table></div>
  <div><button type="button" onclick="myexcel()">excel匯出</button></div>

以上,就可以產生excel檔~~

arrow
arrow
    文章標籤
    HTML TABLE excel jquery
    全站熱搜

    kyo 發表在 痞客邦 留言(0) 人氣()