简体   繁体   中英

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,
                                    NULL,
                                    TRUE,
                                    FALSE);
    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] . 
                             "," .
                             $rowData[0][$i];
      */
      $i+2;
    }
    $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.

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 ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM