PHPexcel如何获取当前的单元格号

时间:2017-11-14 07:45:56

标签: php phpexcel

我在rangeToArray()库上使用PHPexcel显示一组数据 工作正常。每个数据都是唯一的,所以我需要为每个数据运行另一个函数,为此,我必须提供当前的单元格号来运行我的函数。

$mySet = $objPHPExcel->getActiveSheet()->rangeToArray('A1:J31');

foreach ($mySet as $row) {
    echo "<tr>";
    foreach ($row as $data) {

        echo "<td>".$data."</td>";

    }
    echo "</tr>";
}

我的问题是如何获得每次数据迭代的当前单元格数?

1 个答案:

答案 0 :(得分:1)

默认情况下,rangeToArray()返回一个简单的枚举数组;但是如果你看一下方法的论据

/**
 * Create array from a range of cells
 *
 * @param string $pRange Range of cells (i.e. "A1:B10"), or just one cell (i.e. "A1")
 * @param mixed $nullValue Value returned in the array entry if a cell doesn't exist
 * @param boolean $calculateFormulas Should formulas be calculated?
 * @param boolean $formatData Should formatting be applied to cell values?
 * @param boolean $returnCellRef False - Return a simple array of rows and columns indexed by number counting from zero
 *                               True - Return rows and columns indexed by their actual row and column IDs
 * @return array
 */

最后一个参数允许您返回由行和列索引的数组:

$mySet = $objPHPExcel->getActiveSheet()->rangeToArray('A1:J31', null, true, true, true);

foreach ($mySet as $rowNumber => $row) {
    echo "<tr>";
    foreach ($row as $columnAddress => $data) {

        echo "<td>".$columnAddress.$rowNumber.' = '.$data."</td>";

    }
    echo "</tr>";
}