问题描述
我有这样的html表结构;
I have a html table structure like this;
<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文件.
I would like to convert this into a csv/excel file by php.
因此,每个在excel中都是一行,并且每个都是该行中的一个单元格,
So each is a row in excel, and each is a cell in the row,
请怎么做?
我研究并发现了将HTML表转换为CSV自动使用PHP?,但是答案对我来说无法正常工作,因为我将所有单元格都存储在一个单元格"中,所以每一行只有一个单元格.
I have researched and found Converting HTML Table to a CSV automatically using PHP? but the answer does not work properly for me, Im getting all the cell results in one 'cell', so each row only have one cell.
这是我尝试过的;
$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插件
Where $table is the html above. Using simple html dom plugin
推荐答案
似乎生成的CVS在某些MS excel版本中存在问题.根据此页面:
it seems that produced CVS has problems with some MS excel version.according to this page:
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'和';'作为分隔符,按预期加载在MS excel 2003中.
So i changed the ID,... to id,...All in all, with lower case 'id' and ';' as delimiter this loaded as expectedin MS excel 2003.
已更新:
我找到了一种方法,可以通过添加以下代码将UTF8 .csv正确加载到excel中:文件中的 BOM 签名.在PHP中,可以这样做:
i found a way to properly load a UTF8 .csv into excel by adding theBOM signature in the file.In PHP this can be done:
fwrite($fp,"\xEF\xBB\xBF");
...start writing
这3个字符(实际上是1个unicode)forces excel and the likes
可以理解.csv文件作为utf8,因此在内部对其进行解码.
these 3 characters (1 unicode actually) forces excel and the likes
to understandthe .csv file AS utf8 and therefore decoding it internally.
还有另一种不使用BOM的解决方案,但它是一种黑客行为,而不是经过良好测试;只需将您的文件创建为 file.txt (请注意,.txt,而不是.csv),强制excel 询问您所需的编码;您选择utf8并完成.
There is another solution without using the BOM but its a kind of hack and notwell tested; just create your file as file.txt (notice the .txt, not .csv),forcing excel to ask you about the encoding you want; you choose utf8 and done.
这篇关于在PHP中将HTML转换为CSV?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!