文章详情

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

请输入下面的图形验证码

提交验证

短信预约提醒成功

phpEXCEL导出含图片

2023-09-04 13:29

关注
public function generate_excel_one($data=array()){    // 导出Exl    import("PHPExcel.PHPExcel");    import("PHPExcel.PHPExcel.Worksheet.Drawing");    //import("PHPExcel.PHPExcel.Writer.Excel2007");    $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('B1')->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);    $objActSheet->setCellValue('A1', '序号');    $objActSheet->setCellValue('B1', '创建时间');    $objActSheet->setCellValue('C1', '分享模块');    $objActSheet->setCellValue('D1', '分享文案');    $objActSheet->setCellValue('E1', '背景图片');    $objActSheet->setCellValue('F1', '分享微信图片');    $objActSheet->setCellValue('G1', '生成海报图片');    $objActSheet->setCellValue('H1', 'APP&小程序');    $objActSheet->setCellValue('I1', '操作账号');    // 设置个表格宽度    $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(8);    $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(10);    $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(15);    $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(30);    $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()->getStyle('A')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);    $objPHPExcel->getActiveSheet()->getStyle('B')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);    $objPHPExcel->getActiveSheet()->getStyle('C')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);    $objPHPExcel->getActiveSheet()->getStyle('D')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);    $objPHPExcel->getActiveSheet()->getStyle('E')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);    $objPHPExcel->getActiveSheet()->getStyle('F')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);    $objPHPExcel->getActiveSheet()->getStyle('G')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);    $objPHPExcel->getActiveSheet()->getStyle('H')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);    $objPHPExcel->getActiveSheet()->getStyle('I')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);    $dir_mk=PUBLIC_PATH . '/download_temp/';    if (!is_dir($dir_mk)) {        @mkdir($dir_mk, 0755, true);    }    foreach((array)$data as $k=>$v){        $k +=2;        $objActSheet->setCellValue('A'.$k, $v['share_management_id']);        $objActSheet->setCellValue('B'.$k, $v['create_time']);        $objActSheet->setCellValue('C'.$k, $v['share_module_name']);        $objActSheet->setCellValue('D'.$k, $v['share_article']);        if(!empty($v['share_background_img'])) {            $objDrawing[$k] = new \PHPExcel_Worksheet_Drawing();            $temp_img_1 = $this->download($v['share_background_img'], PUBLIC_PATH . '/download_temp/');//background            $local_pic_path_1 = $dir_mk . $temp_img_1;            if (file_exists($local_pic_path_1)) {                // 图片生成 poster                $objDrawing[$k]->setPath($local_pic_path_1);                // 设置宽度高度                $objDrawing[$k]->setHeight(80);//照片高度                $objDrawing[$k]->setWidth(80); //照片宽度                //设置图片要插入的单元格                $objDrawing[$k]->setCoordinates('E' . $k);                // 图片偏移距离                $objDrawing[$k]->setOffsetX(0);                $objDrawing[$k]->setOffsetY(0);                $objDrawing[$k]->setWorksheet($objPHPExcel->getActiveSheet());            }        }        if(!empty($v['share_wechat_img'])) {            $objDrawing_k[$k] = new \PHPExcel_Worksheet_Drawing();            $temp_img_2 = $this->download($v['share_wechat_img'], PUBLIC_PATH . '/download_temp/');//wechat            $local_pic_path_2 = $dir_mk .$temp_img_2;            if (file_exists($local_pic_path_2)) {                // 图片生成 poster                $objDrawing_k[$k]->setPath($local_pic_path_2);                // 设置宽度高度                $objDrawing_k[$k]->setHeight(80);//照片高度                $objDrawing_k[$k]->setWidth(80); //照片宽度                //设置图片要插入的单元格                $objDrawing_k[$k]->setCoordinates('F' . $k);                // 图片偏移距离                $objDrawing_k[$k]->setOffsetX(0);                $objDrawing_k[$k]->setOffsetY(0);                $objDrawing_k[$k]->setWorksheet($objPHPExcel->getActiveSheet());            }        }        if(!empty($v['share_poster_img'])) {            $objDrawing_kk[$k] = new \PHPExcel_Worksheet_Drawing();            $temp_img = $this->download($v['share_poster_img'], PUBLIC_PATH . '/download_temp/');//poster            $local_pic_path = $dir_mk . $temp_img;            if (file_exists($local_pic_path)) {                // 图片生成 poster                $objDrawing_kk[$k]->setPath($local_pic_path);                // 设置宽度高度                $objDrawing_kk[$k]->setHeight(80);//照片高度                $objDrawing_kk[$k]->setWidth(80); //照片宽度                //设置图片要插入的单元格                $objDrawing_kk[$k]->setCoordinates('G' . $k);                // 图片偏移距离                $objDrawing_kk[$k]->setOffsetX(0);                $objDrawing_kk[$k]->setOffsetY(0);                $objDrawing_kk[$k]->setWorksheet($objPHPExcel->getActiveSheet());            }        }        // 表格内容        $objActSheet->setCellValue('H'.$k, $v['share_platform']);        $objActSheet->setCellValue('I'.$k, $v['admin_account_number']);        // 表格高度        $objActSheet->getRowDimension($k)->setRowHeight(80);    }    $fileName = '后台创建分享列表';    $date = date("Y-m-d",time());    $fileName .= "_{$date}.xls";    $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=100');    $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');    $objWriter->save('php://output'); //文件通过浏览器下载    // END}private function download($url, $path = './public/download_temp/'){    $ch = curl_init();    curl_setopt($ch, CURLOPT_URL, $url);    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 90);    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 信任任何证书    $file = curl_exec($ch);    curl_close($ch);    $filename = pathinfo($url, PATHINFO_BASENAME);    $resource = fopen($path . $filename, 'w');    fwrite($resource, $file);    fclose($resource);    return $filename;}

来源地址:https://blog.csdn.net/zwm_yy/article/details/131316293

阅读原文内容投诉

免责声明:

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

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

软考中级精品资料免费领

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

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

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

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

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

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

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