4

I am using PHPExcel for bulk data import. While import the excel sheet I want to know the total row count of existing data. For that I am using the code,

$lastRow = $worksheet->getHighestRow();

Now, I am trying to import an excel sheet, In that sheet having 800 rows. But the row count shows 65536 by using the above code. Actually I want to get the row count is 800.

The above code returns the empty row count also. But I need count of data existing rows. How to get that. I am tried the below code also

$lastRow = $worksheet->getHighestDataRow();

but it is also not working. Can anyone please help me.

Deep Kakkar
  • 5,831
  • 4
  • 39
  • 75
Arya
  • 504
  • 2
  • 8
  • 31
  • Possible duplicate of [How to find out how many rows and columns to read from an Excel file with PHPExcel?](https://stackoverflow.com/questions/4562527/how-to-find-out-how-many-rows-and-columns-to-read-from-an-excel-file-with-phpexc) – Bilal Ahmed Dec 27 '17 at 06:02
  • I tried this also. But not working. It returns 65536 rows. – Arya Dec 27 '17 at 06:06
  • i think you should check 65536 row number.. check it's empty or not? – Bilal Ahmed Dec 27 '17 at 06:09
  • 1
    You say "not working"..... that's getHighestRow() is based on the highest rowneed to code something yourself information stored in the Excel file itself, while getHighestDataRow() is based on reading the content of cells.... but a cell can contain a null or an empty string and will still be counted, so while you might not see that data, it's still there, so both these functions are working as intended.... if you need a different definition of empty, then you'll need to check the cells yourself – Mark Baker Dec 27 '17 at 11:47

2 Answers2

10

To get the number of columns in your excel file you have to use the following code:

$objPHPExcel->setActiveSheetIndex(0)->getHighestColumn();

and to get the number of rows

$objPHPExcel->setActiveSheetIndex(0)->getHighestRow();

For your requirement, you should use the following

$objPHPExcel->setActiveSheetIndex(0)->getHighestDataRow();

Use the getHighestDataRow() method to get the highest row number for cells that have actual content.

Note that a blank space or a null value in a cell can qualify as content.

From the 1.7.6 and below PHPExcel versions it is possible to get worksheet information without reading whole file:

$objReader     = PHPExcel_IOFactory::createReader("Excel2007"); 
$worksheetData = $objReader->listWorksheetInfo($uploadedfile);
$totalRows     = $worksheetData[0]['totalRows'];
$totalColumns  = $worksheetData[0]['totalColumns'];
Deep Kakkar
  • 5,831
  • 4
  • 39
  • 75
1
$sheetData = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);

echo count($sheetData)

it will return the row count.....

Bilal Ahmed
  • 4,005
  • 3
  • 22
  • 42
JIJOMON K.A
  • 1,290
  • 3
  • 12
  • 29