- Development environment:
php7.2.4
nginx/1.13.11
mysql5.7.21
redis4.0.9
maatwebsite/excel2.1
laravel5.5
- Controller Code
public function exportAssetsReport(Request $request , Asset $asset)
{
dispatch(new ExportReports($asset));
}
- Job class code
<?php
namespace App\Jobs;
use App\Handlers\GenerateReport;
use App\Http\Requests\Request;
use App\Models\Asset;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Support\Facades\Log;
class ExportReports implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable;//, SerializesModels;
/**
*
* @var int
*/
public $tries = 1;
public $timeout = 60;
protected $asset;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(Asset $asset)
{
$this->asset = $asset;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$this->asset->exportAssetsReports();
}
}
- Traits
logically process and call the following export method
public function exportAssetsReports(){
set_time_limit(0);
ini_set("memory_limit","512M");
$condition = request()->condition;
$keyword = request()->keyword;
//
$userinfo = User::select("id","user_name","user_phone")->get();
$systeminfo = System::pluck("systems_name","id")->toArray();
$departinfo = Department::pluck("department_name","id")->toArray();
$all_condition = Asset::handle_condition($condition,$keyword);
$asset_info = Asset::with([
"xxx"=>function($q){
$q->withCount("xxx");
},
"xxx.xxx.xx.xx"
])->where(function ($q) use ($all_condition){
foreach($all_condition["condition_where"] as $item){
$q->where($item[0],$item[1],$item[2]);
}
})->where($all_condition["keyword_where"])->get()->toArray();
$single = $this->handleExcelData($asset_info,$userinfo,$systeminfo,$departinfo);
//
$cellData = [...];
$this->exports($cellData,collect($single));
}
protected function exports($cellData,$singles)
{
\Excel::create("xxx" . date("Y/m/d H:i:s",time()),function($excel) use ($cellData,$singles){
$excel->setTitle("xxx");
foreach($singles->chunk(5000) as $key => $value){
$excel->sheet("xxx" . ($key+1), function($sheet) use ($cellData,$value){
$sheet->appendRow($cellData);
$sheet->cells("A1:H1", function($r) {
$r->setBackground("-sharpccc0da");
});
$sheet->cells("I1:O1", function($r) {
$r->setBackground("-sharp8db4e3");
});
$sheet->cells("P1:T1", function($r) {
$r->setBackground("-sharpfac090");
});
$sheet->cells("A1:T1", function($cells) {
$cells->setAlignment("center");
$cells->setFontWeight("bold");
});
$sheet->setWidth(array("A" => 8,"B" => 25,"C" => 20,"D" => 15,"E" => 15,"F" => 15,"G" => 15,"H" => 20,"I" => 20,"J" => 50,"K" => 20,"L" => 20,"M" => 20,"N" => 40,"O" => 20,"P" => 15,"Q" => 20,"R" => 15,"S" => 15,"T" => 45));
$sheet->setHeight(1, 25);
$sheet->getStyle("A1:T1")->applyFromArray(array(
"font" => array (
"bold" => true
),
"alignment" => array(
"horizontal" => \PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
"vertical" => \PHPExcel_Style_Alignment::VERTICAL_CENTER
),
"borders" => array(
"allborders" => array( //
"style" => \PHPExcel_Style_Border::BORDER_THIN //thick
),
),
));
$sheet->setStyle(array(
"alignment" => array(
"horizontal" => \PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
"vertical" => \PHPExcel_Style_Alignment::VERTICAL_CENTER
)
));
foreach($value->chunk(1000) as $item){
$sheet->rows($item);
}
});
}
})->download("xlsx");
}
- error
command line shows the file stream, always showing timeout, using redis asynchronous export has been reporting the above errors, sync mode, can be exported normally, which god to help solve the problem, thank you!