2011-05-31 34 views
1

我有以下的HTML文件:如何創建HTML可以導出到CSV文件的功能?

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" dir="ltr" lang="en"> 
    <head> 
    </head> 
    <body> 
     <p align="center"><strong>Claims Search Results</strong></p> 
     <table id="ClaimsListPrint" class="printClaims"> 
     <thead> 
     <tr style="background-color: rgb(241, 241, 241);"> 
      <th style="background-color: rgb(215, 227, 235);"> 
       Member 
      </th> 
      <th style="background-color: rgb(215, 227, 235);"> 
       Date of Service 
      </th> 
      <th style="background-color: rgb(215, 227, 235);"> 
       Provider 
      </th> 
      <th style="background-color: rgb(215, 227, 235);"> 
       Claim Type 
      </th> 
      <th style="background-color: rgb(215, 227, 235);"> 
       Status 
      </th> 
      <th style="background-color: rgb(215, 227, 235);"> 
       Billed Amount 
      </th> 
      <th style="background-color: rgb(215, 227, 235);"> 
       Paid by Plan 
      </th> 
      <th style="background-color: rgb(215, 227, 235);"> 
       Member Responsiblity 
      </th> 
     </tr> 
    </thead> 
    <tbody> 
     <tr style="background-color: rgb(255, 255, 240);" class="claim"> 
      <td class="claim-member"> 
       John Sample 
      </td> 
      <td class="claim-dateOfService"> 
       02/27/2011 
      </td> 
      <td class="claim-provider"> 
       TestProv1 
      </td> 
      <td class="claim-claimType"> 
       Medical 
      </td> 
      <td class="claim-status"> 
       Processed 
      </td> 
      <td class="claim-billedAmount"> 
       $145 
      </td> 
      <td class="claim-paidByPlan"> 
       $125 
      </td> 
      <td class="claim-memberResponsibility"> 
       $25 
      </td> 
     </tr> 
     <tr style="background-color: rgb(241, 241, 241);" class="claim"> 
      <td class="claim-member"> 
       John Sample 
      </td> 
      <td class="claim-dateOfService"> 
       02/27/2011 
      </td> 
      <td class="claim-provider"> 
       TestProv1 
      </td> 
      <td class="claim-claimType"> 
       Medical 
      </td> 
      <td class="claim-status"> 
       In-Process 
      </td> 
      <td class="claim-billedAmount"> 
       - 
      </td> 
      <td class="claim-paidByPlan"> 
       - 
      </td> 
      <td class="claim-memberResponsibility"> 
       - 
      </td> 
     </tr> 
     </tbody> 
    </table> 
    </body> 
</html> 

我想最終用戶能夠在鏈路上碰杯的文件中像下面這樣:

<a id="link3" class="links" href="home.html">Export to Microsoft Excel</a> 

當他們點擊它,我希望將表格導出爲csv文件。我怎樣才能做到這一點?我可以用Javascript或PHP或其他方式做到這一點嗎?

回答

5

考慮使用jQuery插件:HTML Table to CSV

這是一個班輪:

$('#mytable').table2CSV(); 

您可以在此HTML Table To CSV demo行動看到這一點。

+0

偉大的插件,感謝您的鏈接! – Dan 2011-05-31 17:38:53

1

無論你用什麼語言來生成頁面,我都會輸出它。如果您已經有其他地方的數據,則解析HTML頁面以生成CSV是沒有意義的。頁面如何生成?

+0

我很抱歉。我想導出html表格而不是文件。 – 2011-05-31 17:36:49

0

在PHP:

  1. 加載HTML文件進入一個DOM對象結構,使用PHP的DOMDocument類。

  2. 通過DomDocument進行解析,找到您想要的元素,並將相關值提取到數組中。

  3. 使用PHP的fputsvc()函數將數組保存爲CSV文件。

0

雖然,@ p.campbell有一個很好的解決方案,它爲您提供了CSV格式,它不會給你一個CSV下載。如果您希望最終用戶以CSV格式下載,則可以使用PHP等服務器技術,並返回CSV作爲響應類型

我也同意@mrkmg。如果您有數據,請編寫一些PHP以CSV格式返回數據。

http://mysite.com/data/csv傳遞格式化的數據,並在PHP中將響應類型更改爲CSV。用戶將得到一個不錯的另存爲...對話框。我相信代碼將是這個樣子:

<?php 
// We'll be outputting a CSV 
header('Content-type: application/csv'); 

// It will be called downloaded.csv 
header('Content-Disposition: attachment; filename="downloaded.csv"'); 

// Write CSV... 
?> 
1

如果你想擁有這個CSV文件下載,那麼你可以使用PHP來修改文件頭告訴瀏覽器強制文件下載。我使用以下功能爲現有文件強制下載文件,但您可以修改它以輸出通過進行一些修改即可生成的CSV文件。或者,您可以保存一個CSV文件並使用它來下載它。

順便說一句,這不是我的原代碼。我發現它在StackOverflow的其他地方,它對我很好。

function download($filePath) { 
    if(headers_sent()) die('Headers alread sent. Download cannot initialize.'); 

    // Required for some browsers 
    if (ini_get('zlib.output_compression')) ini_set('zlib.output_compression', 'Off'); 

    // File Exists? 
    if(file_exists($filePath)){ 
     // Parse Info/Get Extension 
     $fsize = filesize($filePath); 
     $path_parts = pathinfo($filePath); 
     $ext = strtolower($path_parts["extension"]); 

     // Determine Content Type 
     switch ($ext) { 
      case "pdf": $ctype="application/pdf"; break; 
      case "exe": $ctype="application/octet-stream"; break; 
      case "zip": $ctype="application/zip"; break; 
      case "doc": $ctype="application/msword"; break; 
      case "xls": $ctype="application/vnd.ms-excel"; break; 
      case "ppt": $ctype="application/vnd.ms-powerpoint"; break; 
      case "gif": $ctype="image/gif"; break; 
      case "png": $ctype="image/png"; break; 
      case "jpeg": 
      case "jpg": $ctype="image/jpg"; break; 
      default: $ctype="application/force-download"; 
     } 

     header("Pragma: public"); // required 
     header("Expires: 0"); 
     header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
     header("Cache-Control: private",false); // required for certain browsers 
     header("Content-Type: $ctype"); 
     header("Content-Disposition: attachment; filename=\"".basename($filePath)."\";"); 
     header("Content-Transfer-Encoding: binary"); 
     header("Content-Length: ".$fsize); 
     ob_clean(); 
     flush(); 
     readfile($filePath); 

    } else { 
     die('File Not Found'); 
    } 
}