PHPExcel - format for the column

Dima Kuzmin picture Dima Kuzmin · Jan 19, 2017 · Viewed 9k times · Source

I need to set for all columns format: FORMAT_NUMBER

I can do it for one cell. But I can not do for the whole column B.

$objPHPExcel->getActiveSheet()->getStyle('B2')->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_NUMBER);

How to set the entire column B? PHPExcel_Style_NumberFormat :: FORMAT_NUMBER

Answer

Mark Baker picture Mark Baker · Jan 19, 2017

You can set styling for an individual cell, or for a range of cells; but not for a column or a row.

To set the style for a range, use

$objPHPExcel->getActiveSheet()
    ->getStyle('B2:B1024')
    ->getNumberFormat()
    ->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_NUMBER);

So just identify the first and last rows that you want to set the style for in column B, and build a range string from that.

$column = 'B'; $firstRow = 2; $lastRow = 1024;

$objPHPExcel->getActiveSheet()
    ->getStyle($column.$firstRow.':'.$column.$lastRow)
    ->getNumberFormat()
    ->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_NUMBER);