zoukankan      html  css  js  c++  java
  • thinkphp5 导出含图片的excel表格

    最近遇到一个需求,导出后台的数据,其中有一栏是图片也要一并导出。

    首先查询mysql获取要导出的数据

    $data=$this->model->whereIn('id',$ids)->select();
    $data = collection($data)->toArray();

    实例化相关导出类

    $objPHPExcel = new PHPExcel();   
    $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
    $objActSheet = $objPHPExcel->getActiveSheet();

    设置水平居中,有几列数据,就设置几列

    $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->setActiveSheetIndex(0)->getStyle('F')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
    $objPHPExcel->setActiveSheetIndex(0)->getStyle('G')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
    $objPHPExcel->setActiveSheetIndex(0)->getStyle('H')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
    $objPHPExcel->setActiveSheetIndex(0)->getStyle('I')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
    $objPHPExcel->setActiveSheetIndex(0)->getStyle('J')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
    $objPHPExcel->setActiveSheetIndex(0)->getStyle('K')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
    $objPHPExcel->setActiveSheetIndex(0)->getStyle('L')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
    $objPHPExcel->setActiveSheetIndex(0)->getStyle('M')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);

    设置第一列的标题值

    $objActSheet->setCellValue('A1', 'ID');
    $objActSheet->setCellValue('B1', '参与者');
    $objActSheet->setCellValue('C1', '类型');
    $objActSheet->setCellValue('D1', '活动名称');
    $objActSheet->setCellValue('E1', '类别');
    $objActSheet->setCellValue('F1', '将次(称号)');
    $objActSheet->setCellValue('G1', '颁奖单位');
    $objActSheet->setCellValue('H1', '级别');
    $objActSheet->setCellValue('I1', '获奖日期');
    $objActSheet->setCellValue('J1', '获奖者');
    $objActSheet->setCellValue('K1', '证明材料');
    $objActSheet->setCellValue('L1', '添加单位');
    $objActSheet->setCellValue('M1', '添加人');

    这些字段中,证明材料一列是图片,数据库存的是图片地址

    设置表格宽度和垂直居中

           // 设置个表格宽度
               $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(16);
               $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(15);
               $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(15);
               $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(20);
               $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(12);
               $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(12);
               $objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(12);
               $objPHPExcel->getActiveSheet()->getColumnDimension('H')->setWidth(12);
               $objPHPExcel->getActiveSheet()->getColumnDimension('I')->setWidth(12);
               $objPHPExcel->getActiveSheet()->getColumnDimension('J')->setWidth(12);
               $objPHPExcel->getActiveSheet()->getColumnDimension('K')->setWidth(12);
               $objPHPExcel->getActiveSheet()->getColumnDimension('L')->setWidth(12);
               $objPHPExcel->getActiveSheet()->getColumnDimension('M')->setWidth(12); 
               // 垂直居中
               $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->setActiveSheetIndex(0)->getStyle('F')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
               $objPHPExcel->setActiveSheetIndex(0)->getStyle('G')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
               $objPHPExcel->setActiveSheetIndex(0)->getStyle('H')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
               $objPHPExcel->setActiveSheetIndex(0)->getStyle('I')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
               $objPHPExcel->setActiveSheetIndex(0)->getStyle('J')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
               $objPHPExcel->setActiveSheetIndex(0)->getStyle('K')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
               $objPHPExcel->setActiveSheetIndex(0)->getStyle('L')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
               $objPHPExcel->setActiveSheetIndex(0)->getStyle('M')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);

    组装数据,处理图片

    foreach($data as $k=>$v){
                   $k +=2;
                   $objActSheet->setCellValue('A'.$k, $v['id']);   
                   $objActSheet->setCellValue('B'.$k, $participant[$v['participant']]);   
                   $objActSheet->setCellValue('C'.$k, $status[$v['status']]);   
                   $objActSheet->setCellValue('D'.$k, $v['name']);   
                   $objActSheet->setCellValue('E'.$k, $type[$v['type']]);   
                   $objActSheet->setCellValue('F'.$k, $v['prizes']);   
                   $objActSheet->setCellValue('G'.$k, $v['issuing']);   
                   $objActSheet->setCellValue('H'.$k,$level[$v['level']]);   
                   $objActSheet->setCellValue('I'.$k, $v['dateofaward']);
                   $objActSheet->setCellValue('J'.$k, $v['awardee']);    
                           
                   //获取到图片信息  
                   // 图片生成
                   $objDrawing[$k] = new PHPExcel_Worksheet_Drawing();
                   $objDrawing[$k]->setPath('.'.$v['certificatefile']);//这里拼接 . 是因为要在根目录下获取
                   // 设置宽度高度
                   $objDrawing[$k]->setHeight(80);//照片高度
                   $objDrawing[$k]->setWidth(80); //照片宽度
                   /*设置图片要插入的单元格*/
                   $objDrawing[$k]->setCoordinates('K'.$k);
                   // 图片偏移距离
                   $objDrawing[$k]->setOffsetX(12);
                   $objDrawing[$k]->setOffsetY(12);
                   $objDrawing[$k]->setWorksheet($objPHPExcel->getActiveSheet());
                       
                   // 表格内容   
                   $objActSheet->setCellValue('L'.$k, $optionsunit_ids[$v['optionsunit_id']]);   
                   $objActSheet->setCellValue('M'.$k, $admin_ids[$v['admin_id']]);
                           
                   // 表格高度
                   $objActSheet->getRowDimension($k)->setRowHeight(80);
                       
    }

    最后给文件命名,导出下载

               $fileName = '公示表';
               $fileName = iconv("utf-8", "gb2312", $fileName);
               //重命名表
               // $objPHPExcel->getActiveSheet()->setTitle('test');
               //设置活动单指数到第一个表,所以Excel打开这是第一个表
               $objPHPExcel->setActiveSheetIndex(0);
               header('Content-Type: application/vnd.ms-excel');
               header("Content-Disposition: attachment;filename="$fileName"");
               header('Cache-Control: max-age=0');
               $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
               $objWriter->save('php://output'); //文件通过浏览器下载    
  • 相关阅读:
    dell台式机网络唤醒
    今天学习到ROS的PCC的新的一种场景,用PCC不是为了带宽叠加而是为了换IP,PCC的标准做法
    默认ros安装好以后,可能对外开放的端口号,可能被黑客利用的地方,ros的0day漏洞
    ros-winbox利用防火墙安全登录,二次碰撞方法
    S5120V2-IRF2配置
    routeros ros mikrotik的ipsec配置,注意配置的步骤,以及哪些硬件支持哪种加速
    60岁-80岁老年人住院医疗险,既往症可理赔!家有长辈首选
    docker上2分钟安装mikrotik routeros
    pandas filter数据筛选
    (纪录片)光的故事 BBC Light Fantastic (2004)
  • 原文地址:https://www.cnblogs.com/myIvan/p/11431168.html
Copyright © 2011-2022 走看看