Phpspreadsheet borders

PHP Spreadsheet Borders

The PHP Spreadsheet Library, also known as PhpSpreadsheet, allows you to easily work with spreadsheets in PHP. Borders are an important aspect of styling cells in a spreadsheet. With PhpSpreadsheet, you can apply different types of borders to individual cells or entire ranges of cells.

Applying Borders

To apply borders to cells in PHP Spreadsheet, you can use the getStyle() method on a cell or range object. This method returns an instance of the PhpSpreadsheet\Style\Border class, which provides different methods for defining border properties.

// Example: Applying borders to a cell
$spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$cell = $sheet->getCell('A1');
$borderStyle = $cell->getStyle()->getBorders();
$borderStyle->getTop()->setBorderStyle(\PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN);
$borderStyle->getBottom()->setBorderStyle(\PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN);
$borderStyle->getLeft()->setBorderStyle(\PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN);
$borderStyle->getRight()->setBorderStyle(\PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN);
$cell->getStyle()->setBorders($borderStyle);

In the example above, we create a new spreadsheet and get the active sheet. We then retrieve a specific cell using the getCell() method and define the border styles using the setBorderStyle() method of each border side. Finally, we apply the borders to the cell by setting the borders property of its style object.

Border Styles

PhpSpreadsheet offers several pre-defined border styles:

  • \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_NONE – No border
  • \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN – Thin border
  • \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_MEDIUM – Medium border
  • \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THICK – Thick border
  • \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_DASHED – Dashed border
  • \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_DOTTED – Dotted border
  • \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_DOUBLE – Double border

You can use these constants in combination with the setBorderStyle() method to define the desired border style.

Border Colors

In addition to style, you can also specify the color of the borders. The Border class provides the getColor() and setColor() methods to get and set the border color respectively. You can use hexadecimal color values or named colors.

// Example: Setting border color
$border = $cell->getStyle()->getBorders();
$border->getLeft()->getColor()->setRGB('FF0000'); // Red color
$border->getRight()->getColor()->setRGB('0000FF'); // Blue color

In the example above, we retrieve the border object, then set the color of the left and right borders to red and blue respectively using the setRGB() method.

Examples

Here are a few examples to demonstrate how to apply different border styles:

// Example 1: Thin border around a range of cells
$borderStyle = $sheet->getStyle('A1:D4')->getBorders();
$borderStyle->getAllBorders()->setBorderStyle(\PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN);

// Example 2: Double border on top and bottom
$borderStyle = $sheet->getStyle('A1:C1')->getBorders();
$borderStyle->getTop()->setBorderStyle(\PhpOffice\PhpSpreadsheet\Style\Border::BORDER_DOUBLE);
$borderStyle->getBottom()->setBorderStyle(\PhpOffice\PhpSpreadsheet\Style\Border::BORDER_DOUBLE);

Leave a comment