PHPExcel very slow - ways to improve?

后端 未结 8 1376
孤街浪徒
孤街浪徒 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++;
    }
    
    0 讨论(0)
  • 2020-12-07 14:15

    For a XLSX export with columns a - amj (~800) and only ~50 rows I also ran into the 30 second boundary. To test my program, I limited the amount of rows processed to just 7, which worked in 25 sec.

    1. going from individual $objPHPExcel->getActiveSheet() to $sheet (first advice) it actually increased the time on a limited amount of rows from 25 sec to 26 sec.

    2. What really helped me was replacing all my getHighestDataColumn() with a simple $column_nr variable that is incremented in PHP, I went from 26 sec to 7 sec.

    After that I was able to process all 50 rows in 11 sec.

    0 讨论(0)
提交回复
热议问题