<?php echo '想起来了写一点'?>

接地气的php,没有高逼格

PHPExcel 导出图片
            $str=$_GET['str'];//勾选
            $str2=$_GET['str2'];//表头

            $td_field=explode(',', $str2);//表头
            $field=explode(',', $str);//勾选
            $objPHPExcel=new \PHPExcel();
            //var_dump($objPHPExcel);die;
            $objPHPExcel->getProperties()->setCreator('http://www.jb51.NET')
                ->setLastModifiedBy('http://www.jb51.Net')
                ->setTitle('Office 2007 XLSX Document')
                ->setSubject('Office 2007 XLSX Document')
                ->setDescription('Document for Office 2007 XLSX, generated using PHP classes.')
                ->setKeywords('office 2007 openxml php')
                ->setCategory('Result file');
            $objPHPExcel->setActiveSheetIndex(0)
                ->setCellValue('A1',$td_field[0])
                ->setCellValue('B1',$td_field[1])
                ->setCellValue('C1',$td_field[2])
                ->setCellValue('D1',$td_field[3])
                ->setCellValue('E1',$td_field[4])
                ->setCellValue('F1',$td_field[5])
                ->setCellValue('G1',$td_field[6])
                ->setCellValue('H1',$td_field[7])
                ->setCellValue('I1',$td_field[8])
                ->setCellValue('J1',$td_field[9]);

            $i=2;

            foreach($data as $k=>$v){
                $objActSheet = $objPHPExcel->getActiveSheet();
                // 图片生成
                $objDrawing[$k] = new \PHPExcel_Worksheet_Drawing();
                $objDrawing[$k]->setPath($v['img']);
                // 设置宽度高度
                $objDrawing[$k]->setHeight(30);//照片高度
                $objDrawing[$k]->setWidth(30); //照片宽度
                /*设置图片要插入的单元格 从第二行开始*/
                $objDrawing[$k]->setCoordinates('I'.$i);
                // 写入图片在指定格中的X坐标值
                $objDrawing[$k]->setOffsetX ( 20 );
                // 写入图片在指定格中的Y坐标值
                $objDrawing[$k]->setOffsetY ( 20 );

                $objDrawing[$k]->setWorksheet($objPHPExcel->getActiveSheet());

                $objPHPExcel->setActiveSheetIndex(0)
                    ->setCellValue('A'.$i,$v["$field[0]"])
                    ->setCellValue('B'.$i,$v["$field[1]"])
                    ->setCellValue('C'.$i,$v["$field[2]"])
                    ->setCellValue('D'.$i,$v["$field[3]"])
                    ->setCellValue('E'.$i,$v["$field[4]"])
                    ->setCellValue('F'.$i,$v["$field[5]"])
                    ->setCellValue('G'.$i,$v["$field[6]"])
                    ->setCellValue('H'.$i,$v["$field[7]"]);

                $objPHPExcel->setActiveSheetIndex(0)->getStyle('A' . $i)->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
                $objPHPExcel->setActiveSheetIndex(0)->getStyle('B' . $i)->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
                $objPHPExcel->setActiveSheetIndex(0)->getStyle('C' . $i)->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
                $objPHPExcel->setActiveSheetIndex(0)->getStyle('D' . $i)->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
                $objPHPExcel->setActiveSheetIndex(0)->getStyle('E' . $i)->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
                $objPHPExcel->setActiveSheetIndex(0)->getStyle('F' . $i)->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
                $objPHPExcel->setActiveSheetIndex(0)->getStyle('G' . $i)->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
                $objPHPExcel->setActiveSheetIndex(0)->getStyle('H' . $i)->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);

                // 水平居中(位置很重要,建议在最初始位置)
                $objPHPExcel->setActiveSheetIndex(0)->getStyle('A1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
                $objPHPExcel->setActiveSheetIndex(0)->getStyle('B1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
                $objPHPExcel->setActiveSheetIndex(0)->getStyle('C1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
                $objPHPExcel->setActiveSheetIndex(0)->getStyle('D1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
                $objPHPExcel->setActiveSheetIndex(0)->getStyle('E1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
                $objPHPExcel->setActiveSheetIndex(0)->getStyle('F1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
                $objPHPExcel->setActiveSheetIndex(0)->getStyle('G1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
                $objPHPExcel->setActiveSheetIndex(0)->getStyle('H1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
                $objPHPExcel->setActiveSheetIndex(0)->getStyle('I1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);


                // 表格高度
                $objActSheet->getRowDimension($k+2)->setRowHeight(80);
                $i++;
            }

            $objPHPExcel->getActiveSheet()->setTitle('通讯录');
            $objPHPExcel->setActiveSheetIndex(0);
            //$filename=urlencode('数据表').'_'.date('Y-m-dHis');
            $filename='通讯录'.'_'.date('Y-m-dHis');

            /*
            *生成xlsx文件
            header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
            header('Content-Disposition: attachment;filename="'.$filename.'.xlsx"');
            header('Cache-Control: max-age=0');
            $objWriter=PHPExcel_IOFactory::createWriter($objPHPExcel,'Excel2007');
            */

            //生成xls文件
            header('Content-Type: application/vnd.ms-excel');
            header('Content-Disposition: attachment;filename="'.$filename.'.xls"');
            header('Cache-Control: max-age=0');
            $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
            $objWriter->save('php://output');
            exit;

阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_38388715/article/details/80348872
个人分类: PHP
想对作者说点什么? 我来说一句

PHPExcel数据库表导出

2011年09月07日 9.88MB 下载

没有更多推荐了,返回首页

不良信息举报

PHPExcel 导出图片

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭