2017-07-26 141 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