【问题标题】:Cannot get cell caching to work in PHPExcel无法让单元格缓存在 PHPExcel 中工作
【发布时间】:2012-12-01 15:49:21
【问题描述】:

我已经尝试了 PHPExcel 1.7.8 手册中用于单元缓存的所有方法,但没有一个有效。每次尝试加载大约 30 MB 的 Excel 文件时,我仍然会耗尽内存。

这是我的代码:

    ini_set('memory_limit', '256M'); // Up from default 32MB

    require MODULES_DIR . '/PHPExcel.php';

    $cacheMethod = PHPExcel_CachedObjectStorageFactory::cache_to_discISAM;
    $cacheSettings = array('dir' => '/usr/local/tmp');
    PHPExcel_Settings::setCacheStorageMethod($cacheMethod, $cacheSettings);

    $inputFileType = PHPExcel_IOFactory::identify($dir . $source_file);

    $objReader = PHPExcel_IOFactory::createReader($inputFileType);

    $objReader->setReadDataOnly(true);

    $objPHPExcel = $objReader->load($dir . $source_file);

    $total_sheets = $objPHPExcel->getSheetCount();
    $allSheetName = $objPHPExcel->getSheetNames();

    $objWorksheet = $objPHPExcel->setActiveSheetIndex(0);

    $highestRow = $objWorksheet->getHighestRow();
    $highestColumn = $objWorksheet->getHighestColumn();

    $headingsArray = $objWorksheet->rangeToArray('A1:'.$highestColumn.'1',null, true, true, true);
    $headingsArray = $headingsArray[1];

    $r = -1;
    $namedDataArray = array();

    for ($row = 2; $row <= $highestRow; ++$row) {
        $dataRow = $objWorksheet->rangeToArray('A'.$row.':'.$highestColumn.$row,null, true, true, true);

        if ((isset($dataRow[$row]['A'])) && ($dataRow[$row]['A'] > '')) {
            ++$r;

            foreach($headingsArray as $columnKey => $columnHeading) {
                $namedDataArray[$r][$columnHeading] = $dataRow[$row][$columnKey];
            }
        }
    }

我已经尝试了每一种单元缓存机制,一直到“cache_to_sqlite3”,但它们都不起作用。

谁能告诉我我在这里做错了什么?

编辑:显然我跳过了“cache_to_sqlite”选项。我试过了,它开始缓存。但是,它在缓存 30 MB 文件和 256 MB 可用内存的过程中仍然会耗尽内存。这是错误:

 Fatal error: Uncaught exception 'Exception' with message 'out of memory' in modules/PHPExcel/CachedObjectStorage/SQLite.php:64 Stack trace: #0 modules/PHPExcel/CachedObjectStorage/SQLite.php(81): PHPExcel_CachedObjectStorage_SQLite->_storeData() #1 modules/PHPExcel/Worksheet.php(1123): PHPExcel_CachedObjectStorage_SQLite->addCacheData('X2498702', Object(PHPExcel_Cell)) #2 modules/PHPExcel/Reader/Excel5.php(3549): PHPExcel_Worksheet->getCell('X2498702') #3 modules/PHPExcel/Reader/Excel5.php(894): PHPExcel_Reader_Excel5->_readLabelSst() #4 modules/updater.inc.php(1478): PHPExcel_Reader_Excel5->load('modules/p...') #5 modules/updater.php(204): Updater->process_xls('prods.xls') #6 {main} thrown in modules/PHPExcel/CachedObjectStorage/SQLite.php on line 64

【问题讨论】:

    标签: php phpexcel


    【解决方案1】:

    单元缓存不会消除内存使用:它会减少它。显然,您仍然没有足够的 PHP 内存来处理这么大的工作簿。

    一个可能的问题是对单元格“X2498702”的引用... Excel BIFF 文件(由 Excel5 阅读器加载的类型)的上限为 65536 行,因此如果阅读器是试图加载第 2498702 行中的单元格。问题不在于单元格缓存,而是 Excel5 阅读器发现数据被误解,或者该 Excel 文件中不应该存在。

    您能否将此特定文件的副本(如果允许保密)上传到 PHPExcel 网站,以便我们查看并尝试确定到底出了什么问题。

    【讨论】:

    • 嗨,马克!只要不公开显示,我就可以上传文件。我在 phpexcel.net 网站上的哪个位置上传?
    • 我已经给你发了一封电子邮件。如果您不明白,请告诉我,非常感谢! :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-24
    • 1970-01-01
    • 1970-01-01
    • 2011-01-22
    • 1970-01-01
    • 2012-05-25
    • 1970-01-01
    相关资源
    最近更新 更多