使用phpspreadsheet返回循环中的行 [英] Return row in loop with phpspreadsheet
本文介绍了使用phpspreadsheet返回循环中的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一张Excel工作表,我只想遍历一行.现在我明白了:
i have an excel sheet and i want to loop through just one single row only. Right now i got this:
$reader->setReadDataOnly(TRUE);
$spreadsheet = $reader->load("KEVIN Call Monkey ALLE BEDRIJVEN 29-10-18 Export_bedrijven_20181030_105432.xlsx");
$worksheet = $spreadsheet->getActiveSheet();
foreach ($worksheet->getRowIterator() as $row) {
$cellIterator = $row->getCellIterator();
$cellIterator->setIterateOnlyExistingCells(FALSE); // This loops through all cells,
// even if a cell value is not set.
// By default, only cells that have a value
// set will be iterated.
foreach ($cellIterator as $cell) {
echo
$cell->getValue();
}
}
这将返回每一行.我只希望显示一行.在这种情况下,行"J".
This is returning every single row. I just want one row to be displayed. In this case Row 'J'.
更新的代码:
require 'vendor/autoload.php';
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use PhpOffice\PhpSpreadsheet\Reader\IReader;
use \PhpOffice\PhpSpreadsheet\IOFactory;
$inputFileName = 'KEVIN Call Monkey ALLE BEDRIJVEN 29-10-18 Export_bedrijven_20181030_105432.xlsx';
$inputFileType = 'Xlsx';
/** Create a new Reader of the type defined in $inputFileType **/
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
/** Load $inputFileName to a Spreadsheet Object **/
$spreadsheet = $reader->load($inputFileName);
$worksheet = $spreadsheet->getActiveSheet();
$rows = $worksheet->toArray();
var_dump($rows);
我有下面的答案之一中的这段代码.我仍然在浏览器中看到http错误500.
I have this code, from one of the answers below. I still get a http error 500 in the browser.
推荐答案
使用一种简单的方法.
在数组中进行转换,然后像这样读取所有键和值
Transform in array and then read all key and value like this
$inputFileName = FILE PATH;
$inputFileType = 'Xlsx';
/** Create a new Reader of the type defined in $inputFileType **/
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
/** Load $inputFileName to a Spreadsheet Object **/
$spreadsheet = $reader->load($inputFileName);
$worksheet = $spreadsheet->getActiveSheet();
$rows = $worksheet->toArray();
foreach($rows as $key => $value) {
// key is the row count(starts from 0)
// array of values
foreach($value as $iter => $column_value) {
//$column_value the value of row
};
};
如果只希望在$ row的var_dump中读取列'J',则找到列'J'的$ key.
If you want only column 'J' read in var_dump of $row find the $key of column 'J'.
这篇关于使用phpspreadsheet返回循环中的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文