2012-01-27 128 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呼籲一刻

相關問題