jfery ajax导出exf表在utf-8中

时间:2018-01-25 11:32:20

标签: jquery ajax excel utf-8

我想通过jquery将html表格下载到带有utf-8格式的excel文件。下载完成后,特殊字符不可读。

这是我的代码(ajax函数被注释,该代码可以在jsfiddle上正常工作):JSFiddle code

HTML代码

<div class="card border-primary mb-3">
  <table class="table table-hover results" id="exportExcel">
    <thead style="color: white; background-color: #0099CC">
      <tr>
        <td>Osobné číslo</td>
        <td>Meno</td>
        <td>Priezvisko</td>
        <td>Oddelenie</td>
        <td class="stockItem"><button type="button" id="excel" class="btn btn-sm btn-outline-light">stiahni zoznam</button></td>
      </tr>
    </thead>
    <tbody id="excelExport"></tbody>
    <!-- data dynamically loaded with ajax -->
      <tr>
        <td>1</td>
        <td>Ľudmila</td>
        <td>Malá</td>
        <td>vyr</td>
      </tr>
      <tr>
        <td>2</td>
        <td>Peter</td>
        <td>Hraško</td>
        <td>lg</td>
      </tr>
  </table>
</div>

jquery代码

$(document).ready(function() {
  $("#excel").click(function(e) {
        // ajax field needed
        /*var stDate = $("#fromDate").val();
        var enDate = $("#toDate").val();*/

        //ajax function
        /*
        $.ajax({  
        type: 'POST',   
        url: 'downloadExcel.php',
        data: 'startDate='+stDate+'&endDate='+enDate, 
        success: function(data) {
            $('#excelExport').html(data);*/

            e.preventDefault();

            //getting data from our table
            var data_type = 'data:application/vnd.ms-excel; charset=utf-8';
            var table_div = document.getElementById('exportExcel');
            var table_html = table_div.outerHTML.replace(/ /g, '%20');

            var a = document.createElement('a');
            a.href = data_type + ', ' + table_html;
            a.download = 'exported_table' + '.xls';
            a.click();
//}
//});
});
});

这在头上:

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

我试图找到工作代码,但没有任何运气。

0 个答案:

没有答案