首页 > 解决方案 > PHPExcel 允许的内存大小耗尽

问题描述

我正在创建带有图像的大 excel 文件,现在我遇到了这个问题。没有图像,一切都很好。除此之外,我无法增加内存限制。

我应该怎么办?我找不到真正的代码示例,我听说过 reader,但我不明白如何正确使用它。

foreach ($array as $key => $value) {
    $sheet->setCellValue("A".$i, $value['articul']);
    $sheet->setCellValue("B".$i, $value['text']);

    //uploading image to my server

    $type_img = explode( ".",$value['image']);
    $type_img =  ".".end($type_img);
    $pht_lnk = $value['image'];
    $photoname = explode("/", $value['image']);
    $photoname = end($photoname);
    $dir_upl = 'img/'.$photoname;
    if (!fileExists($dir_upl)) {
        if(file_put_contents($dir_upl, file_get_contents($pht_lnk))) { 
            $imagePath = $dir_upl;
        }
        else{
            $imagePath = NULL;
        }
    }
    else{
        $imagePath = $dir_upl;
    }

    //here i'm trying to add image

    if (file_exists($imagePath)) {
        $imgg = new PHPExcel_Worksheet_Drawing();
        $imgg->setPath($imagePath);
        $imgg->setCoordinates("C".$i);             
        $imgg->setOffsetX(0);
        $imgg->setOffsetY(0);    
        $sheet->getRowDimension(2)->setRowHeight(190);
        $imgg->setWorksheet($sheet);
    } 
    $sheet->setCellValue("D".$i, $value['price']);
    $i++;
}

标签: phpexcelmemoryout-of-memory

解决方案


如果您无法增加内存限制,请尝试其他库

Eli Dickinson的 PHP 导出数据

简单的excel

PHP_XLSXWriter


推荐阅读