Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find out how many rows and columns to read from an Excel file with PHPExcel?

Tags:

php

phpexcel

With the following code, I am able to read the cells out of an Excel file with PHPExcel.

I currently manually define how many rows and columns to read.

Is there a way that PHPExcel can tell me how many rows and columns I have to read to get all the data out of the worksheet, e.g. even if some rows and columns are left blank?

$file_name = htmlentities($_POST['file_name']); $sheet_name = htmlentities($_POST['sheet_name']); $number_of_columns = htmlentities($_POST['number_of_columns']); $number_of_rows = htmlentities($_POST['number_of_rows']);  $objReader = PHPExcel_IOFactory::createReaderForFile("data/" . $file_name); $objReader->setLoadSheetsOnly(array($sheet_name)); $objReader->setReadDataOnly(true); $objPHPExcel = $objReader->load("data/" . $file_name);  echo '<table border="1">'; for ($row = 1; $row < $number_of_rows; $row++) {     echo '<tr>';     for ($column = 0; $column < $number_of_columns; $column++) {         $value = $objPHPExcel->setActiveSheetIndex(0)->getCellByColumnAndRow($column, $row)->getValue();         echo '<td>';         echo $value . '&nbsp;';         echo '</td>';     }     echo '</tr>'; } echo '</table>'; 

Solution:

Thanks, Mark, here's the full solution with those functions:

$file_name = htmlentities($_POST['file_name']); $sheet_name = htmlentities($_POST['sheet_name']); $number_of_columns = htmlentities($_POST['number_of_columns']); $number_of_rows = htmlentities($_POST['number_of_rows']);  $objReader = PHPExcel_IOFactory::createReaderForFile("data/" . $file_name); $objReader->setLoadSheetsOnly(array($sheet_name)); $objReader->setReadDataOnly(true);  $objPHPExcel = $objReader->load("data/" . $file_name);  $highestColumm = $objPHPExcel->setActiveSheetIndex(0)->getHighestColumn(); $highestRow = $objPHPExcel->setActiveSheetIndex(0)->getHighestRow();  echo 'getHighestColumn() =  [' . $highestColumm . ']<br/>'; echo 'getHighestRow() =  [' . $highestRow . ']<br/>';  echo '<table border="1">'; foreach ($objPHPExcel->setActiveSheetIndex(0)->getRowIterator() as $row) {     $cellIterator = $row->getCellIterator();     $cellIterator->setIterateOnlyExistingCells(false);     echo '<tr>';     foreach ($cellIterator as $cell) {         if (!is_null($cell)) {             $value = $cell->getCalculatedValue();             echo '<td>';             echo $value . '&nbsp;';             echo '</td>';         }     }     echo '</tr>'; } echo '</table>'; 

alt text

like image 702
Edward Tanguay Avatar asked Dec 30 '10 12:12

Edward Tanguay


People also ask

How do I know how many rows and columns in Excel?

Just click the column header. The status bar, in the lower-right corner of your Excel window, will tell you the row count. Do the same thing to count columns, but this time click the row selector at the left end of the row. If you select an entire row or column, Excel counts just the cells that contain data.

How many columns of data can Excel handle?

Maximum number of rows & columns in Excel Excel supports three Worksheets in a Workbook file, and each Worksheet can support up to 1,048,576 rows and 16,384 columns of data. Workbooks, however, can have more than 3 Worksheets if the computer supports enough memory for the additional data.


2 Answers

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

and

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

or

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

which returns a range as a string like A1:AC2048

although trailing blank rows and columns are included in these.

EDIT

or you can use the iterators to loop through the existing rows and columns to get each cell within the worksheets used range. See /Tests/28iterator.php in the production distribution for an example. The iterators can be set to ignore blanks.

like image 172
Mark Baker Avatar answered Sep 22 '22 21:09

Mark Baker


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']; 
like image 22
Alliswell Avatar answered Sep 21 '22 21:09

Alliswell