PHPExcel内存耗尽

时间:2013-07-29 14:26:26

标签: caching memory-leaks phpexcel

我使用PHPExcell导出包含20列的4000行。我想在工作表中每隔一行着色,然后我写了一个循环,但它返回我

FAtal Error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 24 bytes) in D:\Data\xampp\htdocs\Classes\PHPExcel\Style\Supervisor.php

我一直在寻找2个小时来解决问题,但我失败了。我发现的每个主题都很旧,不论是2010年还是更早。我试图使用缓存,这是谎言

$cacheMethod = PHPExcel_CachedObjectStorageFactory:: cache_to_phpTemp;
$cacheSettings = array( ' memoryCacheSize ' => '8MB');
PHPExcel_Settings::setCacheStorageMethod($cacheMethod, $cacheSettings);   

但这没有用。我知道如何解决这个问题?

最后我的循环用于生成xlsx并格式化单元格:

$i = 2;
    foreach($table as $row){
    $objPHPExcel->getActiveSheet()
                ->setCellValue('A'.$i, $row[1])
                ->setCellValue('B'.$i, $row[2])
                ->setCellValue('C'.$i, $row[3])
                ->setCellValue('D'.$i, $row[4])
                ->setCellValue('E'.$i, $row[5])
                ->setCellValue('F'.$i, $row[6])
                ->setCellValue('G'.$i, $row[7])
                ->setCellValue('H'.$i, $row[8])
                ->setCellValue('I'.$i, $row[9])
                ->setCellValue('J'.$i, $row[10])
                ->setCellValue('K'.$i, $row[11])
                ->setCellValue('L'.$i, $row[12])
                ->setCellValue('M'.$i, $row[13])
                ->setCellValue('N'.$i, $row[14])
                ->setCellValue('O'.$i, $row[15])
                ->setCellValue('P'.$i, $row[16])
                ->setCellValue('Q'.$i, $row[17])
                ->setCellValue('R'.$i, $row[20])
                ->setCellValue('S'.$i, $row[21])
                ->setCellValue('T'.$i, $row[22]);
        // Color every second row
    if($i%2!=0){
    $objPHPExcel->getActiveSheet()->getStyle('A'.$i.':T'.$i)->getFill()->applyFromArray(
    array(
    'type'       => PHPExcel_Style_Fill::FILL_SOLID,
    'startcolor' => array('rgb' => 'e8f4ff')
    ));
    }

    //color column "O"
    if($i%2==0){
    $objPHPExcel->getActiveSheet()->getStyle('O'.$i)->getFill()->applyFromArray(
    array(
    'type'       => PHPExcel_Style_Fill::FILL_SOLID,
    'startcolor' => array('rgb' => 'fffacd')
    ));
    }
    else{
    $objPHPExcel->getActiveSheet()->getStyle('O'.$i)->getFill()->applyFromArray(
    array(
    'type'       => PHPExcel_Style_Fill::FILL_SOLID,
    'startcolor' => array('rgb' => 'fff9bd')
    ));
    }

    // color column "N"
    if($i%2==0){
    $objPHPExcel->getActiveSheet()->getStyle('N'.$i)->getFill()->applyFromArray(
    array(
    'type'       => PHPExcel_Style_Fill::FILL_SOLID,
    'startcolor' => array('rgb' => 'eaffee')
    ));
    }
    else{
    $objPHPExcel->getActiveSheet()->getStyle('N'.$i)->getFill()->applyFromArray(
    array(
    'type'       => PHPExcel_Style_Fill::FILL_SOLID,
    'startcolor' => array('rgb' => 'ccffcc')
    ));
    }           


    $i++;
    }
编辑::我发布完整代码因为我不知道我能做什么。我创建新的phpexcel对象,然后导入空模板,第一行作为带有colnames的标题。休息单元格全部为空,具有预设样式。我设置了10 000行的样式。但它仍然给我带来同样的错误。有代码:

            error_reporting(E_ALL);
    ini_set('display_errors', TRUE);
    ini_set('display_startup_errors', TRUE);
    date_default_timezone_set('Europe/London');

    if (PHP_SAPI == 'cli')
        die('This example should only be run from a Web Browser');

    /** Include PHPExcel */
    require_once '../Classes/PHPExcel.php';



    // Create new PHPExcel object
    $objPHPExcel = new PHPExcel();

    $objReader = PHPExcel_IOFactory::createReader('Excel2007');
    $objReader = $objReader->load('components/com_uhlist/Classes/template.xlsx');
    $template = $objReader->getActiveSheet();

    $objPHPExcel->addExternalSheet($template);
    $objPHPExcel->setActiveSheetIndex(1);

    $user = JFactory::getUser();
    $username = $user->get('username');
    // Set document properties
    $objPHPExcel->getProperties()->setCreator($username)
                                 ->setLastModifiedBy($username)
                                 ->setTitle("User & Host List");



    // Add some data
    // Aktywowanie arkusza
    //$objPHPExcel->setActiveSheetIndex(-0);


    // Export danych
    $db = JFactory::getDBO();
    $query = 'SELECT * FROM '.$db->getPrefix().'users_n_hosts';
    $db->setQuery($query);
    $table = $db->loadRowList();

    $i = 2;
    foreach($table as $row){
    $objPHPExcel->getActiveSheet()
                ->setCellValue('A'.$i, $row[1])
                ->setCellValue('B'.$i, $row[2])
                ->setCellValue('C'.$i, $row[3])
                ->setCellValue('D'.$i, $row[4])
                ->setCellValue('E'.$i, $row[5])
                ->setCellValue('F'.$i, $row[6])
                ->setCellValue('G'.$i, $row[7])
                ->setCellValue('H'.$i, $row[8])
                ->setCellValue('I'.$i, $row[9])
                ->setCellValue('J'.$i, $row[10])
                ->setCellValue('K'.$i, $row[11])
                ->setCellValue('L'.$i, $row[12])
                ->setCellValue('M'.$i, $row[13])
                ->setCellValue('N'.$i, $row[14])
                ->setCellValue('O'.$i, $row[15])
                ->setCellValue('P'.$i, $row[16])
                ->setCellValue('Q'.$i, $row[17])
                ->setCellValue('R'.$i, $row[20])
                ->setCellValue('S'.$i, $row[21])
                ->setCellValue('T'.$i, $row[22]);       
    $i++;
    }



    // Nazwa arkusza
    $objPHPExcel->getActiveSheet()->setTitle('User & Host');


    // Set active sheet index to the first sheet, so Excel opens this as the first sheet
    $objPHPExcel->setActiveSheetIndex(0);


    // Redirect output to a client’s web browser (Excel2007)
    header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
    header('Content-Disposition: attachment;filename="User&Host_'.date('d-m-Y').'.xlsx"');
    header('Cache-Control: max-age=0');

    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
    $objWriter->save('php://output');
    exit;

    }

最新代码::将数据插入模板:

            $objPHPExcel = PHPExcel_IOFactory::createReader('Excel2007');
    $objPHPExcel = $objPHPExcel->load('components/com_uhlist/Classes/template.xlsx');
    $objPHPExcel->setActiveSheetIndex(0);

    $user = JFactory::getUser();
    $username = $user->get('username');
    // Set document properties
    $objPHPExcel->getProperties()->setCreator($username)
                                 ->setLastModifiedBy($username)
                                 ->setTitle("User & Host List");

    // Export danych
    $db = JFactory::getDBO();
    $query = 'SELECT * FROM '.$db->getPrefix().'autocad_users_n_hosts';
    $db->setQuery($query);
    $table = $db->loadRowList();

    $i = 2;
    foreach($table as $row){
    $objPHPExcel->getActiveSheet()
                ->setCellValue('A'.$i, $row[1])
                ->setCellValue('B'.$i, $row[2])
                ->setCellValue('C'.$i, $row[3])
                ->setCellValue('D'.$i, $row[4])
                ->setCellValue('E'.$i, $row[5])
                ->setCellValue('F'.$i, $row[6])
                ->setCellValue('G'.$i, $row[7])
                ->setCellValue('H'.$i, $row[8])
                ->setCellValue('I'.$i, $row[9])
                ->setCellValue('J'.$i, $row[10])
                ->setCellValue('K'.$i, $row[11])
                ->setCellValue('L'.$i, $row[12])
                ->setCellValue('M'.$i, $row[13])
                ->setCellValue('N'.$i, $row[14])
                ->setCellValue('O'.$i, $row[15])
                ->setCellValue('P'.$i, $row[16])
                ->setCellValue('Q'.$i, $row[17])
                ->setCellValue('R'.$i, $row[20])
                ->setCellValue('S'.$i, $row[21])
                ->setCellValue('T'.$i, $row[22]);       
    $i++;
    }

    // Redirect output to a client’s web browser (Excel2007)
    header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
    header('Content-Disposition: attachment;filename="User&Host_'.date('d-m-Y').'.xlsx"');
    header('Cache-Control: max-age=0');

    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
    $objWriter->save('php://output');
    exit;

2 个答案:

答案 0 :(得分:1)

您是否尝试过其他任何缓存方法? SQLite或SQLite 3的内存效率最高,因为所有其他方法都保留了内存中的单元索引。

尽可能将样式应用于一系列细胞而不是每个细胞。范围样式的内存效率更高,是推荐的方法 这是因为它只为指定范围创建一个样式对象;样式化单个单元格可为每个用户创建样式记录。

这是两个最大的记忆减少因素。

使用已定义的样式创建初始模板,并使用工作表duplicateStyle()或duplicateStyleArray()方法也可能有所帮助 - 特别是当您可以复制到备用行的单元格范围时 - 因为这不会创建新样式对象但重用现有定义的样式对象。

如果做不到这一点,您可能仍需要增加php内存设置。

答案 1 :(得分:1)

试试PHP XLSXWriter。它是轻量级的,并且随着它的推移将xml表写入磁盘,因此根据我的经验,内存使用率要低得多。它不能以任何方式阅读电子表格,因此虽然它没有像phpexcel那样强大,但它在写作方面做得很好。

phpexcel库在将所有内容写入内存之前将所有内容存储在内存中,并且按设计它具有较高的内存使用率。