简体   繁体   中英

Return row in loop with phpspreadsheet

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();

    }

}

This is returning every single row. I just want one row to be displayed. In this case Row 'J'.

Updated code:

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);

I have this code, from one of the answers below. I still get a http error 500 in the browser.

Work with an easy approach.

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

If you want only column 'J' read in var_dump of $row find the $key of column 'J'.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM