如何在Laravel

时间:2016-05-31 07:58:13

标签: php excel laravel-4

我有一个文件,可以在Laravel 4.2中下载excel

视图页面如下所示

    ini_set('memory_limit', '-1');
    require(app_path().'/libs/excel/Classes/PHPExcel.php');
    $objPHPExcel = new PHPExcel();

    // Set document properties
    $objPHPExcel->getProperties()->setCreator("ideconnect.com")
                                 ->setLastModifiedBy("ideconnect.com")
                                 ->setTitle("Office 2007 XLSX Test Document")
                                 ->setSubject("Office 2007 XLSX Test Document")
                                 ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
                                 ->setKeywords("office 2007 openxml php")
                                 ->setCategory("Test result file");

    $arr = array();

            $users1 = User::where('event','=',$exceleve)->orderby('name')->with(array('answer'=>function($q) use($qid1){

                $q->where('question_id','=','3104')->orWhere('question_id','=','3105');

            }))->get();

    $user_ids=array();
    foreach($users1 as $us){
        if($us->answer[0]->answer_text!='' && ($us->role!='admin_dd' || $us->role!='sales_dd')){
            $user_ids[] = $us->id;
        }
    }

        $userswithanswers = User::whereIn('id', $user_ids)
                        ->with('answer')
                        ->get();


    foreach($userswithanswers as $usa){

        $tempansarr = array();
        $tempcatansarr = array();

        foreach($usa->answer as $ansobj){
            $tempansarr[$ansobj->question_id] = $ansobj->answer_text;
        }

        foreach($usa->categoryvendoranswer as $catansobj){
            $tempcatansarr[$catansobj->categoryvendor_id] = $catansobj->answer;
        }

        $useransarr[$usa->id]['login'] = $usa->email;
        $useransarr[$usa->id]['id'] = $usa->id;
        $useransarr[$usa->id]['answers'] = $tempansarr;
        $useransarr[$usa->id]['catanswers'] = $tempcatansarr;
     //dd($tempcatansarr);
    }

    $questions = Question::orderBy('page')->where('event' ,'=',$exceleve)->where('page' ,'!=',0)->orderBy('order')->get();
    //$categoryvendors = Categoryvendor::all();


    // HEADER ROW FOR FOR EXCEL
    $header1 = array();$header1[]='';
    $header2 = array();$header2[]='Login';
    $preprojectnumber = '';

    foreach($questions as $header){
        /*echo $header->class_name."<br>";
        echo $header->question_group."<br>";*/
        $header3[] = "Page_".$header->page;
        if($header->class_name !='projects checkbox group'){
            //echo "1";
            $header1[] = $header->question_group;
            $header2[] = $header->question_text;

        } else {
            if($header->question_group!=$preprojectnumber){
                $header1[] = $header['question_group'];
                $header2[] = 'Stage of Buying Cycle';

            }
            $preprojectnumber = $header->question_group;

        }

    }

$excelarr = array();
//dd($useransarr);


foreach($useransarr as $usarr){

        $b=array();
        $b[]=$usarr['login'];
        $preprojectnumber = '';
        $tempvar = '';
        $counter=0;

// dd($usarr['answers']);

    foreach($questions as $question){
            if($question->class_name!='projects checkbox group'){ 
                $b[] = $usarr['answers'][$question->id];
            } 
            else 
            {
                if($question->question_group!=$preprojectnumber){
                    $counter=1;
                    $tempvar = $usarr['answers'][$question->id];
                    if($usarr['answers'][$question->id]!=''){$tempvar = $tempvar.', ';}
                } else {
                    $tempvar = $tempvar.$usarr['answers'][$question->id];
                    if($usarr['answers'][$question->id]!=''){$tempvar = $tempvar.', ';}
                }
                    $preprojectnumber = $question->question_group;
                    $counter++;
                    if($counter==5){
                            $b[]=$tempvar;
                    }
            }
    }
    $excelarr[] = $b;
}

array_unshift($excelarr, $header1);
array_unshift($excelarr, $header2);

$objPHPExcel->setActiveSheetIndex(0)
            ->fromArray($excelarr, '', 'A1');

$objPHPExcel->getActiveSheet()->setTitle($exceleve);

$styleArray = array(
      'font' => array(
        'name' => 'Calibri',
        'size' => '10',
        'color' => array(
            'rgb' => 'FFFFFF'
        ),
        'bold' => true,
      ),
      'fill' => array(
        'type' => PHPExcel_Style_Fill::FILL_SOLID,
        'startcolor' => array(
          'rgb' => '000000',
        ),
      ),
    );


$styleArray2 = array(
      'font' => array(
        'name' => 'Calibri',
        'size' => '10',
        'color' => array(
            'rgb' => '000000'
        ),
        'bold' => true,
      ),

      'fill' => array(
        'type' => PHPExcel_Style_Fill::FILL_SOLID,
        'startcolor' => array(
          'rgb' => 'cccccc',
        ),
      ),
    );

$styleArray3 = array(
      'font' => array(
        'name' => 'Calibri',
        'size' => '8',
        ),
    );


$objPHPExcel->setActiveSheetIndex(0)->getDefaultStyle()->applyFromArray($styleArray3);
$objPHPExcel->setActiveSheetIndex(0)->getStyle('A2:LA2')->applyFromArray($styleArray2);
$objPHPExcel->setActiveSheetIndex(0)->getStyle('A1:LA1')->applyFromArray($styleArray);

$objPHPExcel->setActiveSheetIndex(0);

header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="event-data.xlsx"');
header('Cache-Control: max-age=0');


$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');


 // dd('aaa');
//ob_end_clean();
$objWriter->save('php://output');

exit;

如果行数较少,则此代码运行良好,但如果数据增加则抛出

Memory Limit reach

我只是想知道在Queue中放置这个代码会解决问题吗?

如果是,那么我应该如何点缀

谢谢

1 个答案:

答案 0 :(得分:0)

您应该尝试实施Cell Caching,如下所述:https://github.com/PHPOffice/PHPExcel/blob/develop/Documentation/markdown/Overview/04-Configuration-Settings.md#cell-caching

不是试图将所有数据保存在内存中,而是将其中一些数据缓存在磁盘或数据库中。这减少了PHPExcel所需的内存量。 缺点是处理现在需要更长时间(而且似乎是指数级的)。

作为替代方案,您可以尝试使用Spout:https://github.com/box/spout。无论大小如何,它都可以使用不超过3MB的内存来解析电子表格。您将找不到与PHPExcel一样多的功能,但您的示例中使用的所有功能似乎都受支持。