2017-07-26 123 views
-1

我試圖在jsp頁面中導出表格以使用按鈕創建Excel表格。點擊按鈕應該提供一個對話框來保存或取消。我需要使用java或javascript的代碼。使用javascript或java將excel表格導出爲html表格

+0

StackOverflow的是解決編碼問題。這不是你的個人編碼機器。 –

+1

[將HTML表格導出爲ex​​cel - 使用jQuery或Java](https://stackoverflow.com/questions/3206775/export-html-table-to-excel-using-jquery-or-java) –

回答

1

Excel導出腳本適用於IE7 +,Firefox和Chrome

function fnExcelReport() 
{ 
    var tab_text="<table border='2px'><tr bgcolor='#87AFC6'>"; 
    var textRange; var j=0; 
    tab = document.getElementById('headerTable'); // id of table 

    for(j = 0 ; j < tab.rows.length ; j++) 
    {  
     tab_text=tab_text+tab.rows[j].innerHTML+"</tr>"; 
     //tab_text=tab_text+"</tr>"; 
    } 

    tab_text=tab_text+"</table>"; 
    tab_text= tab_text.replace(/<A[^>]*>|<\/A>/g, "");//remove if u want links in your table 
    tab_text= tab_text.replace(/<img[^>]*>/gi,""); // remove if u want images in your table 
    tab_text= tab_text.replace(/<input[^>]*>|<\/input>/gi, ""); // reomves input params 

    var ua = window.navigator.userAgent; 
    var msie = ua.indexOf("MSIE "); 

    if (msie > 0 || !!navigator.userAgent.match(/Trident.*rv\:11\./))  // If Internet Explorer 
    { 
     txtArea1.document.open("txt/html","replace"); 
     txtArea1.document.write(tab_text); 
     txtArea1.document.close(); 
     txtArea1.focus(); 
     sa=txtArea1.document.execCommand("SaveAs",true,"Say Thanks to Sumit.xls"); 
    } 
    else     //other browser not tested on IE 11 
     sa = window.open('data:application/vnd.ms-excel,' + encodeURIComponent(tab_text)); 

    return (sa); 
} 

只需創建一個空白的iframe:

<iframe id="txtArea1" style="display:none"></iframe> 

調用此函數上:

<button id="btnExport" onclick="fnExcelReport();"> EXPORT </button> 
+0

它可能重複。謝謝。 我正在彈出保存爲。我想修改以獲得文件下載彈出。 – niz