在php中将HTML转换为CSV?

在php中将HTML转换为CSV?,php,export-to-csv,Php,Export To Csv,我有一个像这样的html表结构 <tr style="font-weight: bold"> <td>ID</td> <td>Navn</td> <td>Adresse</td> <td>By</td> &

我有一个像这样的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>

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

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

<?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);
?>

您可以使用

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


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

因此,我将代码修改为:

$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”和“;”作为分隔符,此文件按预期加载 在MS excel 2003中

更新:

我找到了一种方法,通过添加 文件中的签名。 在PHP中,可以执行以下操作:

fwrite($fp,"\xEF\xBB\xBF");
...start writing
这3个字符(实际上是1个unicode)
迫使excel等理解
将.csv文件转换为utf8,从而在内部对其进行解码

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

Excel 2003还将其标识为SYLK文件。SYLK文件是以ID或ID_xxx开头的文件,可以读取。因此,将“ID”改为“ID”应该可以解决这个问题。也许有些excel版本不喜欢这个ID,大写/小写..太棒了,这也很有效!但它仍然让我的咒语很奇怪,我能做什么呢?它现在也对我有用了,谢谢!!但是为什么我的ÆØ在屏幕截图中与你的不一样?@Karem,可能是不同的默认语言,甚至是安装在Office中的语言包。
$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`
fwrite($fp,"\xEF\xBB\xBF");
...start writing