2014-03-04 76 views
3

我试图将HTML表格内容导出为Excel。我看到this solution这工作,但并不像我预期(因为我不能选择哪些列复制,它不与大表工作)。

另一种解决方案是通过js复制并手动粘贴到excel文件,但这种方式并不奏效,而且我并不喜欢这种方法。
将HTML表格导出到Excel(XLS或CSV)

不久我想要的是导出表格的自定义视图,而不是所有列。向你展示我的意思的例子:

这是正常的表视图:

enter image description here

,这里是怎么样我想在Excel中显示:

enter image description here

但是由于我有隐藏字段,第一种方法不起作用:

enter image description here

我想一个客户端,跨浏览器,解决方法/溶液,考虑到我在表中具有约2500行。

+1

我认为如果你在服务器端建立excel表格,那么你可以适当地自定义它。 –

+0

你会使用任何框架如支柱,弹簧等 –

+0

@KanhuBhol其实,我坚持古典ASP! – user2517028

回答

1

我做了一些非常相似的东西,每天在我的办公室使用它,遵循网络上的教程。 可以使用this template为PHP:

<? 
    $filename="sheet.xls"; 
    header ("Content-Type: application/vnd.ms-excel"); 
    header ("Content-Disposition: inline; filename=$filename"); 
?> 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
<html lang=it><head> 
<title>Titolo</title></head> 
<body> 
<table border="1"> 
<? 
for ($i=1;$i < 11; $i++) 
{ 
    echo "<tr>"; 
    for ($j=1; $j<11;$j++) 
    { 
     $a = $i * $j; 
     echo "<td>$a</td>"; 
    } 
    echo "</tr>"; 
} 
?> 
</table> 
</body></html> 

你可以写与JS类似的东西,只是生成HTML标签一个简单的表格,并务必写正确的代码在标题中。

1
Excel Export Script works on IE7+ , Firefox and 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); 
          } 

    Just Create a blank iframe 

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

    Call this function on 

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