16

I use PhpSpreadsheet to read o write in Excel files. I want to add to my excel a border style so I used this code:

<?php
    $fxls ='myfile.xlsx';
    $spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($fxls);
    $xls_data = $spreadsheet->getActiveSheet()->toArray(null, true, true, true);
    $sheet = $spreadsheet->getActiveSheet();

    $styleArray = array(
        'borders' => array(
            'outline' => array(
                'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THICK,
                'color' => array('argb' => 'FFFF0000'),
            ),
        ),
    );

    $sheet ->getStyle('B2:G8')->applyFromArray($styleArray);

    /* Generate the Excel File */
    header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
    header('Content-Disposition: attachment;filename="myNEWFile.xlsx"');
    header('Cache-Control: max-age=0');
    header('Cache-Control: max-age=1');
    header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
    header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
    header ('Cache-Control: cache, must-revalidate');
    header ('Pragma: public');
    $writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);
    $writer->save('php://output');
    exit;

I get no error but the excel file is created without border. What I miss !??

dardar.moh
  • 5,987
  • 3
  • 24
  • 33

3 Answers3

37

tl;dr

Beyond the style array way you can also do it in the method chaining way:

use PhpOffice\PhpSpreadsheet\Style\Border;
use PhpOffice\PhpSpreadsheet\Style\Color;

$spreadsheet
    ->getActiveSheet()
    ->getStyle('B2')
    ->getBorders()
    ->getOutline()
    ->setBorderStyle(Border::BORDER_THICK)
    ->setColor(new Color('FFFF0000'));

Blatantly copied from the official docs on cell formatting.

Available border patterns

The docs on cell formatting contains a list of available patterns and their keys used in the style array form. For the method chaining form just slap a get before the capitalized version of the key. These methods are all available under ->getActiveSheet()->getStyle('B2')->getBorders() just like in the example.

  • On a single cell:
    • left: ->getLeft()
    • right: ->getRight()
    • top: ->getTop()
    • bottom: ->getBottom()
    • diagonal: ->getDiagonal()
  • On an area:
    • allBorders: ->getAllBorders()
    • outline: ->getOutline()
    • inside: ->getInside()
    • vertical: ->getVertical()
    • horizontal: ->getHorizontal()

The patterns visualized (also from the docs):

enter image description here

Available border styles

Border::BORDER_DASHDOT
Border::BORDER_DASHDOTDOT
Border::BORDER_DASHED
Border::BORDER_DOTTED
Border::BORDER_DOUBLE
Border::BORDER_HAIR
Border::BORDER_MEDIUM
Border::BORDER_MEDIUMDASHDOT
Border::BORDER_MEDIUMDASHDOTDOT
Border::BORDER_MEDIUMDASHED
Border::BORDER_NONE
Border::BORDER_SLANTDASHDOT
Border::BORDER_THICK
Border::BORDER_THIN
totymedli
  • 29,531
  • 22
  • 131
  • 165
9

You need reassign value to sheet:

$styleArray = array(
    'borders' => array(
        'outline' => array(
            'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THICK,
            'color' => array('argb' => 'FFFF0000'),
        ),
    ),
);

$sheet = $sheet ->getStyle('B2:G8')->applyFromArray($styleArray);
8
$styleArray = array(
    'borders' => array(
        'outline' => array(
            'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THICK,
            'color' => array('argb' => 'FFFF0000'),
        ),
    ),
);

Replace by :

$styleArray = array(
    'borders' => array(
        'outline' => array(
            'style' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THICK,
            'color' => array('argb' => 'FFFF0000'),
        ),
    ),
);

See in line 169-203.

borderStyle has been add after the 1.0.0-beta2 release in 2017-11-26.

Before, borders configuration was still with style

Almagest
  • 81
  • 2
  • The two parts are identical. – Paul Spiegel Jan 12 '18 at 15:51
  • it's not : 'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THICK, to 'style' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THICK, – Almagest Feb 07 '18 at 09:17
  • 2
    Sorry. I must have been blind. But it's wrong though. `style` was for PHPExcel. They changed it to `borderStyle` in PhpSpreedsheet. https://phpspreadsheet.readthedocs.io/en/develop/topics/recipes/#styles - OPs code works for me. – Paul Spiegel Feb 08 '18 at 08:36
  • 2
    Yes indeed, but it's the cofiguration from 1.0.0 release updated on 2017-12-25, this post has been posted on Oct.2017 when phpspreadsheet was on 1.0.0-beta -> https://packagist.org/packages/phpoffice/phpspreadsheet#1.0.0-beta, you can see on github the configuration was still "style" and not "borderStyle" -> https://github.com/PHPOffice/PhpSpreadsheet/blob/1.0.0-beta/src/PhpSpreadsheet/Style.php in line 169-203 – Almagest Feb 12 '18 at 09:07
  • Please add that info to your answer. I can't change my vote without edit. – Paul Spiegel Feb 12 '18 at 19:36
  • As of v1.2, borderStyle works. Of note, the fill property type is now fillType, and allborders is now allBorders – David Hayes May 18 '18 at 03:06