当它非常慢时如何优化 phpExcel?

How to optimal phpExcel when it's very slow?

我正在使用 phpExcel 导出 excel 文件。我输出 2000 行 并且它的执行时间大约是 10 秒 。问题是当我输出 20,000 rows 时出现错误: ERROR TIME OUT 。一开始:max_execution_time = 30,我设置回max_execution_time = 60(这个值等于主服务器上的值)。现在有一种方法可以最大限度地减少文件导出时间,因为数据有时非常大……高达几十万行。请帮我。谢谢。对不起,我的英语不好。


         // array example
        // 20.000 rows
        $data = [
            [
                'name' => 'hello',
                'address' => 'usa',
                'birthday' => '2021-04-30'
            ],
            [
                'name' => 'hello',
                'address' => 'usa',
                'birthday' => '2021-04-30'
            ],
        ];
        // Sample processing code
        $exRow = 2;
        foreach ($data as $val) {
            for ($i = 0; $i < 12; $i++) { // set title
                $excel->getActiveSheet()->setCellValueByColumnAndRow($i, $exRow, $val);
            }
        }
        header('Content-type: application/vnd.ms-excel');
        header('Content-Disposition: attachment; filename="data.xls"');
        PHPExcel_IOFactory::createWriter($excel, 'Excel2007')->save('php://output');

你只需要在 php.ini 文件中增加 max_execution_time。如果您无法访问,只需创建一个 php.iniuser.ini 并将此文本放在那里

max_execution_time=500

您也可以使用 yield 键添加 excel 行。这是帮助您通过惰性渲染

渲染 excel 行

访问这个link