表数据使用php解析并保存excel表中的表数据

时间:2012-04-17 06:41:21

标签: php html excel

我有一个文件“import.php”,其中html数据写在表标签下。现在我想解析该数据并将该数据保存在Excel工作表中。格式未提及,第一个tr包含标题,然后是数据

<html>
  <body>
  <table>
  <tr>
  <th>Name</th>
  <th>Email</th>
  <th>Addr</th>
  <th>City</th>
  </tr>

  <tr>
  <td>Jack</td>
  <td>a@b.com</td>
  <td>xyz Road</td>
  <td>LOS ANGELES</td>
  </tr>

  <tr>
  <td>Sam</td>
  <td>sam@b.com</td>
  <td>pr Road</td>
  <td>TUSTIN</td>
  </tr>
    </table>
  </body>
  </html>

1 个答案:

答案 0 :(得分:1)

也许你最好看看这个:http://phpexcel.codeplex.com/

并且:http://www.easyxls.com/

另一个技巧是将您的数据保存为CSV文件:http://www.homeandlearn.co.uk/php/php10p6.html

更新:

没有更简单的方法可以直接将数据保存到Excel文件中,但保存为CSV。试试这段代码:

<?php 
$data = array( array("firstname" => "Mary", "lastname" => "Johnson", "age" => 25), 
    array("firstname" => "Amanda", "lastname" => "Miller", "age" => 18), 
    array("firstname" => "James", "lastname" => "Brown", "age" => 31), 
    array("firstname" => "Patricia", "lastname" => "Williams", "age" => 7), 
    array("firstname" => "Michael", "lastname" => "Davis", "age" => 43), 
    array("firstname" => "Sarah", "lastname" => "Miller", "age" => 24), 
    array("firstname" => "Patrick", "lastname" => "Miller", "age" => 27) ); 

    # filename for download 
    $filename = "website_data_" . date('Ymd') . ".xls"; 
    header("Content-Disposition: attachment; filename=\"$filename\""); 
    header("Content-Type: application/vnd.ms-excel");
    $flag = false; 
    foreach($data as $row) 
    { 
        if(!$flag) 
        { # display field/column names as first row echo 
            implode("\t", array_keys($row)) . "\r\n"; $flag = true; 
        } 
        array_walk($row, 'cleanData');
        print implode("\t", array_values($row)) . "\r\n"; 
    }


    function cleanData(&$str) 
    { 
        $str = preg_replace("/\t/", "\\t", $str); 
        $str = preg_replace("/\r?\n/", "\\n", $str); 
        if(strstr($str, '"')) 
            $str = '"' . str_replace('"', '""', $str) . '"';
    }
?>

关于html标签中值的分离,您可以从import.php中读取您的html代码,删除所有不必要的标签,将分隔符放在每个标签或标签的末尾,然后将您的值放入单个维度阵列:

<?php 
$htmldata = "";
$htmldata .= "<html>";
$htmldata .= "<body>";
$htmldata .= "<tr>";
$htmldata .= "<th>header1</th>";
$htmldata .= "<th>header2</th>";
$htmldata .= "<th>header3</th>";
$htmldata .= "<th>header4</th>";
$htmldata .= "</tr>";
$htmldata .= "<tr>";
$htmldata .= "<td>data1</td>";
$htmldata .= "<td>data2</td>";
$htmldata .= "<td>data3</td>";
$htmldata .= "<td>data4</td>";
$htmldata .= "</tr>";
$htmldata .= "</body>";
$htmldata .= "</html>";

//Remove the unecessary tags like <html>, </html>, <body>, </body>, <th>, </th>, <td>, </td>
$searchfor = array("<html>", "</html>", "<body>", "</body>", "<tr>", "</tr>", "<th>", "</th>", "<td>", "</td>");
$replacewith = array("", "", "", "", "", "", "", "**SEPERATOR**", "", "**SEPERATOR**"); // Replace </th> & </td> with **SEPERATOR** text
$htmldata = str_replace($searchfor, $replacewith, $htmldata);

$values = explode("**SEPERATOR**", $htmldata); ;
print_r($values);

?>

数组$ values的前4个值包含您的标头值。希望它有所帮助...