2017-01-23 133 views
1

下面給出的事件被調用來將表中的數據導出到EXCEL中,代碼的工作原理類似於Chrome中的魅力。在IE和Firefox我沒有得到任何東西(文件,錯誤等)。 請幫助我前進和導出文件中的所有瀏覽器Excel導出無法在Firefox上正常工作,但在Google Chrome中正常工作

$("[id$=myButtonControlID]").click(function(e) { 
    var result = 'data:application/vnd.ms-excel,' + encodeURIComponent($('div[id$=printHead]').html()); 
    var link = document.createElement("a"); 
    link.download = "Reports"; 
    link.href = result; 
    link.click(); 
}); 
+0

請註明具體「不工作」。 – gus27

+0

我無法將指定內容導出到Excel中 – chandrahasan

+0

您應該嘗試更具體。有沒有錯誤信息?沒有檢索到文件?該文件格式錯誤? Excel打開文件時是否顯示錯誤信息?等 – gus27

回答

3

使用Firefox,你必須在link元素明確添加到DOM,然後才能執行.click()

$("[id$=myButtonControlID]").click(function(e) { 
    var result = 'data:application/vnd.ms-excel,' + encodeURIComponent($('div[id$=printHead]').html()); 
    var link = document.createElement("a"); 
    document.body.appendChild(link); // You need to add this line 
    link.download = "Reports"; 
    link.href = result; 
    link.click(); 
}); 

data:來自IE8的URI支持。但它「不能用於導航[...]」,所以我認爲它不會在<a href="...">中工作。請參閱this link

+0

謝謝,它的工作完美@ gus27 – chandrahasan

+0

IE版本中仍存在問題:10.0.9200.17414 – chandrahasan

+0

@chandrahasan IE 10是否支持data:URI? – gus27

相關問題