PHPEXCEL 导出excel

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' => '没有数据'] );
        }
相关推荐
CYRUS STUDIO1 小时前
Unidbg Trace 反 OLLVM 控制流平坦化(fla)
android·汇编·算法·网络安全·逆向·ollvm
扫地的小何尚2 小时前
NVIDIA工业设施数字孪生中的机器人模拟
android·java·c++·链表·语言模型·机器人·gpu
赵孝正2 小时前
自动用 Excel 转 .CSV 为 .xlsx 的原理
excel
互联网上的猪2 小时前
Excel时间类型函数(包括today、date、eomonth、year、month、day、weekday、weeknum、datedif)
笔记·学习·excel
顾林海4 小时前
深度解析ArrayList工作原理
android·java·面试
安静的海岸_AI4 小时前
Android端WIFI/流量共存技术方案
android
_一条咸鱼_4 小时前
Android Compose 框架进度指示器深入剖析(五十二)
android
张风捷特烈4 小时前
Flutter 伪 3D 绘制#02 | 地平面与透视
android·flutter
每次的天空4 小时前
Kotlin 作用域函数:apply、let、run、with、also
android·开发语言·kotlin
重生之我在写代码4 小时前
如何进行apk反编译
android·程序员·编译器