Laravel Excel Border

Laravel Excel Border

Laravel Excel is a package that allows you to import and export Excel files in Laravel. If you want to add borders to your Excel cells using Laravel Excel, you can use the setBorder method.

Example

Let’s say we have a Laravel controller method that exports data to an Excel file:

    
    public function exportData()
    {
        $data = [
            ['A1', 'B1', 'C1'],
            ['A2', 'B2', 'C2'],
            ['A3', 'B3', 'C3'],
        ];

        $filePath = public_path('exports/data.xlsx');

        Excel::store($data, $filePath);

        return response()->download($filePath);
    }
    
    

To add borders to the cells in the Excel file, we can use the setBorder method provided by Laravel Excel. Here’s an example of how to add borders to the cells in the above code:

    
    public function exportData()
    {
        $data = [
            ['A1', 'B1', 'C1'],
            ['A2', 'B2', 'C2'],
            ['A3', 'B3', 'C3'],
        ];

        $filePath = public_path('exports/data.xlsx');

        Excel::store($data, $filePath, null, null, [
            'borders' => [
                'allBorders' => [
                    'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN,
                    'color' => ['rgb' => '000000'],
                ],
            ],
        ]);

        return response()->download($filePath);
    }
    
    

In the above code, we are passing an array of options to the store method. The borders option is used to define the border style and color for all borders of the cells.

The 'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN sets the border style to thin. You can also use other styles like BORDER_MEDIUM, BORDER_THICK, etc.

The 'color' => ['rgb' => '000000'] sets the border color to black. You can specify any RGB color code.

By using the above code, the cells in the Excel file will have a thin black border around them.

Related Post

Leave a comment