【问题标题】:I need to export excel from laravel in the new version for Maatwebsite\Excel 3.1 . Laravel | Excel我需要在 Maatwebsite\\Excel 3.1 的新版本中从 laravel 导出 excel。拉维尔 | Excel
【发布时间】:2022-11-19 16:17:11
【问题描述】:

嗨,我已经看到 Maatwebsite\Excel 的文档并尝试了解导出文件的新方法.. 现在我有了这个用 Laravel 5.1 制作的项目,我将它升级到 8 并解决了大部分问题,但我坚持使用这个 Maatwebsite\ Excel(包)

如何在新版本中重写这段代码..

          ini_set("memory_limit", "-1");
        $requested = (array) $request->columns_to_export;
        return Excel::create('instances-' . date('Y-m-d'), function ($excel) use ($request, $requested) {
            $excel->sheet('instances', function ($sheet) use ($request, $requested) {
                $columns = [];
                foreach ($requested as $key => $column) {
                    if ($column == 'sku') {
                        $columns[] = 'SKU';
                    }
                    if ($column == 'material_name') {
                        $columns[] = 'Material Nmae';
                    }
                    if ($column == 'new_or_used') {
                        $columns[] = 'NEW OR USED';
                    }
                    if ($column == 'barcode') {
                        $columns[] = 'BARCODE';
                    }
                    if ($column == 'asset_or_sale') {
                        $columns[] = 'ASSEST OR SALE';
                    }
                    if ($column == 'cost') {
                        $columns[] = 'COST';
                    }
                }
                if (!empty($columns)) {
                    $sheet->appendRow($columns);
                }
                $this->data['query']->chunk(500, function ($_500) use ($sheet, $request, $requested) {
                    foreach ($_500 as $o => $INSTANCE) {
                        $row     = [];
                        foreach ($requested as $key => $column) {
                            if ($column == 'sku') {
                                $row[]     = (string) '[' . $INSTANCE->sku . ']';
                            }
                            if ($column == 'material_name') {
                                $row[]     = (string) (empty($INSTANCE->Material) ? '------' : $INSTANCE->Material->{'name_' . app()->getLocale()});
                            }
                            if ($column == 'new_or_used') {
                                $row[]     = (string) ($INSTANCE->is_new == 'Y' ? 'NEW' : 'secondhand');
                            }
                            if ($column == 'barcode') {
                                $row[]     = (string) '[' . $INSTANCE->barcode . ']';
                            }
                            if ($column == 'asset_or_sale') {
                                $row[]     = (string) ($INSTANCE->is_asset == 'Y' ? 'ASSET' : ' not asset');
                            }
                            if ($column == 'cost') {
                                $row[]     = (string) round($INSTANCE->cost_of_good_sold, 2);
                            }
                            if ($column == 'last_stock') {
                                $row[]     = (string) (empty($INSTANCE->LastOrderEntry) ? '------' : ($INSTANCE->LastOrderEntry->Stock ? $INSTANCE->LastOrderEntry->Stock->{'name_' . app()->getLocale()} : '------'));
                            }
                            
                           else {
                                        $row[] = '------';
                                    }
                                }
                            }
                        }
                        if (!empty($row)) {
                            $sheet->appendRow($row);
                        }
                    }
                });
            });
        })
            ->download('xlsx');

【问题讨论】:

    标签: excel laravel maatwebsite-excel laravel-upgrade


    【解决方案1】:

    好的对于那些想知道如何在新版本中重写相同代码的人..我只是使用标题:为此

    foreach ($requested as $key => $column) {
                    if ($column == 'sku') {
                        $columns[] = 'SKU';
                    }
                    if ($column == 'material_name') {
                        $columns[] = 'Material Nmae';
                    }
                    if ($column == 'new_or_used') {
                        $columns[] = 'NEW OR USED';
                    }
                    if ($column == 'barcode') {
                        $columns[] = 'BARCODE';
                    }
                    if ($column == 'asset_or_sale') {
                        $columns[] = 'ASSEST OR SALE';
                    }
                    if ($column == 'cost') {
                        $columns[] = 'COST';
                    }
                }
    

    和映射为此

      $this->data['query']->chunk(500, function ($_500) use ($sheet, $request, $requested) {
                    foreach ($_500 as $o => $INSTANCE) {
                        $row     = [];
                        foreach ($requested as $key => $column) {
                            if ($column == 'sku') {
                                $row[]     = (string) '[' . $INSTANCE->sku . ']';
                            }
                            if ($column == 'material_name') {
                                $row[]     = (string) (empty($INSTANCE->Material) ? '------' : $INSTANCE->Material->{'name_' . app()->getLocale()});
                            }
                            if ($column == 'new_or_used') {
                                $row[]     = (string) ($INSTANCE->is_new == 'Y' ? 'NEW' : 'secondhand');
                            }
                            if ($column == 'barcode') {
                                $row[]     = (string) '[' . $INSTANCE->barcode . ']';
                            }
                            if ($column == 'asset_or_sale') {
                                $row[]     = (string) ($INSTANCE->is_asset == 'Y' ? 'ASSET' : ' not asset');
                            }
                            if ($column == 'cost') {
                                $row[]     = (string) round($INSTANCE->cost_of_good_sold, 2);
                            }
                            if ($column == 'last_stock') {
                                $row[]     = (string) (empty($INSTANCE->LastOrderEntry) ? '------' : ($INSTANCE->LastOrderEntry->Stock ? $INSTANCE->LastOrderEntry->Stock->{'name_' . app()->getLocale()} : '------'));
                            }
                            
                           else {
                                        $row[] = '------';
                                    }
                                }
                            }
                        }
                        if (!empty($row)) {
                            $sheet->appendRow($row);
                        }
                    }
                });
            });
    

    所以新代码将是这样的..

    class MaterialInventoreyExport implements
    FromCollection,
    WithMapping,
    WithHeadings,
    ShouldAutoSize
    

    { 使用可导出;

    protected $data;
    protected $requst;
    
    public function __construct($data, $requst)
    {
        $this->data = $data;
        $this->requst = $requst;
        // dd($this->data->get()[0]);
    }
    
    public function headings(): array
    {
        $requested = $this->requst;
        $columns = [];
        foreach ($requested as $key => $column) {
                    if ($column == 'sku') {
                        $columns[] = 'SKU';
                    }
                    if ($column == 'material_name') {
                        $columns[] = 'Material Nmae';
                    }
                    if ($column == 'new_or_used') {
                        $columns[] = 'NEW OR USED';
                    }
                    if ($column == 'barcode') {
                        $columns[] = 'BARCODE';
                    }
                    if ($column == 'asset_or_sale') {
                        $columns[] = 'ASSEST OR SALE';
                    }
                    if ($column == 'cost') {
                        $columns[] = 'COST';
                    }
                }
        return $columns;
    }
    
    
    public function collection()
    {
        return $this->data->get();
    }
    
    public function map($data): array
    {
        $row = null;
        $requested = $this->requst;
        $A = [];
    
    
        foreach ($requested as $key => $column) {
            if ($column == 'sku') {
                $A[] = $data->sku;
            }
            if ($column == 'material_name') {
                $A[] = (string) (empty($data->Material) ? '------' : $data->Material->{'name_' . app()->getLocale()});
            }
            if ($column == 'new_or_used') {
                $A[] =  (string) ($data->is_new == 'Y' ? 'new' : 'secondHand');
            }
            if ($column == 'barcode') {
                $A[] =  (string) '[' . $data->barcode . ']';
            }
            if ($column == 'asset_or_sale') {
                $A[] =  (string) ($data->is_asset == 'Y' ? 'asset' : 'Not Asset');
            }
            if ($column == 'cost') {
                $A[] = (string) round($data->cost_of_good_sold, 2);
            }
            if ($column == 'last_stock') {
                $A[] = (string) (empty($data->LastOrderEntry) ? '------' : 
                ($data->LastOrderEntry->Stock ? $data->LastOrderEntry->Stock->{'name_' . app()->getLocale()} : '------'));
            }
        }
    
        return $A;
    }
    

    }

    【讨论】:

      猜你喜欢
      • 2020-04-03
      • 2019-05-20
      • 1970-01-01
      • 2020-09-04
      • 1970-01-01
      • 2016-11-23
      • 2016-09-02
      • 2021-09-17
      • 1970-01-01
      相关资源
      最近更新 更多