Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHPExcel set border color of a cell

Tags:

phpexcel

How to set border color of a cell? I used this code, but it's not working:

$objPHPExcel->getActiveSheet()
                ->getStyle('A1')
                ->getBorders()
                ->getAllBorders()
                ->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN)
                ->getColor()
                ->setRGB('DDDDDD');

I know applyFromArray() works fine. But I don't want to use it.

like image 288
Muntashir Akon Avatar asked Jul 03 '15 10:07

Muntashir Akon


3 Answers

A bit late, but for the record, I found that this syntax worked fine;

$ActiveSheet->getStyle("A1:Z1")->applyFromArray(
    array(
        'borders' => array(
            'allborders' => array(
                'style' => PHPExcel_Style_Border::BORDER_THIN,
                'color' => array('rgb' => 'DDDDDD')
            )
        )
    )
);

I also found a complete list of styles and attributes here: http://www.bainweb.com/2012/01/phpexcel-style-reference-complete-list.html

like image 114
Gjermund Dahl Avatar answered Nov 18 '22 18:11

Gjermund Dahl


You can apply color to cells border while creating border.

Define style :

$border_style= array('borders' => array('right' => array('style' => 
PHPExcel_Style_Border::BORDER_THICK,'color' => array('argb' => '766f6e'),)));

Apply style :

$sheet = $objPHPExcel->getActiveSheet();
$sheet->getStyle("A2:A40")->applyFromArray($border_style);

Refer PhpExcel documentation here

like image 10
ManiMuthuPandi Avatar answered Nov 18 '22 20:11

ManiMuthuPandi


Set border without applyFromArray() can be done like this.

    $objPHPExcel->getActiveSheet()
                ->getStyle('A1')
                ->getBorders()
                ->getBottom()
                ->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);

    $objPHPExcel->getActiveSheet()
                ->getStyle('A1')
                -> getBorders()
                ->getLeft()
                ->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);

    $objPHPExcel->getActiveSheet()
                ->getStyle('A1')
                ->getBorders()
                ->getRight()
                ->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);

    $objPHPExcel->getActiveSheet()
                ->getStyle('A1')
                ->getBorders()
                ->getBottom()
                ->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);


The Border Style list are

BORDER_NONE - 'none'
BORDER_DASHDOT - 'dashDot'
BORDER_DASHDOTDOT - 'dashDotDot'
BORDER_DASHED - 'dashed'
BORDER_DOTTED - 'dotted'
BORDER_DOUBLE - 'double'
BORDER_HAIR - 'hair'
BORDER_MEDIUM - 'medium'
BORDER_MEDIUMDASHDOT - 'mediumDashDot'
BORDER_MEDIUMDASHDOTDOT - 'mediumDashDotDot'
BORDER_MEDIUMDASHED - 'mediumDashed'
BORDER_SLANTDASHDOT - 'slantDashDot'
BORDER_THICK - 'thick'
BORDER_THIN - 'thin'

For more PHPExcel

like image 6
Nishal K.R Avatar answered Nov 18 '22 20:11

Nishal K.R