Convert HTML to CSV in php?

后端 未结 3 1483
春和景丽
春和景丽 2021-01-13 01:14

I have a html table structure like this;

            
                ID
                

        
相关标签:
3条回答
  • 2021-01-13 01:54

    I hate to say that it worked for me, but...it worked for me. This is the script I used.

    <?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 did get a note about the file being a SYLK file and not being able to load it in Excel. Clicking OK to this message opened the file normally. If this is your error, it is caused by this line: <td>ID</td> The SYLK file type is identified by a capital ID in the first cell of a text (CSV) file. You can prevent this message by changing it to lowercase or changing the label all together.

    This is the output I get once I have completely opened the file: Excel output

    0 讨论(0)
  • 2021-01-13 02:04

    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.
    

    so i modified the code as:

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

    but says also this:

    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`
    

    So i changed the ID,... to id,... All in all, with lower case 'id' and ';' as delimiter this loaded as expected in MS excel 2003.

    UPDATED:

    i found a way to properly load a UTF8 .csv into excel by adding the BOM signature in the file. In PHP this can be done:

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

    these 3 characters (1 unicode actually) forces excel and the likes to understand the .csv file AS utf8 and therefore decoding it internally.

    There is another solution without using the BOM but its a kind of hack and not well 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.

    0 讨论(0)
  • You can load them in an array using the 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;
    }
    

    You can then convert that array to CSV data like in your example, or just simply build the CSV string directly in the loops.

    Live example

    0 讨论(0)
提交回复
热议问题