1

如何获取工作表的一部分,例如,只有 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;
}

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

4

1 回答 1

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
 */
于 2013-09-18T14:18:54.317 回答