【问题标题】:problem with maatwebsite/excel and \DB::commit();maatwebsite/excel 和 \DB::commit() 的问题;
【发布时间】:2019-11-25 16:03:46
【问题描述】:

我正在尝试将一些数据存储在数据库中,并且在我尝试使用该数据下载和 Excel 文件之后立即。所以我注意到 excel 包阻止了我的提交,并且显然它不允许我将数据存储在数据库中。这是我的代码。 - 我正在使用 Laravel 5.5 -“maatwebsite/excel”:“~2.1.0”,

public function refundTicketAndGenerateExcel($transactions, $table)
{   
    try 
    {   
        \DB::beginTransaction();

            $this->storeRefundData($transactions);
            $response = $this->generateExcel($table);

        \DB::commit();
        return $response;
    } 
    catch (\Exception $e) 
    {   
        \DB::rollback();
        \Log::error($e);
        $result['message'] = $e->getMessage();
        return response()->json($result, 500);
    }
}

public function generateExcel($table)
{   
    Excel::create('Reembolsos', function ($excel) use ($table) {
                    $excel->sheet('Reembolsos', function ($sheet) use ($table) {

                        $FontStyle = array(
                            'font' => array(
                                'name' => 'Arial',
                                'color' => array('rgb' => '000000'),
                                'size' => 11
                            ),
                        );

                        $sheet->loadView($this->path . '.partials.excel', ['table'=>$table]);
                        $sheet->getStyle('A1:K1000')->applyFromArray($FontStyle);
                    });

                })->export('xls');
}

PD:如果我只评论 \DB::beginTransaction() 和 \DB::commit(),一切正常;另一方面,如果我只是评论 Excel::create 块,一切正常;这就是为什么我说 excel 包会阻止我的提交。

提前致谢。

【问题讨论】:

    标签: php laravel export maatwebsite-excel


    【解决方案1】:

    当您调用 export() 时,它会杀死您的脚本。老实说,相当蹩脚的设计:

    protected function _download(Array $headers = [])
    {
        $filename = $this->filename;
        $userAgent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';
        // Just for Microsoft Explore
        if (preg_match('/Trident|Edge/i', $userAgent)) {
            $filename = rawurlencode($filename);
        }
        // Set the headers
        $this->_setHeaders(
            $headers,
            [
                'Content-Type'        => $this->contentType,
                'Content-Disposition' => 'attachment; filename="' . $filename . '.' . $this->ext . '"',
                'Expires'             => 'Mon, 26 Jul 1997 05:00:00 GMT', // Date in the past
                'Last-Modified'       => Carbon::now()->format('D, d M Y H:i:s'),
                'Cache-Control'       => 'cache, must-revalidate',
                'Pragma'              => 'public'
            ]
        );
        // Check if writer isset
        if (!$this->writer)
            throw new LaravelExcelException('[ERROR] No writer was set.');
        // Download
        $this->writer->save('php://output');
        // End the script to prevent corrupted xlsx files
        exit;
    }
    

    https://github.com/Maatwebsite/Laravel-Excel/blob/2.1/src/Maatwebsite/Excel/Writers/LaravelExcelWriter.php#L347-L377

    通过返回 Excel::create() 返回的 writer 对象来解决此问题,并删除 ->export() 部分。

    $writer = Excel::create(blah blah blah); // no->export() now!
    return $writer;
    

    然后在提交后进行导出。

     $writer = $this->generateExcel($table);
     \DB::commit();
     return $writer->export();
    

    【讨论】:

    • 完美运行,你是我的救星@delboy。非常感谢。
    • 很高兴我能帮上忙,玩得开心!
    猜你喜欢
    • 1970-01-01
    • 2023-03-19
    • 2020-03-10
    • 1970-01-01
    • 1970-01-01
    • 2017-11-12
    • 2023-01-25
    • 1970-01-01
    • 2021-09-17
    相关资源
    最近更新 更多