thinkphp+phpexcel导出带图片的表格

全屏阅读
  • 基本信息
  • 作者:
  • 作者已发布:925篇文章
  • 发布时间:2020年07月17日 0:58:56
  • 所属分类:PHP+MySql, THINKPHP
  • 阅读次数:2818次阅读
  • 标签:
		$param = $this->request->param();
        $ids = explode(',',$param['ids']);
        vendor("PHPExcel.PHPExcel");
        $objPHPExcel = new \PHPExcel();

        //水平居中
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('A')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('B')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('C')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('D')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('E')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);

        //设置表头
        $objPHPExcel->getActiveSheet() -> setCellValue('A1', '编号');
        $objPHPExcel->getActiveSheet() -> setCellValue('B1', '昵称');
        $objPHPExcel->getActiveSheet() -> setCellValue('C1', '手机号');
        $objPHPExcel->getActiveSheet() -> setCellValue('D1', '注册时间');
        $objPHPExcel->getActiveSheet() -> setCellValue('E1', '头像');

        // 垂直居中
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('A')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('B')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('C')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('D')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('E')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);

        //设置宽
        $subObject = $objPHPExcel->getSheet();
        $subObject->getColumnDimension('A')->setWidth(10);
        $subObject->getColumnDimension('B')->setWidth(40);
        $subObject->getColumnDimension('C')->setWidth(40);
        $subObject->getColumnDimension('D')->setWidth(40);
        $subObject->getColumnDimension('E')->setWidth(16);

        $where['id'] = array('IN',$ids);
        $result = Db::name('users')->where($where)->select();

        foreach ($result as $k=>&$v){
            $v['time'] = date("Y-m-n H:i:s",$v['time']);
            $num = $k+2;
            $objPHPExcel->getActiveSheet()->setTitle("用户信息");
            $objPHPExcel->getActiveSheet()->setCellValue('A'.$num, $v['id']);
            $objPHPExcel->getActiveSheet()->setCellValue('B'.$num, $v['nickname']);
            $objPHPExcel->getActiveSheet()->setCellValue('C'.$num, $v['mobile']);
            $objPHPExcel->getActiveSheet()->setCellValue('D'.$num, $v['time']);
            //导出图片
            if($v['header_pic']){
                $objDrawing[$num] = new \PHPExcel_Worksheet_Drawing();
                $objDrawing[$num]->setPath('.'.$v['header_pic']);
                $objDrawing[$num]->setHeight(30);//照片高度
                $objDrawing[$num]->setWidth(30); //照片宽度
                $objDrawing[$num]->setCoordinates('E'.$num);
                $objDrawing[$num]->setOffsetX(18);
                $objDrawing[$num]->setOffsetY(5);
                $objDrawing[$num]->setWorksheet($objPHPExcel->getActiveSheet());
            }
            //设置全表格高度
            $objPHPExcel->getActiveSheet()->getRowDimension($num)->setRowHeight(50);
        }
        $objPHPExcel->getActiveSheet()->setCellValue();
        $objPHPExcel->getActiveSheet()->getRowDimension()->setRowHeight(-1);
        $objPHPExcel->getActiveSheet()->getStyle()->getAlignment()->setWrapText(true);
        $objPHPExcel->createSheet();

        $fileName = date('YmdHis');
        ob_end_clean();    //清除缓存,防止乱码
        header('pragma:public');
        header('Content-type:application/vnd.ms-excel;charset=utf-8;');
        header("Content-Disposition:attachment;filename={$fileName}.xls");//attachment新窗口打印inline本窗口打印
        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');

        $objWriter->save('php://output');

        exit;

顶一下
(0)
100%
订阅 回复
踩一下
(0)
100%
» 郑重声明:本文由mpxq168发布,所有内容仅代表个人观点。版权归恒富网mpxq168共有,欢迎转载, 但未经作者同意必须保留此段声明,并给出文章连接,否则保留追究法律责任的权利! 如果本文侵犯了您的权益,请留言。

目前有 0 条留言 其中:访客:0 条, 博主:0 条

给我留言

您必须 [ 登录 ] 才能发表留言!