为什么在这种情况下变量列是3?

时间:2012-01-27 19:27:02

标签: php for-loop

它是关于for循环问题

for ($row = 1; $row <= $highestRow; $row++) {
    echo "<tr>";
    for ($column = 0; $column < $highestColumn; $column++) {
        $val = $sheet->getCellByColumnAndRow($column, $row)->getValue();
        echo "<td>";
        //echo $val;
        echo $row,$column;
        echo "</td>";
  }
  echo "</tr>";
}

在这种情况下,最高行和最高列都是4 输出:

test.xlsx

Import from spreadsheet files

Unname coloum 0  Unname coloum 1  Unname coloum 2  Unname coloum 3  
    13                   13               13                13
    23                   23               23                23
    33                   33               33                33
    43                   43               43                43

列的值是3并且永远不会改变(它应该至少为0?),我之前没有初始化它,这很奇怪

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <link rel="stylesheet" type="text/css" href="../plugin/easyui/themes/default/easyui.css">
    <link rel="stylesheet" type="text/css" href="../plugin/easyui/themes/icon.css">
    <link rel="stylesheet" type="text/css" href="../style/form1.css" />
    <script type="text/javascript" src="../plugin/easyui/jquery-1.7.1.min.js"></script>
    <script type="text/javascript" src="../plugin/easyui/jquery.easyui.min.js"></script>
    </script>
</head>
<body>


<?
session_start();

$file=$_POST['excel'];

include '../plugin/excel/Classes/PHPExcel/IOFactory.php';
$reader = PHPExcel_IOFactory::createReader('Excel2007');
$PHPExcel = $reader->load($file);
$sheet = $PHPExcel->getSheet(0);
$highestRow = $sheet->getHighestRow();
$highestColumn = PHPExcel_Cell::columnIndexFromString($sheet->getHighestColumn());
?>
<div id="stylized" class="view">
<h1><?echo $file;?></h1> 
<p>Import from spreadsheet files</p>
<table id="tt" class="easyui-datagrid" style="width:auto;height:auto;" >
<thead>
<tr>
<?
for ($head = 0; $head < $highestColumn; $head++){
echo "<th field='col' $head> Unname coloum $head </th>";
}
?></tr>
</thead>
<tbody>

<?

for ($row = 1; $row <= $highestRow; $row++) {
    echo "<tr>";
    for ($column = 0; $column < $highestColumn; $column++) {
    $val = $sheet->getCellByColumnAndRow($column, $row)->getValue();
    echo "<td>";
    //echo $val;
    echo "$row,$column";
    echo "</td>";
  }
  echo "</tr>";
}


?>
</tbody>
</table>
<div class="spacer"></div>
</div>
</body>
</html>

感谢

1 个答案:

答案 0 :(得分:0)

评论太大了:

尝试更改

$highestRow = $sheet->getHighestRow(); 
$highestColumn = PHPExcel_Cell::columnIndexFromString($sheet->getHighestColumn()); 

$highestRow = $sheet->getHighestRow(); 
$highestColumn = $sheet->getHighestColumn(); 
$highestColumn++;

for ($head = 0; $head < $highestColumn; $head++){  
    echo "<th field='col' $head> Unname coloum $head </th>";  
}

for ($head = 'A'; $head !== $highestColumn; $head++){  
    echo "<th field='col' $head> Unname coloum $head </th>";  
}

for ($column = 0; $column < $highestColumn; $column++) {        
    $val = $sheet->getCellByColumnAndRow($column, $row)->getValue();        
    echo "<td>";        
    //echo $val;        
    echo "$row,$column";        
    echo "</td>";        
} 

for ($column = 'A'; $column !== $highestColumn; $column++) {     
    // $val = $sheet->getCellByColumnAndRow($column, $row)->getValue();     
    echo "<td>";     
    //echo $val;     
    echo $row , $column;     
    echo "</td>";     
}  

请注意,我暂时注释掉了getCell调用