PHPExcel taking an extremely long time to read Excel file

后端 未结 4 697
礼貌的吻别
礼貌的吻别 2021-01-14 08:11

I\'m using PHPExcel 1.7.8, PHP 5.4.14, Windows 7, and an Excel 2007 spreadsheet. The spreadsheet consists of 750 rows, columns A through BW, and is about 600KB in size. This

4条回答
  •  南方客
    南方客 (楼主)
    2021-01-14 08:29

    Resolved (for me) - see note at bottom of this post

    I'm trying to use pretty much identical code on a dedicated quad core server with 16GB of RAM, also running similar versions - PHPExcel 1.7.9 and PHP 5.4.16

    Just creating an empty reader takes 50 seconds!

    // $inputFileType is 'Excel5';
    $objReader = PHPExcel_IOFactory::createReader($inputFileType);
    

    Loading the spreadsheet (1 sheet, 2000 rows, 25 columns) I want to process (readonly) then takes 1802 seconds.

    $objReader->setReadDataOnly(true);
    $objPHPExcel = $objReader->load($inputFileName);
    

    Of the various types of reader I consistently get timings for instantiation as shown below

    foreach(array(
      'Excel2007',     // 350 seconds
      'Excel5',        //  50 seconds
      'Excel2003XML',  //  50 seconds
      'OOCalc',        //  50 seconds
      'SYLK',          //  50 seconds
      'Gnumeric',      //  50 seconds
      'HTML',          // 250 seconds
      'CSV'            //  50 seconds
      ) as $inputFileType) {
      $objReader = PHPExcel_IOFactory::createReader($inputFileType);
    }
    

    Peak memory usage was about 8MB... far less than the 250MB the script has available to it.

    My suspicion WAS that PHPExcel_IOFactory::createReader($inputFileType) was calling something within a loop that's extremely slow under PHP 5.4.x ?

    However the excessive time was due to how PHPExcel names its class names and corresponding file structure. It has an autoloader that converts class names such as *PHPExcel_abc_def* into PHPExcel/abc/def.php for the require statement. Although we had PHPExcel's class directory defined in our include path, our own (already defined) autoloader couldn't handle the manipulation from class name to file name required (it was looking for *PHPExcel_abc_def.php*). When a class file cannot be included, our autoloader will loop 5 times with a 10 second delay to see if the file is being updated and so might become available. So for every PHPExcel class that needed to be loaded we were introducing a delay of 50 seconds before hitting PHPExcel's own autoloader which required the file in fine.

    Now that I've got that resolved PHPExcel is proving to be truly awesome.

提交回复
热议问题