【问题标题】:Convert HTML to CSV in php?在 php 中将 HTML 转换为 CSV?
【发布时间】:2012-06-05 21:20:57
【问题描述】:

我有一个这样的html表结构;

            <tr style="font-weight: bold">
                <td>ID</td>
                <td>Navn</td>

                <td>Adresse</td>
                <td>By</td>
                <td>Post nr</td>
                <td>E-mail</td>
                <td>Telefon</td>
                <td>Status og dato</td>
                <td>Dropdown info</td>
                <td>Produkt info</td>
                <td>Buydate</td>
                <td>Ref nr. (3 første cifre)</td>
            </tr>
                    <tr>
                <td>40563</td>
                <td>Firstname Lastname</td>

                <td>Address</td>
                <td>Copen</td>
                <td>2100</td>
                <td>ff@hotmail.com</td>
                <td>123123</td>
                <td>Ikke indløst</td>
                <td>EEE-BBB</td>
</tr>

我想通过 php 将其转换为 csv/excel 文件。

所以在excel中每个都是一行,每个是行中的一个单元格,

请问这是怎么做到的?

我研究并找到了Converting HTML Table to a CSV automatically using PHP?,但答案对我来说不起作用,我将所有单元格结果都放在一个“单元格”中,所以每一行只有一个单元格。

这是我尝试过的;

        $html = str_get_html($table);



        header('Content-type: application/ms-excel');
        header('Content-Disposition: attachment; filename=sample.csv');

        $fp = fopen("php://output", "w");

        foreach($html->find('tr') as $element)
        {
            $td = array();
            foreach( $element->find('td') as $row)  
            {
                $td [] = $row->plaintext;
            }
            fputcsv($fp, $td);
        }


        fclose($fp);
        exit;

其中 $table 是上面的 html。使用简单的 html dom 插件

【问题讨论】:

    标签: php export-to-csv


    【解决方案1】:

    生成的 CVS 似乎与某些 MS excel 版本存在问题。 根据this页面:

    However, certain Microsoft programs (I'm looking at you, Access 97), 
    will fail to recognize the CSV properly unless each line ends with \r\n.
    

    所以我将代码修改为:

    $td = array();
    foreach( $element->find('td') as $row) {
       $td[] = $row->plaintext;
    }
    fwrite($fp,implode(";",$td)."\r\n");
    

    但也这样说:

    Secondly, if the first column heading / value of the CSV file begins with 
    `uppercase `ID, certain Microsoft programs (ahem, Excel 2007) will interpret 
    the file `as` being in the` SYLK format rather than CSV`
    

    所以我将 ID,... 更改为 id,... 总而言之,小写的'id'和';'作为定界符,按预期加载 在 MS excel 2003 中。

    更新:

    我找到了一种将 UTF8 .csv 正确加载到 excel 中的方法,方法是添加 BOM 文件中的签名。 在 PHP 中可以这样做:

    fwrite($fp,"\xEF\xBB\xBF");
    ...start writing
    

    这 3 个字符(实际上是 1 个 unicode)forces excel and the likes 了解 .csv 文件为 utf8,因此在内部对其进行解码。

    还有另一种不使用 BOM 的解决方案,但它是一种 hack 而不是 测试良好;只需将您的文件创建为 file.txt(注意 .txt,而不是 .csv), 强制excel询问你想要的编码;你选择 utf8 就完成了。

    【讨论】:

    • Excel 2003 也将其识别为 SYLK 文件。
    • SYLK 文件是以 ID 或 ID_xxx 开头的文件,因为可以读取here。因此,从“ID”更改为“id”应该可以解决它。也许有些excel版本不喜欢那个ID,大写/小写..
    • 太棒了,这也有效!但它仍然让我的 ÆØÅ 字符很奇怪,我该怎么办??
    【解决方案2】:

    您可以使用 PHP DOM classes 将它们加载到数组中

    $data = array();
    $doc = new DOMDocument();
    $doc->loadHTML($html);
    $rows = $doc->getElementsByTagName('tr');
    foreach($rows as $row) {
        $values = array();
        foreach($row->childNodes as $cell) {
            $values[] = $cell->textContent;
        }
        $data[] = $values;
    }
    

    然后您可以像在您的示例中那样将该数组转换为 CSV 数据,或者直接在循环中构建 CSV 字符串。

    Live example

    【讨论】:

      【解决方案3】:

      我不想说它对我有用,但是……它对我有用。这是我使用的脚本。

      <?php
          include('simple_html_dom.php');
      
          $table = '<tr style="font-weight: bold">
                      <td>ID</td>
                      <td>Navn</td>
                      <td>Adresse</td>
                      <td>By</td>
                      <td>Post nr</td>
                      <td>E-mail</td>
                      <td>Telefon</td>
                      <td>Status og dato</td>
                      <td>Dropdown info</td>
                      <td>Produkt info</td>
                      <td>Buydate</td>
                      <td>Ref nr. (3 første cifre)</td>
                  </tr>
                          <tr>
                      <td>40563</td>
                      <td>Firstname Lastname</td>
      
                      <td>Address</td>
                      <td>Copen</td>
                      <td>2100</td>
                      <td>ff@hotmail.com</td>
                      <td>123123</td>
                      <td>Ikke indløst</td>
                      <td>EEE-BBB</td>
      </tr>
      ';
              $html = str_get_html($table);
      
              header('Content-type: application/ms-excel');
              header('Content-Disposition: attachment; filename=sample.csv');
      
              $fp = fopen("php://output", "w");
      
              foreach($html->find('tr') as $element)
              {
                  $td = array();
                  foreach( $element->find('td') as $row)  
                  {
                      $td [] = $row->plaintext;
                  }
                  fputcsv($fp, $td);
              }
      
              fclose($fp);
      ?>
      

      我确实收到了关于该文件是 SYLK 文件并且无法在 Excel 中加载它的说明。单击此消息的“确定”会正常打开文件。如果这是您的错误,则由以下行引起:&lt;td&gt;ID&lt;/td&gt; SYLK 文件类型由文本 (CSV) 文件的第一个单元格中的大写 ID 标识。您可以通过将其更改为小写或同时更改标签来阻止此消息。

      这是我完全打开文件后得到的输出:

      【讨论】:

      • 它现在也适用于我,谢谢!!但是为什么我的 ÆØÅ 在屏幕截图中与你的不一样?
      • @Karem,可能是不同的语言默认设置,甚至是 Office 中安装的语言包。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-19
      • 1970-01-01
      • 2016-02-11
      • 2019-07-07
      相关资源
      最近更新 更多