首页 > 解决方案 > 我可以在 laravel 的 maatwebsite excel 中添加公式吗

问题描述

我需要使用 laravel 向 maatwesite excel 表格中的单元格添加公式请帮我解决这个问题是否可以添加如果可能请建议代码

标签: laravelmaatwebsite-excel

解决方案


添加公式的方法之一是使用WithMapping

例如,您需要在单元格中添加值,如A2+1

use App\Customer;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithMapping;

class CustomersExportFormulas implements FromCollection, WithMapping
{
    public function collection()
    {
        return Customer::all();
    }


    public function map($customer): array
    {
        return [
             $customer->id,
            '=A2+1',
            $customer->first_name,
            $customer->last_name,
            $customer->email,
        ];
    }
}

推荐阅读