Reputation: 39
I Am using Maatwebsite Excel 3.1 for export functionality. How can we set the background color for cells and and font size for headings?.Could you please help me to solve this?
Thank you!
Upvotes: 4
Views: 11016
Reputation: 1913
Use html and the FromView
concern.
documentation
Example Maatwebsite Export FromView concern:
Run artisan command: php artisan make:export ExampleExportView
Change ToCollection
class that is outputted to below ToView
class (php artisan help does not show a ToView
option, so we have to change it ourselves).
<?php
namespace App\Exports;
use Illuminate\Contracts\View\View;
use Maatwebsite\Excel\Concerns\FromView;
class ExampleExportView implements FromView
{
private $table;
public function __construct($table)
{
$this->table = $table;
}
public function view(): View
{
$tableHtml = $this->table;
return view('exports.exampleview', compact('tableHtml'));
}
}
In the exports
directory create exampleview.blade.php
file with laravel echo:
{!! $tableHtml !!}
From your controller build the html table and instantiate the ExampleExportView
class passing the html table to the constructor while returning the download method of the Excel
facade:
<?php
namespace App\Http\Controllers\Admin;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Exports\ExampleExportView;
use Maatwebsite\Excel\Facades\Excel;
class ExampleController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function downloadExcel()
{
$filename = "example_excel_maatwebsite_from_colour_view";
$table = <<<TABLE
<h2>Maatwebsite Excel FromView</h2>
<table>
<thead>
<tr>
<th style='background-color: #007bff; color: #f8f9fa;'>Heading one blue</th>
<th style='background-color: #dc3545; color: #f8f9fa;'>Heading two red</th>
</tr>
</thead>
<tbody>
<tr>
<td style='background-color: #ffb759;'>18 Yellow</td>
<td>17 no colour</td>
</tr>
</tbody>
</table>
TABLE;
return Excel::download(new ExampleExportView($table),
$filename .'.xlsx');
}
Register your route:
Route::get('download-excel-html-table', 'ExampleController@downloadExcel')
->name('download-excel-html-table');
Place the route in your index blade and it will download the excel file with five cells filled with data.
Cell 'A1' will have a large header with word Maatwebsite Excel FromView
.
Cell 'A2' will have value Heading one blue
as well as a blue background with white text and 'B2' will have value Heading two red
as well as a red background with white text, while 'A3' will be yellow with value 18 Yellow
and 'B3' no colour with value 17 no colour
.
You can also use inline css to style html (all documented):
<html>
{{ HTML::style('css/table.css') }}
<!-- Cell styled with class -->
<td class="cell">Cell</td>
<!-- Cell styled with ID -->
<td id="cell">Cell</td>
</html>
Of course this is a small static example. I build large 'sheet' fillers by looping over data arrays and allocating colours to html td's.
Upvotes: 0
Reputation: 2568
Firstly, implement WithHeadings
and add use RegistersEventListeners
. This will allow you to use afterSheet
method autowired to the event:
use Maatwebsite\Excel\Concerns\FromArray;
use Maatwebsite\Excel\Concerns\RegistersEventListeners;
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Events\AfterSheet;
class BomExport implements FromArray, WithEvents
{
use RegistersEventListeners;
public static function afterSheet(AfterSheet $event)
{
// Add styling here
}
// ...
}
For alternative ways of placing your styling code, check the documentation.
In afterSheet
method, you can access the underlaying library and its Worksheet
object (\PhpOffice\PhpSpreadsheet\Worksheet\Worksheet):
$sheet = $event->sheet->getDelegate();
Using that object, you can e.g.:
Set font size, weight and color on the first row:
$sheet->getStyle('1')->getFont()
->setSize(16)
->setBold(true)
->getColor()->setRGB('0000ff')
Set background color on the second column:
$sheet->getStyle('B')->getFill()
->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)
->getStartColor()->setARGB('FFFF0000');
Set border thickness on a cell:
$sheet->getStyle('D3')->getBorders()->getAllBorders()
->setBorderStyle(\PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THICK);
Set row height:
$sheet->getRowDimension('1')->setRowHeight(26);
For more options, see the documentation.
Unfortunately, styling whole rows ($sheet->getStyle('1')
) and columns ($sheet->getStyle('B')
) doesn't work in Excel Mobile (version 16001.12325.20032.0) I had to use cell ranges ($sheet->getStyle('A1:Z1')
or $sheet->getStyle('A1:A999')
)
Putting it together:
use Maatwebsite\Excel\Concerns\FromArray;
use Maatwebsite\Excel\Concerns\RegistersEventListeners;
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Events\AfterSheet;
class BomExport implements FromArray, WithEvents
{
use RegistersEventListeners;
public static function afterSheet(AfterSheet $event)
{
$sheet = $event->sheet->getDelegate();
$sheet->getStyle('1')->getFont()->setSize(16);
$sheet->getStyle('1')->getFill()
->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)
->getStartColor()->setARGB('FFFF0000');
// ...
}
// ...
}
Upvotes: 9
Reputation: 39
I have a controller
public function exportComplaint($request)
{
return Excel::download(new ComplaintExport($complaintData), 'excel.xls');
}
And in App\Exports
use Maatwebsite\Excel\Concerns\FromArray;
use Maatwebsite\Excel\Concerns\WithHeadings;
class ComplaintExport implements FromArray, WithHeadings
{
protected $request = null;
public function __construct($request)
{
$this->request = $request;
}
public function array(): array
{
return $this->request;
}
public function headings(): array
{
return ['name','contacts']; //etc
}
}
In the above code where i need to add sheet
function?
Upvotes: -1
Reputation: 2131
Try something like this:
$sheet->row(1, ['Col 1', 'Col 2', 'Col 3']); // etc
$sheet->row(1, function($row) {
$row->setBackground('#CCCCCC');
});
Also you can change $sheet->row() to $sheet->cell() and keep passing a row number as first argument.
$sheet->cell(1, function($row) {
$row->setBackground('#CCCCCC');
});
Upvotes: -1