简体   繁体   中英

laravel maatwebsite/excel formula error unexpected ","

I am fetching results from database and then creating excel sheet from it.

This stores the file in storage path.

public function downloadResults(array $filterArr) {
        unset($queryArr);
        $start = 0;
        $limit = 10000;
        
        $response = $this->getFilterResults($filterArr, true, $start, $limit);
        $resultHits = $response['resultHits'];
        # if results are empty, throw an exception
        if($resultHits->isEmpty())
            throw new \Exception("No results found for given filter.", 1);
        
        # otherwise continue with the download process...
        $exportArr = [];
        foreach($resultHits as $key=>$value) {
            $list = (array) $value;
            array_push($exportArr, $list);
        }
        $type = 'xls';
        $export = Excel::create('result_set_download_'.time(), function($excel) use ($exportArr) {
            $excel->sheet('mysheet', function($sheet) use ($exportArr) {
                $sheet->fromArray($exportArr);
            });
        })->store($type, storage_path('app/public/exports'), true);

        return $export;
    }

If export and store is successful, then stored filename is sent back to browser, else error message is sent.

try {
            $exportArr = $this->product->downloadResults($filterArr);
            $status = true;
            $jsonArr['filename'] = $exportArr['file'];            
        } catch (\Exception $e) {
            $status = false;            
            $jsonArr['message'] = $e->getMessage();            
        } finally {
            $jsonArr['success'] = $status;
            echo json_encode($jsonArr);
        }

This is working fine until now, but in case of few exports I get this error:

mysheet!Z2898 -> Formula Error: Unexpected ,

There's not any calculation done while exporting.

I also found a similar post here Laravel Excel Formula Error: Unexpected operator '='

And this is the answer to that post:

// Enable calculation
$reader->calculate();

// Disable calculation
$reader->calculate(false);

But I am confused as to where to put this.

If you want to enable any formula, first export your laravel-excel config

php artisan vendor:publish

Search Maatwebsite then publish it. In my case is 12. Type 12 then Enter.

Maat网站提供者

That will copy a config file to your config folder.

在此处输入图像描述

Then from config/excel.php change this line:

excel.php

pre_calculate_formulas

'pre_calculate_formulas' => true, // default is false

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM