dsbo44836129 2012-09-03 16:34
浏览 40
已采纳

PHPExcel内存问题 - 更多想法?

Iam trying to write an Excel2007 File with Zend Framework and PHPExcel. I know i could increase the PHP Memory Limit.. but is there no other way?

This is my Code so far, it works well with 5000 Rows and 77 Columns but i need 12000 rows :-) Memory is set to 128MB

    $cacheMethod = PHPExcel_CachedObjectStorageFactory::cache_to_sqlite;
    PHPExcel_Settings::setCacheStorageMethod($cacheMethod);

    $db = Zend_Db_Table_Abstract::getDefaultAdapter();
    $phpExcel = new PHPExcel();

    // set headers
    $select = $db->select();
    $select->from('IMPORT')->limit(1);
    $data = $select->query()->fetchAll();
    $columns = array_keys($data[0]);
    $phpExcel->setActiveSheetIndex(0);
    $colCNT = 1;

    foreach ($columns as $column) {
        $letter = Nc_Utils::getNameFromNumber($colCNT);
        $phpExcel->getActiveSheet()->SetCellValue($letter . '1', $column);
        $colCNT++;
    }

    unset($select);
    unset($data);

    $sql = 'SELECT * FROM IMPORT LIMIT 7000';
    $stmt = $db->query($sql);

    $rowCNT = 2;
    while($rows = $stmt->fetch()){
        $phpExcel->getActiveSheet()->fromArray($rows, null, 'A'.$rowCNT);
        $rowCNT++;
    }  

    unset($rowCNT);
    unset($select);
    unset($db);

    // MEMORY USAGE = 4 MB!

    $phpExcelWrite = new PHPExcel_Writer_Excel2007($phpExcel);
    $phpExcelWrite->setUseDiskCaching(true);
    $phpExcelWrite->save(str_replace('.php', '.xls', __FILE__));

PHP Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 13878925 bytes) in /Applications/MAMP/htdocs/phpexcel/library/PHPExcel/Shared/XMLWriter.php on line 102

  • 写回答

4条回答 默认 最新

  • duanhong1985 2012-09-03 17:11
    关注

    I have tried to use PhpExcel but found out the same problems as you. I've tried all the proposals to reduce memory consumption (the link Klinky posted in his answer) but got only 25-30% improvement. I even thought about creating CSV output instead of excel.

    Now I am using PEAR's Spreadsheet_Excel_Writer and it works fine for me with roughly the same amount of data (11k rows 62 columns), though it takes quite some time to generate the file.

    It is not as powerful as PhpExcel, but as I see in your example you are not using it to style cells and write functions, right?

    Example:

    $workbook = new Spreadsheet_Excel_Writer();
    $workbook->send($filename . '.xls');
    $workbook->setVersion(8); // excel 8
    // Create worksheet
    $worksheet =& $workbook->addWorksheet('submissions');
    $worksheet->setInputEncoding('UTF-8');
    // Write some data on Sheet 1
    $row = 0;
    $worksheet->write($row, 0, 'Poster #ID');
    $worksheet->write($row, 1, 'Surname');
    $worksheet->write($row, 2, 'Firstname');
    $worksheet->write($row, 3, 'Country');
    $worksheet->write($row, 4, 'E-mail');
    $worksheet->write($row, 5, 'All authors');
    $worksheet->write($row, 6, 'Institutions');
    $worksheet->write($row, 7, 'Abstract topic');
    $worksheet->write($row, 8, 'Abstract title');
    foreach ($records as $rec_id => $rec_data) {
        $row++;
        $worksheet->write($row, 0, 'P-' . $rec_data['id']);
        $worksheet->write($row, 1, $rec_data['submitter_surname']);
        $worksheet->write($row, 2, $rec_data['submitter_firstname']);
        $worksheet->write($row, 3, $rec_data['submitter_country']);
        $worksheet->write($row, 4, $rec_data['submitter_email']);
        $worksheet->write($row, 5, $rec_data['authors']);
        $worksheet->write($row, 6, $rec_data['institutions']);
        $worksheet->write($row, 7, $rec_data['abstract_topic']);
        $worksheet->write($row, 8, $rec_data['abstract_title']);
    }
    $workbook->close(); // output
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图