3

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

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;
4

2 回答 2

1

试试PHP XLSXWriter。它是轻量级的,并且在运行时将 xml 表写入磁盘,因此根据我的经验,它的内存使用量要低得多。它不能以任何方式读取电子表格,因此虽然它不如 phpexcel 强大,但它在编写方面做得很好。

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

于 2013-09-12T16:55:12.290 回答
1

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

尽可能将您的样式应用于一系列单元格,而不是每个单独的单元格。范围样式的内存效率要高得多,并且是推荐的方法 这是因为它只为指定范围创建单个样式对象;同时为单个单元格设置样式会为每个单独的使用创建样式记录。

这是两个最大的内存减少因素。

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

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

于 2013-07-29T20:27:36.207 回答