PHPexcel如何获取当前单元格号

PHPexcel how to get the current cell number

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

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

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

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

    }
    echo "</tr>";
}

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

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

/**
 * 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
 */

最后一个参数允许您 return 按行和列索引的数组:

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