使用 Laravel-Excel 和流的方法匯出 Excel

iamcyan發表於2018-09-30

1、使用laravel-excel擴充套件包匯出

擴充套件包的3.0的版本和2.0相比做了很多的改動,個人感覺更容易使用了。擴充套件包給出了很多基於query的匯出,檢視的匯出。下面例子為基於array的匯出,其他的檢視文件即可。

匯出類:

    use Maatwebsite\Excel\Concerns\FromCollection;
    use Maatwebsite\Excel\Concerns\Exportable;
    use Maatwebsite\Excel\Concerns\WithHeadings;
    class UsersExport implements FromCollection, WithHeadings
    {
        use Exportable;

        private $data;
        private $headings;

        //資料注入
        public function __construct($data, $headings)
        {
            $this->data = $data;
            $this->headings = $headings;
        }

        //實現FromCollection介面
        public function collection()
        {
            return collect($this->data);
        }

        //實現WithHeadings介面
        public function headings(): array
        {
            return $this->headings;
        }

    }

控制器中匯出

    namespace App\Http\Controllers;

    use Maatwebsite\Excel\Facades\Excel;
    use App\Exports\UsersExport;
    class UsersController extends Controller
    {
        public function export()
        {
            $data = [
                [
                    'name' => 'cheng',
                    'email' => 'cheng111'
                ],
                [
                    'name' => 'cheng',
                    'email' => 'cheng111'
                ],
            ];

            $headings = [
                'name',
                'email'
            ];
            return Excel::download(new UsersExport($data, $headings), 'users.csv');

        }
    }

2、使用流的形式匯出

    public function export($params)
        {
            set_time_limit(0);

            $columns = ['欄位名'];

            $fileName = 'GPS管理明細' . '.csv';
            //設定好告訴瀏覽器要下載excel檔案的headers
            header('Content-Description: File Transfer');
            header('Content-Type: application/vnd.ms-excel');
            header('Content-Disposition: attachment; filename="'. $fileName .'"');
            header('Expires: 0');
            header('Cache-Control: must-revalidate');
            header('Pragma: public');

            $fp = fopen('php://output', 'a');//開啟output流
            mb_convert_variables('GBK', 'UTF-8', $columns);
            fputcsv($fp, $columns);     //將資料格式化為CSV格式並寫入到output流中

            $num = $this->getExportNum($params);
            $perSize = 2000;//每次查詢的條數
            $pages   = ceil($num / $perSize);

            for($i = 1; $i <= $pages; $i++) {
                $list = $this->getUnitExportData($params, $i, $perSize);

                foreach($list as $item) {
                    $rowData[] = $this->switchDeviceMakerToDesc($item['device_maker']);
                    .
                    .
                    .
                    mb_convert_variables('GBK', 'UTF-8', $rowData);
                    fputcsv($fp, $rowData);
                    unset($rowData);
                }
                unset($accessLog);//釋放變數的記憶體

                ob_flush();     //重新整理輸出緩衝到瀏覽器
                flush();        //必須同時使用 ob_flush() 和flush() 函式來重新整理輸出緩衝。
            }
            fclose($fp);
            exit();
        }

相關文章