Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHPSpreadsheet: how to get the number of loaded rows?

How do I find out how many rows I have loaded using PHPSpreadsheet\Reader\Xlsx::load() method?

I cannot find methods (or properties) for getting row count in Spreadsheet or Worksheet classes either.

BTW I am using following code:

$filename = 'test.xlsx';
$inputFileType = \PhpOffice\PhpSpreadsheet\IOFactory::identify($filename);
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);

$reader->setReadDataOnly(true);  
$reader->setLoadSheetsOnly($sheet);  

$this->spreadsheet = $reader->load($filename);
$this->worksheet = $this->spreadsheet->getActiveSheet();
like image 575
Pontiac_CZ Avatar asked Feb 14 '18 14:02

Pontiac_CZ


People also ask

How do I count the number of rows in Excel using PHP?

$objPHPExcel->setActiveSheetIndex(0)->getHighestDataRow(); Use the getHighestDataRow() method to get the highest row number for cells that have actual content.

How to get cell value in PhpSpreadsheet?

To retrieve the value of a cell, the cell should first be retrieved from the worksheet using the getCell() method. A cell's value can be read using the getValue() method. // Get the value from cell A1 $cellValue = $spreadsheet->getActiveSheet()->getCell('A1')->getValue();

What is PhpSpreadsheet?

PhpSpreadsheet is a library written in pure PHP and offers a set of classes that allow you to read and write various spreadsheet file formats such as Excel and LibreOffice Calc.

How do I download PhpSpreadsheet?

Use composer to install PhpSpreadsheet into your project. Or also download the documentation and samples if you plan to use them. A good way to get started is to run some of the samples. Don't forget to download them via --prefer-source composer flag.


1 Answers

Using the worksheet's getHighestRow() method

$highestRow = $this->spreadsheet->getActiveSheet()->getHighestRow();

or getHighestDataRow() if you're only interested in rows where cells contain data and not any blank rows at the end of the worksheet

like image 149
Mark Baker Avatar answered Oct 10 '22 09:10

Mark Baker