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' => '没有数据'] );
        }
相关推荐
姑苏风2 小时前
《Kotlin实战》-附录
android·开发语言·kotlin
数据猎手小k5 小时前
AndroidLab:一个系统化的Android代理框架,包含操作环境和可复现的基准测试,支持大型语言模型和多模态模型。
android·人工智能·机器学习·语言模型
你的小106 小时前
JavaWeb项目-----博客系统
android
Python大数据分析@6 小时前
python操作CSV和excel,如何来做?
开发语言·python·excel
风和先行6 小时前
adb 命令查看设备存储占用情况
android·adb
John.liu_Test7 小时前
js下载excel示例demo
前端·javascript·excel
AaVictory.7 小时前
Android 开发 Java中 list实现 按照时间格式 yyyy-MM-dd HH:mm 顺序
android·java·list
似霰8 小时前
安卓智能指针sp、wp、RefBase浅析
android·c++·binder
大风起兮云飞扬丶8 小时前
Android——网络请求
android
干一行,爱一行8 小时前
android camera data -> surface 显示
android