循环访问excel文档中的记录

循环访问excel文档中的记录

问题描述:

我需要遍历excel文件中的一些记录并将它们输出为HTML表格的标题,但我不知道什么是最好的方法,因为我是PHP新手。循环访问excel文档中的记录

每列标题开始为标题后面的索引,所以你有:

国家1,COUNTRY2,COUNTRY3 ....通过对Country50。

我想有更自动化的方式来取回这些值,而不是固守

$结果=“$国家1”。

每个

还必须包含国家,也应转换成一个阵列,即其他的事情,所以他们并不需要,如果是这样的话要打印的ID,所以结果可能是

<th id="Country1">Value of Country1</th> 
<th id="Country2">Value of Country2</th> 
<th id="Country3">Value of Country3</th> 
<th id="Country8">Value of Country8</th> 
<th id="Country24">Value of Country24</th> 
<th id="Country30">Value of Country30</th> 

什么是最好的“代码灯”方法来做到这一点?

问候!

+0

这是一个真正的Excel文件或CSV文件?你知道如何阅读该文件吗?或者你是否需要帮助将数据转换为HTML表格?另外,请解释您为什么必须拥有这些ID。您可以在没有它们的情况下使用CSS和JavaScript访问各个节点。 – Gordon 2011-02-01 10:08:18

这样的事情应该工作:

<?php 
$handle = @fopen("/tmp/myfile.csv", "r"); 
if ($handle) { 

    // first line should be the header, we make it an array of strings 
    if (($buffer = fgets($handle, 4096)) !== false) 
     $headers[] = explode(",", $buffer); 
    else 
     echo "Error: empty file...\n"; 

    // second line should be the values, for each line we output the xml markups 
    while (($buffer = fgets($handle, 4096)) !== false) 
    { 
     $values[] = explode(",", $buffer); 

     if(count($headers) != count(values)) 
      echo "Error: the 2 lines do not have same number of columns...\n"; 
     else 
     { 
      for($i = 0 ; $i < count($headers) ; $i++) 
       echo "<th id='".$headers[$i]."'>".$values[$i]."</th>\n"; 
     } 
    } 
    else 
     echo "Error: second line empty...\n"; 

    if (!feof($handle)) 
     echo "Erreur: fgets() failed...\n"; 

    fclose($handle); 
} 
?>