Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Php Excel set whole column data alignment not working

Tags:

php

phpexcel

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);
like image 239
pankil thakkar Avatar asked Nov 09 '12 11:11

pankil thakkar


3 Answers

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);
like image 99
Mark Baker Avatar answered Oct 04 '22 18:10

Mark Baker


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);
like image 40
z3d0 Avatar answered Oct 04 '22 19:10

z3d0


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

like image 42
Jijesh Cherrai Avatar answered Oct 04 '22 18:10

Jijesh Cherrai