lundi 5 février 2018

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 ?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire