Using PHPSpreadsheet to Add Borders
In PHPSpreadsheet, you can add borders to cells, ranges, or worksheets to enhance the visual appearance of your Excel files. Borders can be used to separate cells, highlight certain sections, or create visual distinctions.
Applying Borders to Cells
To add borders to individual cells, you need to:
- Create an instance of the
PhpOffice\PhpSpreadsheet\Style\Border
class. - Specify the border style, color, and thickness using appropriate methods of the
Border
class. - Apply the border style to your desired cell(s) using the
getStyle()
method.
Example
<?php
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Style\Border;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
// Create a new Spreadsheet instance
$spreadsheet = new Spreadsheet();
// Select the active sheet
$sheet = $spreadsheet->getActiveSheet();
// Add data to cell A1
$sheet->setCellValue('A1', 'Hello, World!');
// Apply a thick solid border to cell A1
$border = new Border();
$border->setBorderStyle(Border::BORDER_THICK);
$border->setColor(new \PhpOffice\PhpSpreadsheet\Style\Color(\PhpOffice\PhpSpreadsheet\Style\Color::COLOR_RED));
$sheet->getStyle('A1')->applyFromArray([
'borders' => [
'allBorders' => [
'borderStyle' => $border->getBorderStyle(),
'color' => ['rgb' => $border->getColor()->getRGB()],
],
],
]);
// Save the spreadsheet
$writer = new Xlsx($spreadsheet);
$writer->save('border_example.xlsx');
?>
In this example, we create a new spreadsheet, add the text “Hello, World!” to cell A1, and then add a thick red border to cell A1 using the getStyle()
method and applying the border settings using the applyFromArray()
method.
Applying Borders to Ranges
If you want to apply borders to a range of cells, you can use the getStyle()
method with range notation to select the desired range. Then, you can apply the border style using the same process as for individual cells.
Example
<?php
// ...
// Apply a thin dashed border to range A1:B5
$sheet->getStyle('A1:B5')->applyFromArray([
'borders' => [
'outline' => [
'borderStyle' => Border::BORDER_DASHED,
'color' => ['rgb' => '000000'],
],
'inside' => [
'borderStyle' => Border::BORDER_THIN,
'color' => ['rgb' => '000000'],
],
],
]);
// ...
?>
In this example, we apply a dashed outline border to the range A1:B5 and a thin solid border to the inside of the range. The color is set to black using the ‘rgb’ notation.
Applying Borders to Worksheets
If you want to apply borders to an entire worksheet, you can use the getDefaultStyle()
method to specify the default style for all cells in the worksheet. Then, you can set the border style using the applyFromArray()
method.
Example
<?php
// ...
// Get the default style of the worksheet
$defaultStyle = $sheet->getDefaultStyle();
// Apply a thick solid border to all cells in the worksheet
$defaultStyle->applyFromArray([
'borders' => [
'allBorders' => [
'borderStyle' => Border::BORDER_THICK,
'color' => ['rgb' => 'FF0000'],
],
],
]);
// ...
?>
In this example, we get the default style of the worksheet using the getDefaultStyle()
method. Then, we apply a thick red border to all cells in the worksheet using the applyFromArray()
method.
Conclusion
PHPSpreadsheet provides various methods to add borders to cells, ranges, or entire worksheets. By using the Border
class and applying the desired border styles, colors, and thickness, you can create visually appealing Excel files using PHP.