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