【问题标题】:How can I create functionality where html can be exported to a CSV file?如何创建可以将 html 导出到 CSV 文件的功能?
【发布时间】:2023-03-13 07:46:01
【问题描述】:

我有以下 html 文件:

<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 或其他什么来做到这一点吗?

【问题讨论】:

    标签: javascript php csv html-table export-to-csv


    【解决方案1】:

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

    这是一个单行:

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

    您可以在HTML Table To CSV demo 上看到这一点。

    【讨论】:

      【解决方案2】:

      无论您使用什么语言来生成页面,我都会使用它来导出它。如果您已经在其他地方拥有数据,那么解析 HTML 页面以生成 CSV 是没有意义的。页面是如何生成的?

      【讨论】:

      • 我很抱歉。我想导出 html 表而不是文件。
      【解决方案3】:

      如果您想下载此 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'); 
          }
      }
      

      【讨论】:

        【解决方案4】:

        在 PHP 中:

        1. 使用 PHP 的 DOMDocument 类将 HTML 文件加载到 DOM 对象结构中。

        2. 通过 DomDocument 进行解析,找到你想要的元素,并将相关值提取到一个数组中。

        3. 使用 PHP 的 fputsvc() 函数将数组保存为 CSV 文件。

        【讨论】:

          【解决方案5】:

          虽然@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...
          ?> 
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2014-04-11
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2019-08-19
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多