2013-07-13 27 views
0

我在HTML表中有大約5000行(tr),每行有10列(td)。現在我試圖出口這整個HTML表格使用下面的jQuery代碼到excel:無法使用jQuery將html表導出爲ex​​cel

​​

也嘗試過許多其他的jQuery插件像DataTables.net,jqWidgets和的jqGrid但每次我的瀏覽器被撞毀並有重新加載頁面。

+0

首先嚐試發送數據到應用程序服務器,然後設置頭從你的服務河 – ravisoni

+0

無法使用應用程序服務器,因爲我必須僅使用客戶端來實現這些功能。 –

+0

你可以在jsfiddle中重現問題嗎? – Default

回答

1

window.open()都有其適用範圍和侷限性已在此崗位得到了很好的解釋:Export to CSV using jQuery and html

你的關心,我有2500行測試,它工作正常。 (我不能在jsfiddler上上傳這麼多的數據,但我相信它也能工作5000行。)

另外,我懷疑你是否將你的html表格封裝到容器DIV中。只要把你的HTML表格到一個DIV和使用這樣的 -

$("[id$=myButtonControlID]").click(function(e) { 
    window.open('data:application/vnd.ms-excel,' + encodeURIComponent($('div[id$=divTableDataHolder]').html())); 
    e.preventDefault(); }); 

http://jsfiddle.net/AnilAwadh/wJyWm/

encodeURIComponent方法()是用於編碼的特殊字符,如果你的數據有任何一個Javascript功能及其使用可選的。

0

如果你的目標非IE瀏覽器,請嘗試以下操作:

var test = $('#data'); 
window.open('data:application/vnd.ms-excel,' + 
      encodeURIComponent(test[0].outerHTML)); 

看到它在這裏工作通過使用Chrome:http://jsfiddle.net/56tvb/2/

如果你的目標的Internet Explorer作爲一個瀏覽器,就必須尋找一種不同的方法,因爲目前的一個將無法​​正常工作。從MSDN庫中,data Protocol話題說:

數據URI只爲以下元素和/或 屬性的支持。

object (images only) 
img 
input type=image 
link 
CSS declarations that accept a URL, such as background, backgroundImage, 
and so on. 

數據URI可以嵌套。

出於安全原因,數據URI僅限於下載的 資源。 無法將數據URI用於導航(對於腳本)或 來填充幀或iframe元素。

0

我的朋友建議這種方法 - 嘗試創建一個blob並使用navigator.msSaveBlob()或導航器。 msSaveOrOpenBlob()。

var csvContent=data; 
    var blob = new Blob([csvContent],{ 
     type: "text/csv;charset=utf-8;", 
    }); 

    navigator.msSaveBlob(blob, "filename.csv") 
//or navigator. msSaveOrOpenBlob(blob, "filename.csv") 

msdn.microsoft.com/en-us/library/ie/hh772331(v=vs.85).aspx

0

你可以試試這個插件 - TABLEEXPORT。JS

HTML:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
<script src="src/jquery.table2excel.js"></script> 
<body> 
<tr class="noExl"> 
    <th>#</th> 
    <th>Column heading</th> 
    <th>Column heading</th> 
    <th>Column heading</th> 
</tr> 
</body> 

的jQuery:

$("button").click(function(){ 
    $("#table2excel").table2excel({ 
    // exclude CSS class 
    exclude: ".noExl", 
    name: "Excel Document Name" 
    }); 
}); 

插件下載:http://www.jqueryscript.net/table/Export-Html-Table-To-Excel-Spreadsheet-using-jQuery-table2excel.html