官网:https://docs.laravel-excel.com/3.1/getting-started/
安装composer require maatwebsite/excel导出 一、导出单个Sheet
1、service封装
data = $data;
$this->headings = $headings;
$this->title = $title;
$this->columnFormats = $columnFormats;
return MaatwebsiteExcelFacadesExcel::download($this, $filename);
}
public function array() : array {
return $this->data ? : [];
}
public function title() : string {
return $this->title ? : '';
}
public function headings() : array {
return $this->headings ? : [];
}
public function columnFormats() : array {
return $this->columnFormats ? : [];
}
}
2、使用案例
'你这瓜保熟吗?',
'field2' => '柚子味西瓜',
'field3' => '柚子味香水',
'field4' => '18000000000',
'field5' => '12'
],
[
'field1' => '你这瓜保熟吗?',
'field2' => '柚子味西瓜',
'field3' => '柚子味香水',
'field4' => '18000000000',
'field5' => '12'
]
];
// 标题
$headings = ['字段1', '字段2', '字段3', '字段4', '字段5'];
// 字段自定义格式
$columnFormats = [
'D' => NumberFormat::FORMAT_TEXT
];
return (new ExcelExport())->download($result, $headings, 'WorkSheet', 'Demo.xlsx', $columnFormats);
}
}
二、导出多个Sheet
1、services封装
data = $data;
$this->headings = $headings;
$this->title = $title;
$this->columnFormats = $columnFormats;
}
public function array() : array {
return $this->data ? : [];
}
public function headings() : array {
return $this->headings ? : [];
}
public function title() : string {
return $this->title ? : '';
}
public function columnFormats() : array {
return $this->columnFormats ? : [];
}
}
sheets = $sheets;
return MaatwebsiteExcelFacadesExcel::download($this, $filename);
}
public function sheets() : array {
return $this->sheets ? : [];
}
}
2、使用案例
'sheet1',
'data' => [
[
'field1' => '你这瓜保熟吗?',
'field2' => '柚子味西瓜',
'field3' => '柚子味香水',
'field4' => '18000000000',
'field5' => '12.00'
],
[
'field1' => '你这瓜保熟吗?',
'field2' => '柚子味西瓜',
'field3' => '柚子味香水',
'field4' => '18000000000',
'field5' => '12.00'
]
],
'columnFormats' => [
'D' => NumberFormat::FORMAT_TEXT,
'E' => NumberFormat::FORMAT_TEXT,
]
],
[
'sheet' => 'sheet2',
'data' => [[
'field1' => '你这瓜保熟吗?',
'field2' => '柚子味西瓜',
'field3' => '柚子味香水',
'field4' => '18000000000',
'field5' => '12.00'
],
[
'field1' => '你这瓜保熟吗?',
'field2' => '柚子味西瓜',
'field3' => '柚子味香水',
'field4' => '18000000000',
'field5' => '12.00'
]
],
'columnFormats' => [
'D' => NumberFormat::FORMAT_TEXT,
'E' => NumberFormat::FORMAT_TEXT,
]
]
];
// 标题
$headings = ['字段1', '字段2', '字段3', '字段4', '字段5'];
$sheet = [];
foreach ($sheetData as &$item) {
$sheet[] = new Multiple($item['data'], $headings, $item['sheet'], $item['columnFormats']);
}
return (new MultipleSheetsExport())->download($sheet);
}
}



