建立UserExport
namespace App\Admin\Extensions\Exporter;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\FromArray;
use Maatwebsite\Excel\Concerns\WithDrawings;
use PhpOffice\PhpSpreadsheet\Worksheet\Drawing;
class UserExport implements FromArray, ShouldAutoSize, WithDrawings
{
protected $invoices;
protected $logo;
public function __construct(array $invoices, array $logo)
{
$this->invoices = $invoices;
$this->logo = $logo;
}
use Exportable;
public function array(): array
{
return $this->invoices;
}
public function drawings()
{
$result = [];
foreach ($this->logo as $k => $v) {
$k += 2;
${'drawing' . $k} = new Drawing();
${'drawing' . $k}->setName('頭像');
${'drawing' . $k}->setDescription('頭像');
//圖片路徑
${'drawing' . $k}->setPath(public_path($v));
${'drawing' . $k}->setHeight(50);
//設定圖片列
${'drawing' . $k}->setCoordinates('C' . $k);
$result[] = ${'drawing' . $k};
}
return $result;
}
}
控制器
class SendController extends Controller
{
//
public function index()
{
return (Excel::download(new UserExport([
['使用者名稱' ,'年齡' ,'頭像'],
['張三', 15, ''],
['李四', 17, ''],
['王五', 18, ''],
['趙六', 19, ''],
], [
'1.jpg',
'2.jpg',
'3.jpg',
'4.jpg',
]), '使用者列表.xlsx'));
}
}
本作品採用《CC 協議》,轉載必須註明作者和本文連結