2012-01-27 151 views
1

它是关于一个for循环问题为什么在这种情况下的变量列是3?

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>"; 
} 

最高行和highestcolumn均是4在这种情况下 OUTPUT:

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

能否请您正确缩进代码?至少用一个首都开始你的句子? – PeeHaa 2012-01-27 19:28:52

+0

对不起,我急于发布它。 – user782104 2012-01-27 19:32:20

+3

这不是他的错SO恨制表符:) – Tim 2012-01-27 19:32:43

回答

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呼吁一刻

相关问题