How can I change color a character laravel excel maatwebsite?
You may register macro within a service provider's boot
method. For example App\Providers\AppServiceProvider
provider's will look like as:
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use \Maatwebsite\Excel\Sheet;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
//
Sheet::macro('styleCells', function (Sheet $sheet, string $cellRange, array $style) {
$sheet->getDelegate()->getStyle($cellRange)->applyFromArray($style);
});
}
/**
* Register any application services.
*
* @return void
*/
public function register()
{
//
}
}
You should create different service provider to resister this kind of macro for isolating third party's concern.
For font color set font style :
public function registerEvents(): array
{
return [
AfterSheet::class => function(AfterSheet $event) {
$event->sheet->styleCells(
'B1:D1',
[
//Set border Style
'borders' => [
'outline' => [
'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THICK,
'color' => ['argb' => 'EB2B02'],
],
],
//Set font style
'font' => [
'name' => 'Calibri',
'size' => 15,
'bold' => true,
'color' => ['argb' => 'EB2B02'],
],
//Set background style
'fill' => [
'fillType' => \PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID,
'startColor' => [
'rgb' => 'dff0d8',
]
],
]
);
},
];
}
Laravel Excel Style with CSS not working while exporting view
As I commented, for more complicated styles you need to use PhpSpreadsheet's styling methods. More info on the phpspreadsheet's docs.
The way I did it was by using the events to get the underlying phpspreadsheet classes.
use Maatwebsite\Excel\Concerns\FromView;
use Maatwebsite\Excel\Concerns\RegistersEventListeners;
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Events\AfterSheet;
class MatrixExcelExport implements FromView, WithEvents
{
use RegistersEventListeners;
public function __construct(...) { ... }
public function view(): View
{
return view('admin.matrix._excel', [...]);
}
public static function afterSheet(AfterSheet $event)
{
// Create Style Arrays
$default_font_style = [
'font' => ['name' => 'Arial', 'size' => 10]
];
$strikethrough = [
'font' => ['strikethrough' => true],
];
// Get Worksheet
$active_sheet = $event->sheet->getDelegate();
// Apply Style Arrays
$active_sheet->getParent()->getDefaultStyle()->applyFromArray($default_font_style);
// strikethrough group of cells (A10 to B12)
$active_sheet->getStyle('A10:B12')->applyFromArray($strikethrough);
// or
$active_sheet->getStyle('A10:B12')->getFont()->setStrikethrough(true);
// single cell
$active_sheet->getStyle('A13')->getFont()->setStrikethrough(true);
}
}
Or you could also follow laravel excel's styling guide
The WithStyles concerns allows styling columns, cells and rows. This might be useful when you want to make the heading row bold.
namespace App\Exports;
use Maatwebsite\Excel\Concerns\WithStyles;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
class InvoicesExport implements WithStyles
{
public function styles(Worksheet $sheet)
{
return [
// Style the first row as bold text.
1 => ['font' => ['bold' => true]],
// Styling a specific cell by coordinate.
'B2' => ['font' => ['italic' => true]],
// Styling an entire column.
'C' => ['font' => ['size' => 16]],
];
}
}For the contents of the styles array, please refer to the PhpSpreadsheet docs(opens new window)
If you prefer the fluent syntax for styling cells, you can do it as follows:
namespace App\Exports;
use Maatwebsite\Excel\Concerns\WithStyles;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
class InvoicesExport implements WithStyles
{
public function styles(Worksheet $sheet)
{
$sheet->getStyle('B2')->getFont()->setBold(true);
}
}
Set background color for heading in export CSV format in laravel maatwebsite
Comma-separated values (CSV) file stores tabular data (numbers and text) in plain text, so you can't assign a background color in a CSV file. You should use Excel (xls or xlsx) format.
Related Topics
How to Check If a String Contains a Specific Word
How to Apply Bindvalue Method in Limit Clause
Should We Ever Check For MySQLi_Connect() Errors Manually
How to Configure Xampp to Send Mail from Localhost
Reference - Frequently Asked Questions About Pdo
How to Parameterize the Table Name in a Prepared Statement
Call to a Member Function on a Non-Object
Pdo Support For Multiple Queries (Pdo_MySQL, Pdo_MySQLnd)
Grabbing the Href Attribute of an a Element
Showing All Errors and Warnings
How to Create Cron Job Using PHP
How to Get a Youtube Video Thumbnail from the Youtube API
Replace Preg_Replace() E Modifier With Preg_Replace_Callback
Cross-Origin Request Headers(Cors) With PHP Headers
How to Get Time Difference in Minutes in PHP
Show a Number to Two Decimal Places
Dynamically Display a CSV File as an HTML Table on a Web Page