$name = 't_'.date('YmdHi');
     require_once $_SERVER['DOCUMENT_ROOT'].'/../'.'vendor/phpoffice/phpexcel/Classes/PHPExcel.php';
     $excel = new \PHPExcel(); //引用phpexcel
     iconv('UTF-8', 'gb2312', $name); //针对中文名转码
     $header = ['昵称', '关注数量','公众号']; //表头,名称可自定义
     $excel->setActiveSheetIndex(0);
     $excel->getActiveSheet()->setTitle($name); //设置表名
     $excel->getActiveSheet()->getDefaultRowDimension()->setRowHeight(18);
     $excel->getActiveSheet()->getColumnDimension('A')->setWidth(20);
     $excel->getActiveSheet()->getColumnDimension('B')->setWidth(20);
     $excel->getActiveSheet()->getColumnDimension('C')->setWidth(100);
     $letter = ['A', 'B', 'C', 'D', 'E', 'F', 'G'];//列坐标

       ///设置单元格换行
     $excel->getActiveSheet()->getStyle("C")->getAlignment()->setWrapText(true);
   //生成表头
     for ($i = 0; $i < count($header); $i++) {
        //设置表头值
        $excel->getActiveSheet()->setCellValue("$letter[$i]1", $header[$i]);
       
       
        //设置表头字体样式
        $excel->getActiveSheet()->getStyle("$letter[$i]1")->getFont()->setName('宋体');
        //设置表头字体大小
        $excel->getActiveSheet()->getStyle("$letter[$i]1")->getFont()->setSize(14);
        //设置表头字体是否加粗
        $excel->getActiveSheet()->getStyle("$letter[$i]1")->getFont()->setBold(true);
        //设置表头文字水平居中
        $excel->getActiveSheet()->getStyle("$letter[$i]1")->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        //设置文字上下居中
        $excel->getActiveSheet()->getStyle($letter[$i])->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
        //设置单元格背景色
        $excel->getActiveSheet()->getStyle("$letter[$i]1")->getFill()->getStartColor()->setARGB('FFFFFFFF');
        $excel->getActiveSheet()->getStyle("$letter[$i]1")->getFill()->setFillType(\PHPExcel_Style_Fill::FILL_SOLID);
        $excel->getActiveSheet()->getStyle("$letter[$i]1")->getFill()->getStartColor()->setARGB('FF6DBA43');
        //设置字体颜色
        $excel->getActiveSheet()->getStyle("$letter[$i]1")->getFont()->getColor()->setARGB('FFFFFFFF');
     }
     
     //写入数据
     foreach ($data as $k => $v) {
        //从第二行开始写入数据(第一行为表头)
        $excel->getActiveSheet()->setCellValue('A' . ($k + 2), $v['nickname']);
        $excel->getActiveSheet()->setCellValue('B' . ($k + 2), $v['c']);
        $excel->getActiveSheet()->setCellValue('C' . ($k + 2), $v['platform']);
     }
     
     //设置单元格边框
     $excel->getActiveSheet()->getStyle("A1:E" . (count($data) + 1))->getBorders()->getAllBorders()->setBorderStyle(\PHPExcel_Style_Border::BORDER_THIN);

//清理缓冲区,避免中文乱码
     ob_end_clean();
     header('Content-Type: application/vnd.ms-excel');
     header('Content-Disposition: attachment;filename="' . $name . '.xls"');
     header('Cache-Control: max-age=0');
     //导出数据
     $res_excel = \PHPExcel_IOFactory::createWriter($excel, 'Excel2007');
     $res_excel->save('php://output');


今日吐槽

让烦恼与风去远行;让痛苦陪云去流浪。哼一曲好歌,也许心情,就会轻松许多

Contact ME

TAG Link