Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/236.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将Excel行作为数组读取:PHPExcel是否具有fgetcsv()等效项?_Php_Excel_Phpexcel - Fatal编程技术网

将Excel行作为数组读取:PHPExcel是否具有fgetcsv()等效项?

将Excel行作为数组读取:PHPExcel是否具有fgetcsv()等效项?,php,excel,phpexcel,Php,Excel,Phpexcel,我正在用PHPExcel库修改一些CSV读取代码,在遍历数据表时遇到了问题 我的代码: $filetype = PHPExcel_IOFactory::identify($filename); $objReader = PHPExcel_IOFactory::createReader($filetype); $xl = $objReader->load($filename); $objWorksheet = $xl->getActiveSheet(); foreach($objWo

我正在用PHPExcel库修改一些CSV读取代码,在遍历数据表时遇到了问题

我的代码:

$filetype = PHPExcel_IOFactory::identify($filename);
$objReader = PHPExcel_IOFactory::createReader($filetype);
$xl = $objReader->load($filename);
$objWorksheet = $xl->getActiveSheet();

foreach($objWorksheet->getRowIterator() as  $rowIndex => $row){
    print_r($row);
}
我不希望在迭代之前将整个工作表转换为数组,因为我的一些Excel文件可能非常大,我希望避免大量内存使用

我所期望的:For
$row
返回该行中所有单元格的数组

我得到的:
$row
是一个PHPExcel对象,该行中有单元格


PHPExcel中是否有一个
fgetcsv()
等价物,可以让我获得一行中所有单元格的数组?

是的,当您使用行迭代器时,它将依次为每行返回一个
PHPExcel\u工作表\u行
对象;然后可以使用单元格迭代器(请参见
/Example/28iterator.php
)迭代该行对象(
$row

或者,您可以将该行转换为数组,而不是使用单元格迭代器

$lastColumn = $objWorksheet->getHighestColumn();
foreach($objWorksheet->getRowIterator() as $rowIndex => $row) {
    // Convert the cell data from this row to an array of values
    //    just as though you'd retrieved it using fgetcsv()
    $array = $objWorksheet->rangeToArray('A'.$rowIndex.':'.$lastColumn.$rowIndex);
}

由于
$array
只是一行中的一个单元格数组,所以它只会占用使用
fgetcsv()
调用检索到的行所占用的内存。

太棒了,第二个版本正是我想要得到的!
$lastColumn = $objWorksheet->getHighestColumn();
foreach($objWorksheet->getRowIterator() as $rowIndex => $row) {
    // Convert the cell data from this row to an array of values
    //    just as though you'd retrieved it using fgetcsv()
    $array = $objWorksheet->rangeToArray('A'.$rowIndex.':'.$lastColumn.$rowIndex);
}