Dima Kuzmin
Dima Kuzmin

Reputation: 161

PHPExcel - format for the column

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

Upvotes: 1

Views: 12071

Answers (2)

Mark Baker
Mark Baker

Reputation: 212402

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);

Upvotes: 3

Dima Kuzmin
Dima Kuzmin

Reputation: 161

    foreach (range('B', $objPHPExcel->getActiveSheet()->getHighestDataRow()) as $col) {
$objPHPExcel->getActiveSheet()->getStyle('B'.$col)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_NUMBER);
        }

Upvotes: 0

Related Questions