doupafu6980
2015-07-11 06:17
浏览 400
已采纳

PHPExcel循环遍历行和列

Need help identifying weird problem that i'm facing. I did tried searching in stack overflow but didn't find any possible answer.

Here is sample program that works displaying all rows and columns on UI

<?php

date_default_timezone_set('America/Los_Angeles');
require_once 'PHPExcel-1.8/Classes/PHPExcel.php';
include 'PHPExcel-1.8/Classes/PHPExcel/IOFactory.php';

$path = 'demo.xlsx';

$sheet = $objPHPExcel->getSheet(0);
$highestRow = $sheet->getHighestRow();
$highestColumn = $sheet->getHighestColumn();
 $highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
for ($row = 2; $row <= $highestRow; ++ $row) {
    $val=array();
for ($col = 0; $col < $highestColumnIndex; ++ $col) {
   $cell = $worksheet->getCellByColumnAndRow($col, $row);
   $val[] = $cell->getValue();
 //End of For loop   
}

$Col1 = $val[0] ;
$Col2 = $val[1] ;
$Col3 = $val[2];

echo $Col1;
echo $Col2;
echo $Col3;
echo "<br>";

//End of for loop
}
?>

This program works perfectly fine printing all columns and rows for n-lenght

Problem - Now our requirement is to get values of Col1, Col2, Col3 and using mysql_query compare into database and do further action.

Minute we add anything above //End of for loop. It only iterates once and stops without throwing any php errors.

e.g.

.....

echo $Col1;
echo $Col2;
echo $Col3;
echo "<br>";

**$sql = mysql_query("select COALESCE(MAX(SrNo), 0)  AS Max_No from TABLEA where ColumnA = 1 and ColumnB = '$Col3'");
    $row = mysql_fetch_array($sql);


    echo $row["Max_No"];**

//End of for loop
}
?>

If we add above SQL the same program only iterates once and stops? It doesn't show any errors in logs or on screen.

Thanks in advance for your help!.

图片转代码服务由CSDN问答提供 功能建议

需要帮助识别我所面临的奇怪问题。 我确实试图在堆栈溢出搜索但没有找到任何可能的答案。

以下示例程序可以显示UI上的所有行和列

 &lt;?php 
 
date_default_timezone_set('America /  Los_Angeles'); 
require_once'PHPExcel-1.8 / Classes / PHPExcel.php'; 
include'PHPExcel-1.8 / Classes / PHPExcel / IOFactory.php'; 
 
 $ path ='demo.xlsx'; 
 \  n $ sheet = $ objPHPExcel-&gt; getSheet(0); 
 $ highestRow = $ sheet-&gt; getHighestRow(); 
 $ highestColumn = $ sheet-&gt; getHighestColumn(); 
 $ highestColumnIndex = PHPExcel_Cell ::  columnIndexFromString($ highestColumn); 
for($ row = 2; $ row&lt; = $ highestRow; ++ $ row){
 $ val = array(); 
for($ col = 0; $ col&lt; $  highestColumnIndex; ++ $ col){
 $ cell = $ worksheet-&gt; getCellByColumnAndRow($ col,$ row); 
 $ val [] = $ cell-&gt; getValue(); 
 // End of For  loop 
} 
 
 $ Col1 = $ val [0]; 
 $ Col2 = $ val [1]; 
 $ Col3 = $ val [2]; 
 
echo $ Col1; 
echo $ Col2  ; 
echo $ Col3; 
echo“&lt; br&gt;”; 
 
 // for循环结束
} 
?&gt; 
   
 
 

该程序可以完美地打印n-le的所有列和行 nght

问题 - 现在我们的要求是获取Col1,Col2,Col3的值并使用mysql_query比较到数据库并执行进一步操作。

我们在上面添加任何内容// for for循环结束。 它只迭代一次并停止而不会抛出任何php错误。

e.g。

.....

  echo $ Col1; 
echo $ Col2; 
echo $ Col3; 
echo“&lt; br&gt;  ;“; 
 
 
 ** $ sql = mysql_query(”选择COALESCE(MAX(SrNo),0)来自TABLEA的AS Max_No,其中ColumnA = 1,ColumnB ='$ Col3'“); 
 $ row = mysql_fetch_array(  $ sql); 
 
 
 echo $ row [“Max_No”]; ** 
 
 // for循环结束
} 
?&gt; 
   \  n 
 

如果我们添加上面的SQL,相同的程序只迭代一次并停止? 它不会在日志或屏幕上显示任何错误。

提前感谢您的帮助!

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • doukoumi3389 2015-07-11 10:39
    已采纳

    As you're using the same variable $row for the row number in the Excel iteration and for the result of your select query, it's not surprising that you're running into problems.....

    The integer value that holds the Excel row number is being overwritten by the array that you get from your SQL query, and then you're trying to use that result array as the next Excel row number

    Solution: Use a different variable for these two elements.

    $rowData = mysql_fetch_array($sql);
    echo $rowData["Max_No"];**
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dou47278 2016-10-04 22:36

    If you try to iterate with for ($col = 2; $col <= $highestColumn; ++ $col){...} it will work for columns from A to Z, but it fails pass the Z (Ex. iterate between 'A' to 'AB').

    In order to iterate pass 'Z', you need to convert the column to integer, increment, compare, and get it as string again:

    $MAX_COL = $sheet->getHighestDataColumn();
    $MAX_COL_INDEX = PHPExcel_Cell::columnIndexFromString($MAX_COL);
        for($index=0 ; $index <= $MAX_COL_INDEX ; $index++){
        $col = PHPExcel_Cell::stringFromColumnIndex($index);
    
        // do something, like set the column width...
        $sheet->getColumnDimension($col)->setAutoSize(TRUE);
    }
    

    With this, you easy iterate pass the 'Z' column.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题