Laravel Excel 匯出精美的excel圖文檔案

本篇文章以一個需求案例提供方向給需要匯出Excel的朋友參考,內文不會詳細說明文件細項,只針對有用到的method說明

參考連結

https://docs.laravel-excel.com/3.0/exports/collection.html

https://phpoffice.github.io/PhpSpreadsheet/1.4.1/PhpOffice/PhpSpreadsheet/Worksheet/Drawing.html

https://laracasts.com/discuss/channels/laravel/storing-image-file-retrieved-from-external-url?page=1

https://laraveldaily.com/laravel-excel-export-formatting-and-styling-cells/

https://phpspreadsheet.readthedocs.io/en/latest/topics/recipes/#setting-a-columns-width

案例需求:

  1. 將資料匯出到excel須包含圖片以及文字
  2. 必須設定圖片大小與cell大小
  3. 要匯出的圖片只有url link並不存在伺服器端

一. 透過Laravel Excel 匯出資料

根據官網提供的步驟如下

透過composer安裝 Laravel Excel

在config/app.php中加入provider 以及Alias

最後,publish config

二. 匯出一個基本的excel資料

Laravel Excel 提供多種快速的匯出功能,本次介紹最容易上手的From View

From View,共名思義就是透過view blade的表格資料直接mapping到excel table中

在App\Exports 建立一個匯出資料的class

簡單說明就是透過Invoice Eloquent取出所有資料,並且放入export.invoice view中

接著在 resource\view\export 中建立invoice.blade.php,用來顯示資料

內容其實就是一般的view blade

資料與view都準備好之後就可以在controller中呼叫來下載了

在usercontroller中呼叫Excel::download,並且new InvoicesExport Class,第二個參數是指定下載的檔案名稱

到目前為止已經透過 Laravel Excel 間單的匯出了資料

三. 透過Drawing 匯出圖片到excel中,並且設定好cell長寬

要在excel中插入圖片必須透過PhpSpreadsheet的Drawing 將圖片繪入

範例如下

簡單說明一下邏輯,首先我們在第二部分撰寫的InvoicesExport class中必須新增一個function “registerEvents”,用來調整excel的內容,並且透過AfterSheet 來控制sheet中cell的大小、顏色、邊筐等等設定

接著針對幾個設定項目來做說明

**注意**

registerEvents 執行時間會在view 之前,所以無法在view中先整理好資料邏輯再由registerEvents來排列excel規格

四. 遠端圖片該如何插入Excel中

Drawing只能插入本地端的圖片,但是本次任務是將遠端圖片插入excel中,因此必須先下載圖片儲存後才能插入excel中

其實只要透過file_get_contents,去得遠端內容後,再透過Storage就可以輕鬆地將圖片儲存在本地端

透過Storage::pu,檔案會儲存在Storage\app 中

串連以上,就可以透過 Laravel Excel 將遠端圖片插入到excel sheet的指定cell中了!