Get number of CSV rows

Topics: Developer Forum
Oct 16, 2012 at 6:23 AM

Hi

Is there a function that we can get the number of rows in a CSV file?  I am using the below code to read the data of the CSV file. 

$sheetData = $objPHPExcel->getActiveSheet()->toArray(null, true, true, true);

Thanks,

aobs

Coordinator
Oct 16, 2012 at 7:32 AM

count($sheetData);

or

$objPHPExcel->getActiveSheet()->getHighestRow();

Oct 16, 2012 at 11:29 AM

If the file is csv, all rows are concatenated into one long string.  That is, $sheetData only contains one long row.  Is there a function to get the actual number of rows?  or we have to write our own code to count the rows?

Coordinator
Oct 16, 2012 at 1:28 PM

If the CSV is being loaded as a single row, then there's something wrong with the loader function, or the line ending isn't being recognised. Can you please provide an example of this CSV (sanitized if necessary)

Oct 16, 2012 at 3:05 PM
Edited Oct 16, 2012 at 3:33 PM

I am using Excel 2007 running on Windows XP Pro and save the file as CSV file.  You can download it here: http://www.abacusinmotion.com/PHPExcel/testartsy1.csv.

I use this code to create the object:

$objPHPExcel = PHPExcel_IOFactory::load($filename);

This is what I got in the array:

array(1) { [1]=>array(1) { ["A"]=>string(50) "A bb,d@d.com,123abc123,20 b b,B@b.com,123abc123,40" } }

Coordinator
Oct 16, 2012 at 5:35 PM
Edited Oct 16, 2012 at 5:37 PM

Very weird: CSV loading is pretty standard, so I can't see why this isn't working.... it's not as if PHPExcel tries to do anything clever with CSV, simply uses PHP's built-in fgetcsv() function.... will need to investigate what could have broken such a basic piece of functionality

Coordinator
Oct 16, 2012 at 7:50 PM

Does your csv file have a file extension when you're loading it into PHPExcel?

http://phpexcel.codeplex.com/workitem/18794

Oct 17, 2012 at 2:59 AM
Edited Oct 17, 2012 at 6:00 AM

The file uploaded to the server has .csv as the file extension.  The server script will change its filename, keep the file extension type and move it to a directory.  The file with name changed is  21aa24fbdb1ec5d576c6f666b1f2d494.csv.  It has the same contents as the original file.  The file has csv extension when passed to PHPExcel.

I use fgetcsv to read the file and the file can be read properly identifying 4 fields and 2 rows.

Coordinator
Oct 17, 2012 at 7:39 AM

OK, so I need to identify a weird bug somewhere in the Excel CSV Reader! <sigh> If you were loading to PHPExcel while the file was still in PHP's temporary upload directory before being moved, then I could understand this.

I can't reproduce it: it loads cleanly for me using both the 1.7.7 and 1.7.8 code. Can you try using

$reader = PHPExcel_IOFactory::identify($filename);

and then echoing the value of $reader just to confirm.

 

Oct 17, 2012 at 2:45 PM

$reader is HTML

Coordinator
Oct 17, 2012 at 4:55 PM

Try with the bugfix I listed previously:

http://phpexcel.codeplex.com/workitem/18794

This bugfix was to prevent CSV files being wrongly identified as HTML

 

Code is on the develop branch of github

Oct 21, 2012 at 8:50 PM

Looks like the same problem is happening with TXT files -- The Identify method says it's an HTML file, and only one line gets loaded.

Coordinator
Oct 21, 2012 at 9:38 PM

Then try the same solution.... this bug has been fixed in the develop branch on github.... but a txt file is pretty meaningless in spreadsheet terms