Home Ask Login Register

Developers Planet

Your answer is one click away!

Biribu February 2016

Find highest column in row PHPExcel

I am triying to read all columns from an excel file but its number is variable depending on the row. For example: Row1, col2, col3, col4 Row2, col2,col3 Row3,col2,col3,col4,col5,col6

I am trying to do something like:

for ($row = 1; $row < 2; $row++){
    $rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row,
    echo print_r($rowData[0][0]);
    echo "<br>";
    $list_of_coordinates = "";
    $i = 1;
    //echo print_r($rowData[$row][$i+1]);
    while($rowData[0][$i+1] != ""){
      /*$list_of_coordinates = $list_of_coordinates . 
                             "," .
                             $rowData[0][$i+1] . 
                             "," .
    $list_of_coordinates = ltrim($list_of_coordinates, ",");
    echo $list_of_coordinates;
    echo "<br>";

But the $highestcolumn has the column number of the maximum one, in this file: BM. But some of the rows have just 3 or 4 columns. Is it possible to update this number? MY while loop is not working right now.


Mark Baker February 2016

The getHighestColumn() and getHighestDataColumn() methods accept an optional row number as an argument. If called without passing any argument, they will return the highest column number in the worksheet; but if called with a row number, they will return the highest column in the specified row.

However, as an alternative, you could use the row and column iterators, for existing cells only (see 28iterator.php in /Examples).

Post Status

Asked in February 2016
Viewed 3,797 times
Voted 4
Answered 1 times


Leave an answer

Quote of the day: live life