Export HTML table to Excel file but broken characters

I would like to export my html table to excel.
I tried to use php first, but my table may contain 2 ~ 3 sql code, so it is a little tricky.
So I want to use javascript / jQuery to export it.
It exported successfully, but there is an error - my HTML table contained Chinese, but in the EXCEL file, it became broken.
When I open the EXCEL file in notepad, it can display Chinese normally.

↓ script ↓

<script>
    //<![CDATA[ 
    $(window).load(function(){
        $("#btnExport").click(function(e) {
            window.open('data:application/vnd.ms-excel,' + encodeURIComponent($('#dvData').html()));
            e.preventDefault();
        });
    });//]]>  
</script>

      

↓ HTML and PHP ↓

<div id="dvData">
<table rules="all">
    <thead>
        <th width="50"></th>
        <th width="150">登入人數(Login)</th>
        <th width="150">儲值人數(P)</th>
        <th width="150">儲值總和(Sum)</th>
    </thead>
    <tr>
        <td>新玩家(New player)</td>
        <td><?php echo $new1; ?></td>
        <td><?php echo $ndata03['data'][0]['count(distinct userid)']; ?></td>
        <td><?php echo $ndata03['data'][0]['sum(amount)']; ?></td>
    </tr>
    <tr>
        <td>老玩家(Old player)</td>
        <td><?php echo $old1; ?></td>
        <td><?php echo $odata03['data'][0]['count(distinct userid)']; ?></td>
        <td><?php echo $odata03['data'][0]['sum(amount)']; ?></td>
    </tr>
</table>
</div>

      

How can I change the code?

+3


source to share





All Articles