$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( ) ; $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 ) ; foreach ( $data as $key => $value ) { $excelRow = ( $key + 3 ) ; $objPHPExcel -> getActiveSheet ( ) -> getCell ( 'D' . $excelRow ) -> setValue ( $value [ 'tod_date' ] ) ; $richTextObjNation = new \ PHPExcel_RichText( ) ; $count_nation = count ( $value [ 'nations' ] ) ; foreach ( $value [ 'nations' ] as $k_nation => $v_nation ) { 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 ) ; 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 , "Excel2007" ) ; $objWriter -> save ( 'php://output' ) ; exit ; } else { return response ( ) -> json ( [ 'code' => 1 , 'msg' => '没有数据' ] ) ; }