简体   繁体   English

即使使用单元缓存,PHPExcel内存仍然耗尽 - 其他解决方案

[英]PHPExcel memory still exhausted even with cell caching - other solutions

<b>Fatal error</b>:  Allowed memory size of 134217728 bytes exhausted (tried to allocate 78 bytes) in <b>/var/www/leanne/api/classes/PHPExcel/CachedObjectStorage/PHPTemp.php</b> on line <b>66</b><br />

Hi, 嗨,

I asked this question a few days ago and was advised to change my code and to use cell caching. 我几天前问过这个问题 ,并建议更改我的代码并使用单元格缓存。 While I have changed my code and attempted to use cell caching, I am still getting a memory error. 虽然我已经更改了我的代码并尝试使用单元格缓存,但我仍然遇到内存错误。 I am desperate to find a solution to this. 我迫切希望找到解决方案。

Can anyone advise on which caching method would be best for writing excel files ranging between 1 to 100,000 rows of data? 任何人都可以建议哪种缓存方法最适合编写1到100,000行数据的excel文件? If cell caching doesn't work, I may need to use another solution that allows me to append to an xls file in the same way I do with the CSV version. 如果单元格缓存不起作用,我可能需要使用另一种解决方案,允许我以与CSV版本相同的方式附加到xls文件。

An example of my current code is below: 我当前代码的一个示例如下:

if ($count_prods > 0) {

    $format = strtolower($export_data['output']);
    $temp_file_location = '../temp/exports/products/';
    $filename = 'data_' + $shop->ID . '_' . $export_id . '_test';
    $separator = ',';
    $endrow = "\n";

    $fh = fopen($temp_file_location . $filename . '.csv', 'a');

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

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

    $objPHPExcel = new PHPExcel();

    $rowID = 2;
    $counter = 1;
    for ($i = 0; $i < $count_prods; $i += $batchlimit) {
        $csv = '';
        $limit = $batchlimit * $counter;
        $start = $i + 1;
        $productData = $productExport->getProductData($start, $limit);

        if ($counter == 1) {
            //get column names
            if ($format == 'csv') {
                $column_titles = implode(',', $productExport->product_fields);
                $column_no = count($column_titles);
                $csv = $column_titles . $endrow;
            } else {
                $objPHPExcel->getActiveSheet()->fromArray($productExport->product_fields, NULL, 'A1');
            }
        }

        //loop through data export array
        foreach ($productData as $product_id => $product_details) {
            $columnID = 'A';
            foreach ($product_details as $key => $value) {
                if ($format == 'csv') {
                    $csv .= '"' . str_replace('"', '\'', $product_details[$key]) . '"' . $separator;
                } else {
                    $objPHPExcel->getActiveSheet()->setCellValue($columnID . $rowID, $product_details[$key]);
                }
                $columnID++;
            }
            if ($format == 'csv') {
                $csv = rtrim($csv, $separator);
                $csv .= $endrow;
            }
            $rowID++;
        }
        if ($format == 'csv') {
            fwrite($fh, $csv);
            $csv = '';
        }

        $counter++;
    }
    if ($format == 'csv') {
        fclose($fh);
    }

    //if  XLS file 
    if ($format == 'xls') {
        //$objPHPExcel = $objReader->load($temp_file_location . $filename . '.csv');
        // $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        //$objWriter->save($temp_file_location . $filename . '.xls');
        $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
        $objWriter->save($temp_file_location . $filename . '.xlsx');
    }

you could increase the memory and time allocated to the script using: 您可以使用以下命令增加分配给脚本的内存和时间:

ini_set('memory_limit', '2048M'); ini_set('memory_limit','2048M'); set_time_limit('1200'); 参数或者set_time_limit( '1200');

PHPExcel is an excelent library, and I just love it. PHPExcel是一个优秀的图书馆,我只是喜欢它。 However, to support all those Excel features, it eats a lot of memory. 但是,为了支持所有这些Excel功能,它会占用大量内存。

You are doing simple reading and writing, without any advanced Excel stuff. 你正在做简单的阅读和写作,没有任何高级的Excel东西。

Give Spout a try. 试试Spout吧。 https://github.com/box/spout https://github.com/box/spout

From the main description: Spout is a PHP library to read and write spreadsheet files (CSV, XLSX and ODS), in a fast and scalable way. 从主要描述: Spout是一个PHP库,以快速和可扩展的方式读写电子表格文件(CSV,XLSX和ODS)。 Contrary to other file readers or writers, it is capable of processing very large files while keeping the memory usage really low (less than 3MB). 与其他文件读取器或编写器相反,它能够处理非常大的文件,同时保持内存使用率非常低(小于3MB)。

For exporting or importing from excel it is at least 10x faster and eats very little memory, as it is streaming data to and from the file 对于从excel导出或导入,它至少快10倍并且占用非常少的内存,因为它是与文件之间的数据流

I have successfully replaced PHPExcel for some very big Excel files for which I only needed exporting data. 我已成功将PHPExcel替换为一些非常大的Excel文件,我只需要导出数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM