文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

PHPEXCEL 导出excel

2023-09-17 17:45

关注

在这里插入图片描述

       $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' => '没有数据'] );        }

来源地址:https://blog.csdn.net/xcbzsy/article/details/132475867

阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-后端开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯