PHPExcel - format for the column

亡梦爱人 提交于 2019-12-11 08:33:40

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!