laravel-excel3.1 导出的基础参数设置
表头设置
use Maatwebsite\Excel\Concerns\WithHeadings;
... ...
public function headings(): array
return array_column([
'序号','姓名'
表标题设置
use Maatwebsite\Excel\Concerns\WithTitle;
... ...
public function title(): string
return '汇总总表';
设置单元格
use Maatwebsite\Excel\Concerns\WithStyles;
... ...
public function styles(Worksheet $sheet)
// 冻结行
$sheet->freezePaneByColumnAndRow('1','2');
// 合并单元格
$sheet->mergeCells('A1:F1');
// 设置字体颜色和背景色
$sheet->getStyle('A1:J1')->applyFromArray([
'font' => [
'name' => '宋体',
'bold' => true,
'italic' => false,
'strikethrough' => false,
'color' => [
'rgb' => 'FFFFFF'
'fill' => [
'fillType' => 'linear', //线性填充,类似渐变
'rotation' => 45, //渐变角度
'startColor' => [
'rgb' => '34A8E7' //初始颜色
//结束颜色,如果需要单一背景色,请和初始颜色保持一致
'endColor' => [
'argb' => '34A8E7'
列文本格式设置
use PhpOffice\PhpSpreadsheet\Cell\Cell;
use PhpOffice\PhpSpreadsheet\Cell\DataType;
use PhpOffice\PhpSpreadsheet\Cell\DefaultValueBinder;
... ...
class xxx extends DefaultValueBinder implements FromCollection
... ...
public function bindValue(Cell $cell, $value)
$column = $cell->getColumn();
if (in_array($column, ['D', 'E'])) {
$cell->setValueExplicit($value, DataType::TYPE_STRING);
return true;
return parent::bindValue($cell, $value);
use Maatwebsite\Excel\Concerns\WithColumnWidths;
... ...
public function columnWidths(): array
return [
'A' => 10,
'C' => 10,
'D' => 35,
'H' => 10,
'I' => 10,
'J' => 25,