PHPExcel very slow - ways to improve?

后端 未结 8 1399
孤街浪徒
孤街浪徒 2020-12-07 13:28

I am generating reports in .xlsx using PHPExcel. It was okay in the initial testing stages with small data sets (tens of rows, 3 sheets), but now when using it on a real pro

8条回答
  •  一个人的身影
    2020-12-07 14:15

    I was running into the same issue - had about 450 rows with 11 columns of data that I was trying to write, and I kept running up against the 30-second timeout. I was able to get the execution time down to 2 seconds or less by adding all of my new rows in bulk, and then going through and setting the cell content after the fact. In other words, I insert 450 rows in one call to insertNewRowBefore(), and then loop through and set content within those rows later.

    Like so:

    $num_rows = count($output_rows);
    $last_row = $sheet->getHighestRow();
    $row = $last_row + 1;
    $sheet->insertNewRowBefore($row, $num_rows);
    // Now add all of the rows to the spreadsheet
    foreach($output_rows as $line) {
        $i = 0;
        foreach($line as $val) {
            // Do your setCellValue() or setCellValueByColumnAndRow() here
            $i++;
        }
        $row++;
    }
    

提交回复
热议问题