PHPExcel:阅读工作表的一部分

时间:2013-09-18 14:09:05

标签: php phpexcel

如何获取工作表的一部分,例如,只有1到10或20到30等行? 现在我用这个:

public function GetArray($from = false, $to = false) {
    $rowIterator = $this->objPHPExcel->getRowIterator();
    foreach ($rowIterator as $id=>$row) {

        if($from)
            if($id < $from) continue;

        if($to)
            if($id > $to) break;

        $cellIterator = $row->getCellIterator();
        foreach ($cellIterator as $cell) {
            $arResult[$id][] = trim($cell->getCalculatedValue());
        }
    }
    return $arResult;
}

但我认为这不是最佳解决方案。

1 个答案:

答案 0 :(得分:2)

最简单的方法是使用Worksheet的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
 */