0

我正在尝试从数据库加载一个大型 Excel 文件。但由于我的用户群增加了,

服务器多次给出 500 错误。

它确实下载了 excel,但尝试下载几乎 10 - 20 次失败

这是我的代码

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");


// Add some data
//***************************************************************************************
$arr = array();
// $firstpass=true;
// $projectscheckboxgroupadded=false;
// $prequestiongroup = '';


    if($exceleve =="Rescom Summit Bangalore 2016")
        $qid1 = "1";
    if($exceleve =="Rescom Summit Middle East 2016")
        $qid1="7297";

        if($exceleve =="Design Mission Saudi Arabia 2016")
        $qid1 = "325";

        if($exceleve =="Design Mission Middle East 2016")
        $qid1 = "649";

        if($exceleve =="Design Mission India 2016")
        $qid1 = "973";

        if($exceleve =="Design Mission Turkey 2016")
        $qid1 = "1297";

        if($exceleve =="Design Mission Africa 2016")
        $qid1 = "1945";

        if($exceleve =="Smart Education India 2016")
        $qid1 = "2269";

        if($exceleve =="Hotelier Summit Africa 2016")
        $qid1 = "2593";

        if($exceleve =="Hotelier Summit Middle East 2016") 
        $qid1 = "3889";


        if($exceleve =="Pharmac India 2016")
        $qid1 = "2917";

        if($exceleve =="Hotelier Summit India 2016")
        $qid1 = "3241";

        if($exceleve =="Healthscape Series Africa 2016")
        $qid1 = "4537";

        if($exceleve =="Healthscape Series India 2016")
        $qid1 = "5185";

        if($exceleve =="Healthscape Series Saudi Arabia 2016")
        $qid1 = "6157";

        if($exceleve =="Design Mission Asia 2016")
        $qid1 = "6502";

        //dd($qid1);

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

            $q->where('question_id','=',$qid1)->orWhere('question_id','=',$qid1+1);

        }))->get();


//dd($users1);

$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;
    }
}



$useransarr = array();
$users = User::whereIn('id', $user_ids)->get();

 //$x = User::where('id','1648')->with('answer')->get();
 //dd($x);
//foreach ($user_ids as $value) {
    # code...
// $user_id = array();
//      array_push($user_id,1680);
//      array_push($user_id,1676);

//This FUcking page is taking lot of Mwemory so I gave ini_set('memory_limit', '-1');
    $userswithanswers = User::whereIn('id', $user_ids)
                    ->with('answer')
                    ->get();
//}//


// dd($userswithanswers);
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);
}


 //dd($userswithanswers);
// $questions = Question::all();
// $users = User::whereIn('id', $user_ids)->get();

$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;

    }

}

 //dd($questions);

//************************
$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;
                    }
            }
    }

    //dd('sdfs');
/**
    $vendorstring='';
    foreach($categoryvendors as $catven){
        if(isset($usarr['catanswers'][$catven->id])){
            if($usarr['catanswers'][$catven->id]=='yes'){
                $vendorstring = $vendorstring.$catven->vendorname.', ';
            }
        }
    }
    $b[]=$vendorstring;**/

    $excelarr[] = $b;
    // var_dump($b);
    // print_r($usarr['catanswers']);
    // echo('<br/><br/>');
}

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

if($exceleve=="Healthscape Series Saudi Arabia 2016")
$exceleve = "Healthscape Saudi";


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


if($exceleve =="Design Mission Saudi Arabia 2016")
    $exceleve ="Design Mission Saudi 2016";

if($exceleve =="Design Mission South Africa 2016")
    $exceleve ="Design Mission Africa 2016";

$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');

谢谢

4

0 回答 0