jerome
jerome

Reputation: 715

Laravel PHP Excel Export memory exausted

So i am having this error on exporting my data on excel.

Fatal error: Allowed memory size of 268435456 bytes exhausted (tried to allocate 72 bytes) in /home1/greatsup/public_html/system/application/vendor/phpoffice/phpexcel/Classes/PHPExcel/Cell.php on line 889

Which is surprising because the table that i have been exporting ONLY contains 10 rows. So i tried printing it first to see if any data is fetch but still got the same error.

PS: i tried using the excel export on larger sets of data. But in this controller i am having a hard time figuring what is the problem.

Here is my code

 public function excel_export(Request $request) {

    $post = $request->all();
    $dateFrom = $post['updated_at_from'];
    $dateTo = $post['updated_at_to'];

    error_reporting(E_ALL);
    ini_set('display_errors', TRUE);
    ini_set('display_startup_errors', TRUE);
    date_default_timezone_set('Asia/Manila');
    if (PHP_SAPI == 'cli')
        die('This example should only be run from a Web Browser');
    $objPHPExcel = new PHPExcel();
    $objPHPExcel = PHPExcel_IOFactory::load("templates/unit_specifications_format.xlsx");
    $objPHPExcel->getProperties()->setCreator(Config::$APPNAME)
                                 ->setLastModifiedBy(Config::$APPNAME)
                                 ->setTitle(null)
                                 ->setSubject(null)
                                 ->setDescription(null)
                                 ->setKeywords(null)
                                 ->setCategory(null);
    $fontSettings = array(
    'font'  => array(
        'bold'  => true,
        'color' => array('rgb' => 'FFFFFF'),
        'size'  => 12,
        'name'  => 'calibri'
    ));
    $headerSettings = array('fill' => array(
    'type'  => PHPExcel_Style_Fill::FILL_SOLID,
    'color' => array('rgb' => \App\Resources\Config::$PRIMARYCOLOR)
    ));
    $borderSettings = $BStyle = array(
      'borders' => array(
        'allborders' => array(
          'style' => PHPExcel_Style_Border::BORDER_THIN
        )
      )
    );

    $query = DB::table('unit_specifications')->get();
    print_r($query);
    exit;


    $objPHPExcel->setActiveSheetIndex(0);
    $sheet = $objPHPExcel->getActiveSheet();

    $start = 5;
    $row = $start;
    $column = 0;
    $no = 1;




    $allCombination = PHPExcel_Cell::stringFromColumnIndex(0).$start.':'.PHPExcel_Cell::stringFromColumnIndex($column-1).($row-1);
    $sheet->getStyle($allCombination)->applyFromArray($borderSettings);
    header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
    header('Content-Disposition: attachment;filename="'.$this->excel_filename.'"');
    header('Cache-Control: max-age=0');
    header('Cache-Control: max-age=1');
    header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
    header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
    header ('Cache-Control: cache, must-revalidate');
    header ('Pragma: public');
    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
    $objWriter->save('php://output');
    exit;
}

Any thoughts what i have missed ?

Upvotes: 1

Views: 2370

Answers (1)

Ahmed Shams
Ahmed Shams

Reputation: 358

If you aren't sure what your PHP memory limit is set to, it's helpfully included in the error message. The size is reported in bytes, though, so we've done some conversions for you:

PHP: Fatal Error: Allowed Memory Size of 8388608 Bytes Exhausted - 8 MB
PHP: Fatal Error: Allowed Memory Size of 16777216 Bytes Exhausted - 16 MB
PHP: Fatal Error: Allowed Memory Size of 33554432 Bytes Exhausted - 32 MB
PHP: Fatal Error: Allowed Memory Size of 67108864 Bytes Exhausted - 64 MB
PHP: Fatal Error: Allowed Memory Size of 134217728 Bytes Exhausted - 128 MB
PHP: Fatal Error: Allowed Memory Size of 268435456 Bytes Exhausted - 256 MB
PHP: Fatal Error: Allowed Memory Size of 536870912 Bytes Exhausted - 512 MB
PHP: Fatal Error: Allowed Memory Size of 1073741824 Bytes Exhausted - 1 GB

to solve this error go to your php.ini

ini_set('memory_limit', '1024M'); // or you could use 1G

it depends on what Apache you use most of them use word

memory_limit

so search about this word and modify it and check your data hope this solve your problem

Upvotes: 1

Related Questions