美文网首页
laravel+vue 前后端分离开发excel导出

laravel+vue 前后端分离开发excel导出

作者: IT宝哥哥 | 来源:发表于2020-08-11 12:02 被阅读0次
    1. 使用composer require maatwebsite/excel安装excel导出扩展;
    2. 导出代码php

    特别注意:use Maatwebsite\Excel\Concerns\WithStyles;//这个扩展如果使用composer安装的话没有文件,需要手动复制,包括WithColumnWidths这个接口

    <?php
    
    namespace App\Exports;
    
    use Illuminate\Support\Collection;
    use Maatwebsite\Excel\Concerns\FromCollection;
    use Maatwebsite\Excel\Concerns\ShouldAutoSize;
    //use Maatwebsite\Excel\Concerns\WithStyles;//这个扩展如果使用composer安装的话没有文件,需要手动复制,包括WithColumnWidths这个接口
    //use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
    
    class UOrderExport implements FromCollection,ShouldAutoSize/*,WithStyles*/
    {
        public $data;
        public $header;
    
        /**
         * UOrderExport constructor.
         * @param Collection $data
         * @param array $header
         */
        public function __construct(array $header, Collection $data)
        {
            $this->data = $data;
            $this->header = $header;
        }
    
        public function collection()
        {
            return $this->createCollection();
        }
    
        public function createCollection()
        {
            return collect([$this->header])->concat([$this->data]);
        }
    
    /*    public function styles(Worksheet $sheet)
        {
            return [
                // Style the first row as bold text.
                1    => ['font' => ['bold' => true]],
            ];
        }*/
    }
    
    
    //$total 是数组,如果是Collection要转换一下
    private function export(array $total): \Illuminate\Http\JsonResponse
        {
            $header = ['订单号', '商品名称', '规格', '单价', '数量', '小计'];
            $fileName = date('YmdHis') . '.xlsx';
            foreach ($total as $k => $v) {
                $total[$k] = collect($v)->toArray();//total里面是对象,转换为数组
                $tmp = [];
                $tmp['order_num'] = $total[$k]['order_num'];
                $tmp['ugoods_name'] = $total[$k]['ugoods_name'];
                $tmp['spec_name'] = $total[$k]['spec_name'];
                $tmp['cash'] = $total[$k]['cash'];
                $tmp['number'] = $total[$k]['number'];
                $tmp['total'] = $tmp['cash'] * $tmp['number'];
                $tmp['is_delivery'] = $total[$k]['is_delivery'] ? '已发货' : '未发货';
                $total[$k] = $tmp;
            }
            if (!Excel::store(new UOrderExport($header, collect($total)), 'public/excel/' . $fileName)) {
                return $this->status('导出失败', null, 400);
            } else {
                return $this->status('导出成功', $fileName, 200);
            }
        }
    
    1. 前端如何下载?
      第一步会在服务器的storage/excel目录下创建excel文件,前端直接打开那个url即可,配合target
            methods: {
                handleExport() {
                    let query = Object.assign(this.query, {export: true});
                    orderList(query).then(res => {
                        if (res.code === 200) {
                            //打开一个窗口,即是下载
                            window.open(`${this.baseUrl}storage/excel/${res.data}`, '_blank');
                        } else {
                            this.$message.error(res.msg);
                        }
                    })
                },
            },
    

    相关文章

      网友评论

          本文标题:laravel+vue 前后端分离开发excel导出

          本文链接:https://www.haomeiwen.com/subject/newedktx.html