在PHPExcel行中找到最高的列 [英] Find highest column in row PHPExcel

查看:176
本文介绍了在PHPExcel行中找到最高的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从excel文件中读取所有列,但其数量根据行而变化.例如: 第1列,第2列,第3列,第4列 第2列,第2列,第3列 第3行,col2,col3,col4,col5,col6

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

但是$ highestcolumn的列号最大,在此文件中:BM.但是某些行只有3或4列.是否可以更新此号码?我的while循环目前无法正常工作.

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.

推荐答案

getHighestColumn()getHighestDataColumn()方法接受可选的行号作为参数.如果在不传递任何参数的情况下调用它们,则它们将返回工作表中的最高列号;但是如果使用行号进行调用,它们将返回指定行中的最高列.

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.

但是,作为替代,您可以仅对现有单元格使用行和列迭代器(请参见/Examples中的28iterator.php).

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

这篇关于在PHPExcel行中找到最高的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆