2012-05-31 9 views
5

私はこのようなhtmlテーブル構造を持っています。PHPでHTMLをCSVに変換しますか?

  <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>[email protected]</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?が見つかりましたが、答えが正しく機能しません。すべてのセルの結果が1つの 'セル'になるため、各行には1つのセルしかありません。

これは私が試したことです。

 $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プラグインを使用して

+0

@Andy updated q – Karem

答えて

4

生産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 UPDATED

期待どおりにロードこのデリミタとして:

を私が正しくファイルに BOM署名を追加することにより、ExcelにUTF8の.CSVをロードする方法を発見しました。PHPで これを行うことができます。

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

これらの3つの文字(実際には1つのユニコード)forces excel and the likesは にAS UTF8、したがって、内部的にそれをデコードする.csvファイルを理解すること。

BOMを使用せずにハックの一種であるもう1つの解決方法がありますが、 は十分にテストされていません。 file.txt(.txtには.csvではなく)があります。 forcingを実行すると、にエンコードについての質問が表示されます。あなたはutf8を選択して完了します。

+0

Excel 2003は、それをSYLKファイルとしても識別しました。 – Andy

+0

SYLKファイルはIDまたはID_xxxで始まり、[ここ](http://support.microsoft.com/kb/215591)を読むことができるファイルです。したがって、 'ID'から 'id'に変更すると修正されるはずです。おそらく、そのID、大文字小文字のようないくつかの優れたバージョンはありません。 –

+0

素晴らしいですが、これも機能しています!しかし、それは私のÆØÅの奇妙な奇妙な、私は何ができますか? – Karem

2

私はそれが私のために働いたと言いたいと思うが、それは私のために働いた。これは私が使ったスクリプトです。

<?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>[email protected]</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で読み込めないことに注意しました。このメッセージにOKをクリックすると、ファイルが正常に開かれました。これがあなたのエラーである場合、それはこの行によって引き起こされます:<td>ID</td> SYLKファイルタイプは、テキスト(CSV)ファイルの最初のセルにある大文字のIDによって識別されます。このメッセージは、小文字に変更するか、ラベルをまとめて変更することで防止できます。 Excel output

+0

あまりにも感謝しています!しかし、なぜ私のÆØÅはあなたのスクリーンショットのように正しく表示されませんか? – Karem

+0

@Karem、おそらく異なる言語のデフォルト、またはOfficeにインストールされている言語パックさえあります。 – Andy

3

はあなたがその後、CSVデータにその配列を変換することができ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文字列を構築してください。

Live example

+0

組み込みのPHPクラスを使用するための+1! – Drewness

関連する問題