【问题标题】:laravel-excel 3.1 with laravel framework 6.2 and php 7.4laravel-excel 3.1 与 laravel 框架 6.2 和 php 7.4
【发布时间】:2021-03-16 16:49:23
【问题描述】:

在我升级 laravel 版本和 php 版本后,我遇到了 laravel-excel 3.1 的问题,我也从 laravel 得到了一些升级 laravel-excel 的要求。之后,在我使用 2.1 之前,我遇到了 excel-export 不支持新版本(3.1)的问题。任何人都可以帮助更新我上面的当前代码吗?请!谢谢大家!

private function export_customer_invoice_report($customer_invs)
    {
        $data_array = array([
            'Issue Date',
            'Purchase Order Invoice',
            'Bag ID',
            'SKU',
            'Color-Size',
            'QTY',
            'Sale Price',
            'Additional Discount',
            'Actual Sale Price',
            'Delivery Fee',
            'Customer Balance',
            'Pre-Paid Amount',
            'Supplier Actual Price',
            'Remark']);

        foreach ($customer_invs as $key => $value) {
            $product_variants = Helper::get_pro_option_id($value->order_item_id);
            $doubleSpace = strip_tags($product_variants);
            $singleSpace_product_variants = str_replace("\n ", "", $doubleSpace);
            $issue_date = Helper::dateTimeDisplay($value->created_at);
            $additional_dis = $value->additional_discount_percent ? $value->additional_discount_percent : 0;
            $sale_price_after_disc = ($value->unit_price * $value->count_item) - ((($value->unit_price * $value->count_item) * $value->discount_percent) / 100);
            $total_sale_price_discount_addit = ($sale_price_after_disc * $value->additional_discount_percent) / 100;
            $actual_sale_price = $sale_price_after_disc - $total_sale_price_discount_addit;

            // check if supplier actual price is zero, take from supplier price instead
            $supplier_actual_price = $value->supplier_order_actual_price > 0
            ? $value->supplier_order_actual_price
            : $value->supplier_price;
            $data_list = array(
                $issue_date,
                $value->invoice_id,
                $value->bag_id,
                OrderItem::getProductSKU($value->order_item_id),
                $singleSpace_product_variants,
                $value->count_item,
                '$' . number_format($sale_price_after_disc, 2),
                '%' . $additional_dis,
                '$' . number_format($actual_sale_price, 2),
                '$' . number_format($value->delivery_price, 2),
                '$' . number_format($value->customer_balance, 2),
                '$' . number_format($value->prepaid_amount, 2),
                '¥' . number_format($supplier_actual_price, 2),
                $value->note,
            );
            array_push($data_array, $data_list);
        }

        Excel::create('customer_invoice_report', function ($excel) use ($data_array) {
            // Set the title
            $excel->setTitle('no title');
            $excel->setCreator('no no creator')->setCompany('no company');
            $excel->setDescription('report file');
            $excel->sheet('sheet1', function ($sheet) use ($data_array) {
                $sheet->cells('A1:M1', function ($cells) {
                    $cells->setBackground('#e7e7e7');
                    $cells->setFontWeight('bold');
                });
                $row = 1;
                $startRow = -1;
                $previousKey = '';

                foreach ($data_array as $index => $value) {
                    if ($startRow == -1) {
                        $startRow = $row;
                        $previousKey = $value[2];
                    }
                    $sheet->setCellValue('A' . $row, $value[0]);
                    $sheet->setCellValue('B' . $row, $value[1]);
                    $sheet->setCellValue('C' . $row, $value[2]);
                    $sheet->setCellValue('D' . $row, $value[3]);
                    $sheet->setCellValue('E' . $row, $value[4]);
                    $sheet->setCellValue('F' . $row, $value[5]);
                    $sheet->setCellValue('G' . $row, $value[6]);
                    $sheet->setCellValue('H' . $row, $value[7]);
                    $sheet->setCellValue('I' . $row, $value[8]);
                    $sheet->setCellValue('J' . $row, $value[9]);
                    $sheet->setCellValue('K' . $row, $value[10]);
                    $sheet->setCellValue('L' . $row, $value[11]);
                    $sheet->setCellValue('M' . $row, $value[12]);
                    $sheet->setCellValue('N' . $row, $value[13]);
                    $nextKey = isset($data_array[$index + 1]) ? $data_array[$index + 1][2] : null;

                    if ($row >= $startRow && (($previousKey != $nextKey) || ($nextKey == null))) {
                        $cellToMergeJ = 'J' . $startRow . ':J' . $row;
                        $cellToMergeK = 'K' . $startRow . ':K' . $row;
                        $cellToMergeL = 'L' . $startRow . ':L' . $row;
                        $sheet->mergeCells($cellToMergeJ);
                        $sheet->mergeCells($cellToMergeK);
                        $sheet->mergeCells($cellToMergeL);
                        $sheet->cells('J' . $startRow . ':J' . $row, function ($cellsJ) {$cellsJ->setValignment('center');});
                        $sheet->cells('K' . $startRow . ':K' . $row, function ($cellsK) {$cellsK->setValignment('center');});
                        $sheet->cells('L' . $startRow . ':L' . $row, function ($cellsL) {$cellsL->setValignment('center');});
                        $startRow = -1;
                    }
                    $row++;
                }
            });
        })->download('xlsx');
    }

【问题讨论】:

  • “我遇到了问题”没什么好说的。为什么不更新到当前版本的 Laravel 而不是几年前的版本?
  • @miken32 Laravel 6.x 是最新的 LTS 版本(2019 年 9 月发布)。一些组织可能更喜欢坚持 LTS 版本,这样他们就不必每 6 个月升级一次框架。这是一个取决于组织的需求和资源的决定。 LTS vs latest 是一个超出这个问题范围的辩论,但是,因为如果升级到 Laravel 8,OP 会遇到完全相同的问题。:)

标签: laravel laravel-excel laravel-6.2


【解决方案1】:

我们只是为一个项目这样做。从 2.x 迁移到 3.x 时,laravel-excel 包非常不同。范式从简单的实用程序转变为将进出口表示为自包含的类。我建议将用于生成导出的所有逻辑从控制器移到此类中,但如果您愿意,可以将其保留在控制器中。以下是您需要做的总体思路:

首先,创建一个导出类:

php artisan make:export CustomerInvoiceReport

然后,编辑新创建的类,它应该在app/Exports/CustomerInvoiceReport.php 中。如果$customer_invs 是一个数组,则实现FromArray 接口。如果是 Laravel 集合,请实现 FromCollection 接口。此外,您可以使用Exportable trait 将download() 方法添加到类(我们稍后会用到)。

对于这个例子,我假设它是一个数组。您需要修改 array() 方法,以便它返回您修改后的数组(我将把它留给您):

namespace App\Exports;

use App\Invoice;
use Maatwebsite\Excel\Concerns\FromArray;
use Maatwebsite\Excel\Concerns\Exportable;

class CustomerInvoiceExport implements FromArray
{
    use Exportable;

    protected $invoices;

    public function __construct(array $invoices)
    {
        $this->invoices = $invoices;
    }

    public function array(): array
    {
        // insert/move your logic for modifying the array here

        return $this->invoices;
    }
}

最后,在您的控制器中,简单地实例化您的导出类并返回下载:

use App\Exports\CustomerInvoiceReport;

...

private function export_customer_invoice_report($customer_invs)
{
    return (new CustomerInvoiceReport($customer_invs))
        ->download('customer_invoice_report.xlsx');
}

您可以在导出类中控制其他内容,例如列headers 和formatting。有关详细信息,请参阅documentation on creating exports。

【讨论】:

  • 如何像我目前的情况一样合并单元格?谢谢@wunch
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-25
  • 1970-01-01
  • 2020-08-18
  • 1970-01-01
  • 2014-01-05
  • 1970-01-01
相关资源
最近更新 更多