2014-03-04 74 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>