Search code examples
phpsymfonyphpexcel

How to optimal phpExcel when it's very slow?


I am doing exporting excel file using phpExcel. I output 2000 rows and its execution time is about 10 seconds . The problem is that when I output 20,000 rows it gets error: ERROR TIME OUT . At first: max_execution_time = 30, I set it back to max_execution_time = 60 (This value is equal to the value on the main server). Now is there a way to minimize file export time, since the data is sometimes very large..Up to a few hundred thousand rows. Please help me. Thanks. Sorry my english is not good.

  • Here is my code :

         // 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');

Solution

  • you just need to increase max_execution_time in php.ini file. if you don't access to do this just can make a php.ini or user.ini and put this text on there

    max_execution_time=500

    also you can use yield key to add excel row. this is help you render excel rows by lazy rendering

    visit this link