How can I do a loop which based on Excel worksheet columns? I found (and used) WorksheetIterator, RowIterator and CellIterator but nothing about columns.
7 Answers
There is no ColumnIterator, so you'll have to do this by hand.
For any given worksheet:
To loop rows for a column:
$column = 'A';
$lastRow = $worksheet->getHighestRow();
for ($row = 1; $row <= $lastRow; $row++) {
$cell = $worksheet->getCell($column.$row);
// Do what you want with the cell
}
To loop columns in a row, you can take advantage of PHP's Perls-style ability to increment characters:
$row = 1;
$lastColumn = $worksheet->getHighestColumn();
$lastColumn++;
for ($column = 'A'; $column != $lastColumn; $column++) {
$cell = $worksheet->getCell($column.$row);
// Do what you want with the cell
}
Note that when comparing column letters to test for the last column in the loop, we can't simply use < or <= because we're comparing strings, and "B" > "AZ" in standard string comparison, so we use a != comparison, having incremented the highest column value to give the first column ID past the end point.
You can also use
$worksheet->cellExists($column.$row);
in the loop to test for the existence of a cell before accessing it using getCell() (or not) to emulate the iterator getIterateOnlyExistingCells() behaviour
The iterators are actually fairly slow, so you may well find these simple loops are faster than using the iterators.
UPDATE (2015-05-06)
PHPExcel version 1.8.1 has introduced a new Column Iterator. The Row and Column iterators also allows you to specify a range of rows or columns to iterate, and allow you to use prev() and well as next() when looping through

- 209,507
- 32
- 346
- 385
-
8I was trying to get a numeric value for columns. I wasn't aware the character increment feature of PHP. Thanks for the solution and this neat tip! – Hazard Apr 07 '11 at 08:47
-
$lastColumn = $worksheet->getHighestColumn(); is returning B.and B is last filled column please help. – Bugfixer Oct 01 '15 at 13:14
-
1Well if `B` is the last filled column, what do you expect `getHighestColumn()` to return? That's what it's supposed to do – Mark Baker Oct 01 '15 at 21:04
-
if a particular cell has a comma separeted values ,how can I extract that? – Swanand Keskar Sep 02 '16 at 07:35
-
@swanandkeskar - If you want to ask a question, ask it as a question, not as a comment against an unrelated question – Mark Baker Sep 02 '16 at 08:03
-
What about double letters column? Would PHP correctly iterate across Z to AA? – datasn.io Nov 07 '17 at 06:41
-
@kavoir.com - Yes it will, that's the whole point of the answer: `To loop columns in a row, you can take advantage of PHP's Perls-style ability to increment characters:` – Mark Baker Nov 07 '17 at 07:51
This short snippet provides loop throught rows of columns. It gets the indexes of last non empty column (and its row) and loops to that indexes, so be aware of forgotten values in excel.
Code loops throught rows of column A, then rows of column B ...
$objReader = new PHPExcel_Reader_Excel2007();
$objReader->setReadDataOnly(true);
$objPHPExcel = $objReader->load($file);
foreach ($objPHPExcel->getWorksheetIterator() as $worksheet)
{
$worksheetTitle = $worksheet->getTitle();
$highestColumn = $worksheet->getHighestColumn();
$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
// expects same number of row records for all columns
$highestRow = $worksheet->getHighestRow();
for($col = 0; $col < $highestColumnIndex; $col++)
{
// if you do not expect same number of row records for all columns
// get highest row index for each column
// $highestRow = $worksheet->getHighestRow();
for ($row = 1; $row <= $highestRow; $row++)
{
$cell = $worksheet->getCellByColumnAndRow($col, $row);
$val = $cell->getValue();
// do what you want with cell value
}
}
}

- 151
- 1
- 6
loop columns as range
foreach ( range('A', $Excel->getActiveSheet()->getHighestColumn()) as $column_key) {
}

- 2,616
- 1
- 15
- 13
-
3
-
1It only works with columns that have one character name ( A - Z ). See https://www.php.net/manual/en/function.range.php – user1077915 Oct 02 '20 at 20:50
Try this! Work!
$limit = 10000;
$value='sua mensagem'
for($i=0,$j='A';$i<$limit;$i++,$j++) {
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue($j.$i, $value);
}
set in $value what you want print.

- 1,527
- 18
- 15
-
1Well, Set in limit 16.384, if you wanna use all colls. This's a limit from Excel. Variable $j is (A ... AAAAAA) and $i is (1 ... 1.048.576), you can use only $j for coll letter and fix number: `->setCellValue($j.'1', $value); ` and $value you can you can make another loop or put data from a database and / or data file to populate the values. – Rogerio de Moraes Jul 21 '14 at 03:44
This is the fix for the getColumnLetter method of the last message, it allows you to get the "letters" of the columns, whatever the number of columns you have
function getColumnLetter( $number ){
$prefix = '';
$suffix = '';
$prefNum = intval( $number/26 );
if( $number > 25 ){
$prefix = getColumnLetter( $prefNum - 1 );
}
$suffix = chr( fmod( $number, 26 )+65 );
return $prefix.$suffix;
}

- 11
- 1
This recursive method was designed allows you to get the "letters" of the columns, whatever the number of columns you have, from "A" to "ZZZZZZZZZ..." :
function getColumnLetter( $number )
{
$prefix = '';
$suffix = '';
$prefNum = intval( $number/26 );
if( $prefNum > 25 )
{
$prefix = getColumnLetter( $prefNum );
}
$suffix = chr( fmod( $number, 26 )+65 );
return $prefix.$suffix;
}
So you can loop the columns of an PHP array on index number and convert it to a string of letters and use it in PHPExcel methods, like "setCellValue" ... But solutions above are surely faster if you can use them !

- 169
- 1
- 7
-
2PHPExcel already has a helper method for this called PHPExcel_Cell::stringFromColumnIndex() – dctucker Dec 14 '16 at 20:52
-
Yes, with a limitation to 3 letters. https://github.com/PHPOffice/PHPExcel/blob/1.8/Classes/PHPExcel/Cell.php line 790 – Glaubule Dec 15 '16 at 22:07
-
2I think the three-letter limitation is related to practical limitations such as the maximum number of columns in an Excel worksheet (16,384). Three letters A-Z can cover that range and then some. Can you identify a use case where a four-letter column name is required? – dctucker Feb 17 '17 at 20:14
In PhpSpreadsheet ( which replaces PHPExcel ) you can do:
foreach( $sheet->getColumnIterator() as $col )
{
$sheet->getColumnDimension( $col->getColumnIndex() )->setAutosize( true );
}

- 828
- 1
- 12
- 26