IDNavnAdresse<...">

Convert HTML to CSV in php?

I have an 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>

I would like to convert this to a csv / excel file by php.

So each of them is a row in excel, and each of them is a cell in a row,

Please, how can this be done?

I researched and found Converting HTML tables to CSV automatically using PHP? but the answer does not work properly for me, I get all the cell results in one 'cell', so each row contains only one cell.

This is what I tried;

        $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;

Where $ table is the html above. Using a simple html dom plugin

+5
source share
3 answers

, CVS MS excel. :

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.

:

UTF8.csv excel, BOM . PHP :

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

3 ( unicode) forces excel and the likes, CSV AS utf8 , , .

, , ; file.txt ( .txt, .csv), excel, ; utf8 .

+4

, PHP DOM

$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- .

+3

, , ... . script, .

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

I received a message stating that the file is a SYLK file and cannot load it into Excel. When you click OK in this message, the file was opened normally. If this is your mistake, it is caused by this line: <td>ID</td>The SYLK file type is identified by capital IDin the first cell of the text file (CSV). You can prevent this message by changing it to lowercase or by changing the label together.

This is the result that I get after I fully opened the file: Excel output

+2
source

All Articles