问题
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
回答1:
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);
回答2:
foreach (range('B', $objPHPExcel->getActiveSheet()->getHighestDataRow()) as $col) {
$objPHPExcel->getActiveSheet()->getStyle('B'.$col)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_NUMBER);
}
来源:https://stackoverflow.com/questions/41738103/phpexcel-format-for-the-column