Php Excel set whole column data alignment not working

荒凉一梦 提交于 2019-12-21 07:10:08

问题


I am Using this code For E column data set to right align but Its not showing me effect

$objPHPExcel->getActiveSheet()
    ->getStyle('E')
    ->getAlignment()
    ->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);

instead of 'E' if i write E6 then it display E6 cell data to right.

$objPHPExcel->getActiveSheet()
    ->getStyle('E6')
    ->getAlignment()
    ->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);

回答1:


You're correct: row and column styles aren't supported by PHPExcel.

Cell styling is, but you can also set style by a range of cells:

$objPHPExcel->getActiveSheet()
    ->getStyle('E1:E256')
    ->getAlignment()
    ->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);



回答2:


Since nobody explained how to style an entire column, which was part of the question, here's the code:

$lastrow = $objPHPExcel->getActiveSheet()->getHighestRow();

$objPHPExcel->getActiveSheet()
        ->getStyle('E1:E'.$lastrow)
        ->getAlignment()
        ->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);



回答3:


Try this code. It works well.And I have confirmed.

 $activeSheet = $phpExcelObject->getActiveSheet();
    //..
    //...
     $activeSheet->getStyle("E")
                 ->getAlignment()
                 ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);

This code align the column E in horizontal right




回答4:


I have confirmed this as well while trying to apply certain number formats to colums: you cannot apply a style to a column - getStyle('E'), you must specify the range - getStyle('E1:E50').

$objPHPExcel->getActiveSheet()->fromArray($row_array, NULL, 'A2');
$rows = count($row_array);
$objPHPExcel->getActiveSheet()->getStyle('C2:C'.$rows)->getNumberFormat()->setFormatCode('000000000');

This code will left-pad the numbers in column C with zeros



来源:https://stackoverflow.com/questions/13307111/php-excel-set-whole-column-data-alignment-not-working

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