php
复制代码
$styleArray = [
'alignment' => [
'horizontal' => Alignment::HORIZONTAL_CENTER,
'vertical' => Alignment::VERTICAL_CENTER
],
];
$border_style = [
'borders' => [
'allborders' => [
'style' => \PHPExcel_Style_Border::BORDER_THIN ,//细边框
]
]
];
$begin_date = $request->begin_date;
$end_date = $request->end_date;
$search = $request->search_word;
$arr_com = [];
if (isset($request->com_val)){
$arr_com = explode(',',$request->com_val);
}
$arr_fact = [];
if (isset($request->fact_val)){
$arr_fact = explode(',',$request->fact_val);
}
$res = $this->getData($begin_date,$end_date,$search,$arr_com,$arr_fact,0,0);
$data = $res['data'];
if(count($data)>0){
ob_end_clean();
ob_start();
// 最后导出
$year = date('Y',strtotime($begin_date));
$week = intval(date('W',strtotime($begin_date)));
$filename = $year.'年'.$week.'周出货表';
$objPHPExcel = new \PHPExcel();
$objPHPExcel->getActiveSheet()->setTitle($filename);
$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(10);
$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(30);
//行高
$objPHPExcel->getActiveSheet()->getRowDimension('1')->setRowHeight(40);
$objPHPExcel->getActiveSheet()->getRowDimension('2')->setRowHeight(20);
//第一行
$objPHPExcel->getActiveSheet()->getCell("A1")->setValue($filename);
$objPHPExcel->getActiveSheet()->mergeCells('A1:I1');
//第二行
$objPHPExcel->getActiveSheet()->getCell("A2")->setValue('出货日期');
$richTextObj = new \PHPExcel_RichText(); //每一行都需要重新New一个richText
$setStyleObj = $richTextObj->createTextRun("国别(海运/");
$setStyleObj->getFont()->setColor(new \PHPExcel_Style_Color("FF000000")); //设置颜色
$setStyleObj = $richTextObj->createTextRun("空运");
$setStyleObj->getFont()->setColor(new \PHPExcel_Style_Color("FFFF0000")); //设置颜色
$setStyleObj = $richTextObj->createTextRun("/");
$setStyleObj->getFont()->setColor(new \PHPExcel_Style_Color("FF000000")); //设置颜色
$setStyleObj = $richTextObj->createTextRun("陆运");
$setStyleObj->getFont()->setColor(new \PHPExcel_Style_Color("FF00FF00")); //设置颜色
$setStyleObj = $richTextObj->createTextRun("/");
$setStyleObj->getFont()->setColor(new \PHPExcel_Style_Color("FF000000")); //设置颜色
$setStyleObj = $richTextObj->createTextRun("铁路");
$setStyleObj->getFont()->setColor(new \PHPExcel_Style_Color("FF0000FF")); //设置颜色
$setStyleObj = $richTextObj->createTextRun("/");
$setStyleObj->getFont()->setColor(new \PHPExcel_Style_Color("FF000000")); //设置颜色
$setStyleObj = $richTextObj->createTextRun("海空");
$setStyleObj->getFont()->setColor(new \PHPExcel_Style_Color("FFFF00FF")); //设置颜色
$setStyleObj = $richTextObj->createTextRun(")");
$setStyleObj->getFont()->setColor(new \PHPExcel_Style_Color("FF000000")); //设置颜色
$objPHPExcel->getActiveSheet()->getCell("E2")->setValue($richTextObj);
//数据写进Excel,并对关键字高亮处理
foreach ($data as $key =>$value){
$excelRow = ($key + 3); //excel第几行
$objPHPExcel->getActiveSheet()->getCell('D'.$excelRow)->setValue($value['tod_date']);
//国别 运输方式处理
$richTextObjNation = new \PHPExcel_RichText(); //每一行都需要重新New一个richText 拼接 运输方式 突击拔高
$count_nation = count($value['nations']);
foreach ($value['nations'] as $k_nation => $v_nation){
//国别 运输方式: 0海运,1空运,2陆运,3铁路,4海空
if($v_nation['flag'] == 0){
$setStyleObj = $richTextObjNation->createTextRun($v_nation['name']);
$setStyleObj->getFont()->setColor(new \PHPExcel_Style_Color("FF000000")); //设置颜色
if($k_nation != $count_nation - 1){
$setStyleObj = $richTextObjNation->createTextRun("、");
$setStyleObj->getFont()->setColor(new \PHPExcel_Style_Color("FF000000")); //设置颜色
}
}
if($v_nation['flag'] == 1){
$setStyleObj = $richTextObjNation->createTextRun($v_nation['name']);
$setStyleObj->getFont()->setColor(new \PHPExcel_Style_Color("FFFF0000")); //设置颜色
if($k_nation != $count_nation - 1){
$setStyleObj = $richTextObjNation->createTextRun("、");
$setStyleObj->getFont()->setColor(new \PHPExcel_Style_Color("FF000000")); //设置颜色
}
}
if($v_nation['flag'] == 2){
$setStyleObj = $richTextObjNation->createTextRun($v_nation['name']);
$setStyleObj->getFont()->setColor(new \PHPExcel_Style_Color("FF00FF00")); //设置颜色
if($k_nation != $count_nation - 1){
$setStyleObj = $richTextObjNation->createTextRun("、");
$setStyleObj->getFont()->setColor(new \PHPExcel_Style_Color("FF000000")); //设置颜色
}
}
if($v_nation['flag'] == 3){
$setStyleObj = $richTextObjNation->createTextRun($v_nation['name']);
$setStyleObj->getFont()->setColor(new \PHPExcel_Style_Color("FF0000FF")); //设置颜色
if($k_nation != $count_nation - 1){
$setStyleObj = $richTextObjNation->createTextRun("、");
$setStyleObj->getFont()->setColor(new \PHPExcel_Style_Color("FF000000")); //设置颜色
}
}
if($v_nation['flag'] == 4){
$setStyleObj = $richTextObjNation->createTextRun($v_nation['name']);
$setStyleObj->getFont()->setColor(new \PHPExcel_Style_Color("FFFF00FF")); //设置颜色
if($k_nation != $count_nation - 1){
$setStyleObj = $richTextObjNation->createTextRun("、");
$setStyleObj->getFont()->setColor(new \PHPExcel_Style_Color("FF000000")); //设置颜色
}
}
}
$objPHPExcel->getActiveSheet()->getCell('E'.$excelRow)->setValue($richTextObjNation);
$objPHPExcel->getActiveSheet()->getRowDimension($excelRow)->setRowHeight(20);
$objPHPExcel->getActiveSheet()->getStyle('E'.$excelRow)->getAlignment()->setWrapText(true);
}
//垂直居中
$objPHPExcel->getActiveSheet()->getStyle('A1:I2')->applyFromArray($styleArray);
//细边框
$objPHPExcel->getActiveSheet()->getStyle('A2:I'.$excelRow)->applyFromArray($border_style);
//字体
$objPHPExcel->getActiveSheet()->getStyle('A1:I1')->getFont()->setBold(true)->setName('黑体')->setSize(16);
$objPHPExcel->getActiveSheet()->getStyle('A2:I2')->getFont()->setBold(true)->setName('宋体')->setSize(12);
// 导出excel
ob_end_clean();
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="' . $filename . '.xls');
header('Cache-Control: max-age=0');
header('Content-Type: text/html; charset=utf-8');
//$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, "Excel2007");
$objWriter->save('php://output');
exit;
}
else{
return response()->json(['code' => 1,'msg' => '没有数据'] );
}