doutan5724
2015-05-05 04:35
浏览 57
已采纳

PHPExcel按原样写行,没有任何计算/样式

Is there a way to tell PHPExcel to just write rows supplied from an array, without doing any calculation / apply styling / any other thing it does while writing OR when using fromArray ?

Need this for performance.

        $inputFileName = 'client_files/sample.xlsx';
        $objPHPExcel = PHPExcel_IOFactory::load($inputFileName);

        $objPHPExcel->getSheet(0)->setCellValue('D2', '@' . $user . ' followers');
        $objPHPExcel->getSheet(0)->fromArray(
            $followersData,
            NULL,
            'A5'
        );
        $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
        $objWriter->setPreCalculateFormulas(false);

        $objWriter->save(FINAL_FOLDER . '/' . $line[0] . '.xlsx');

Memory consumption isn't an issue. But the above is just taking too much time (2 minutes with 2700 rows)

the ->save() call takes 93 seconds. The ->fromArray() takes 53 seconds

Also is there any other wayy faster Excel library that allows loading existing xlsx and then writing to it ?

Thanks

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongze8698 2015-05-06 12:32
    已采纳

    I did a bunch of things that resulted in wayyyy faster performance.

    1. ran the script outside the IDE
    2. set memory limit to 3GB
    3. Used a different version of PHP
    4. Fixed memory leak

      $objPHPExcel->disconnectWorksheets() ;
      unset($objPHPExcel) ;
      

    I am not sure what solved the issue..

    点赞 打赏 评论
  • doucu7525 2015-05-06 00:27

    You can try using Spout. If you don't care about styling/calculation, it should solve your performance problem (it takes only a few seconds).

    Something along these lines should work:

    $inputFileName = 'client_files/sample.xlsx';
    $reader = ReaderFactory::create(Type::XLSX);
    $reader->open($inputFileName);
    
    $outputFileName = FINAL_FOLDER . '/' . $line[0] . '.xlsx';
    $writer = WriterFactory::create(Type::XLSX);
    $writer->openToFile($outputFileName);
    
    $reader->nextSheet();
    
    $rowCount = 0;
    while ($reader->hasNextRow()) {
        $row = $reader->nextRow();
    
        if ($rowCount === 1) {
            $row[1] = '@' . $user . ' followers';
        }
    
        $followersDataForCurrentRow = $followersData[$rowCount];
        $columnIndexStart = 4; // To add stuff in the 5th column
    
        foreach ($followersDataForCurrentRow as $followerValue) {
            $row[$columnIndexStart] = $followerValue;
            $columnIndexStart++;
        }
    
        $writer->addRow($row);
    
        $rowCount++;
    }
    
    $reader->close();
    $writer->close();
    
    点赞 打赏 评论

相关推荐 更多相似问题