2016-05-17 55 views
0

下面是導出HTML表到excel我的示例代碼:導出到ExcelĴ查詢代碼導出後在Excel中刪除0

<!DOCTYPE HTML> 
<html> 
    <head> 
     <script src = "https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" > 
     </script> 
     <script> 
      $(function(){ 
       $('button').click(function(){ 
        var dvData = $('#dvData').html(); 
        window.open('data:application/vnd.ms-excel,' + $('#dvData').html()); 
       }) 
      }) 
     </script> 
    </head> 
    <body> 
     <button> 
      Export To Excel 
     </button> 
     <div id="dvData"> 
      <table> 
       <th> ID </th> 
       <th> Name </th> 
       <tr> 
        <td>"1.0"</td> 
        <td>string1</td> 
       </tr> 
       <tr> 
        <td>10</td> 
        <td>string2</td> 
       </tr> 
      </table> 
     </div> 
    </body> 
</html> 

該出口1.0在Excel 1這是不對的。我嘗試添加單引號,但出現在我不想要的網頁中。

+1

可能重複:http://stackoverflow.com/questions/25989238/exporting-html-table-with-correct-format-in-javascript – 1cgonza

回答

0

使用「MSO的數字格式」,並定義數據格式

即:「#,## 0.0

以獲取更多信息,您可以檢查這個答案。

Exporting HTML table with correct format in javascript

+0

應該提到的是,'數據:應用程序/越南盾.ms-excel,...'是一個'URI'。所以這個「URI」中的數據必須是URI編碼的。 'window.open('data:application/vnd.ms-excel,'+ encodeURIComponent($('#dvData')。html()));'。 –

+0

是@AxelRichter你是對的 – Sid

+0

@Sid這個工程,但現在的問題是一個像1.0這樣的數字顯示爲1.00,而是我想這些數字被導出,因爲它就像一個字符串 – Neha