php excel文件导出之二 图像导出

来源:互联网 发布:淘宝首页服务 编辑:程序博客网 时间:2024/05/16 07:24

PHP文件导出 之图像 和 文字同时导出

       其实之前写了个php文件导出,跟这个极为相似,因为项目需要对图像进行导出,查询一番,又写了一个,

这个能实现图像的导出(只能是本地图像,不能使用远程图像链接)


[php] view plain copy
  1. /** 
  2.      * 导出对应的活动投票记录 
  3.      */  
  4.     public function exportxls(){  
  5.   
  6.         $alist = datafrom db;    //从数据库获取相应的数据  
  7.   
  8.         //1. 从数据库来获取对应的二维数组    
  9.         $alist = array(...);    
  10.         $list = $alist;    
  11.         $data = array();    
  12.         //2. 设置xls的 表头名    
  13.         $headArr = array("排名","姓名","手机","获奖","参与时间");    
  14.         if(false === empty($list)){    
  15.             $i=0;    
  16.             foreach ($list as $key => $val){    
  17.                 //组装对应的单元格A,B,C,D。。。    
  18.                 $data[$i] = array(    
  19.                        ($i+1),            //A    
  20.                        $val['name'],      //B    
  21.                        $val['tel'],       //C    
  22.                        $val['award'],     //D    
  23.                        ...    
  24.                     );    
  25.                  $i++;    
  26.             }    
  27.         }else{    
  28.             $data[0] = array('暂无相关记录!');    
  29.         }    
  30.   
  31.         $imgindexs = array(5); //放入是 图片的列的索引 第一个是0 此为一维数组  
  32.   
  33.         $fileName = "your name-".date('Y-m-d');  
  34.         $this->output_customer($headArr,$data,$fileName,$imgindexs);  
  35.     }  
  36.   
  37.   
  38.     public function output_customer($headArr,$alist,$filename,$imgindexs){  
  39.   
  40.         set_time_limit(0);  
  41.         ini_set('memory_limit''-1');  
  42.   
  43.         $dir = $_SERVER['DOCUMENT_ROOT']; //定义网站根目录  
  44.   
  45.         /** 设置报错级别 */  
  46.         error_reporting(E_ALL);  
  47.         ini_set('display_errors', TRUE);  
  48.         ini_set('display_startup_errors', TRUE);  
  49.         if (PHP_SAPI == 'cli')  
  50.         die('This example should only be run from a Web Browser');  
  51.   
  52.         /** Include PHPExcel */  
  53.         require_once $dir.'/public/phpexcel/PHPExcel.php';  
  54.   
  55.   
  56.         // Create new PHPExcel object  
  57.         $objPHPExcel = new PHPExcel();  
  58.   
  59.         // Set document properties  
  60.         $objPHPExcel->getProperties()->setCreator("Maarten Balliauw")  
  61.         ->setLastModifiedBy("Maarten Balliauw")  
  62.         ->setTitle("Office 2007 XLSX Test Document")  
  63.         ->setSubject("Office 2007 XLSX Test Document")  
  64.         ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")  
  65.         ->setKeywords("office 2007 openxml php")  
  66.         ->setCategory("Test result file");  
  67.   
  68.         /*实例化excel图片处理类*/  
  69.         $objDrawing = new PHPExcel_Worksheet_Drawing();  
  70.   
  71.         $width = 25;  
  72.         $colnums = count($headArr);  
  73.         for($i = 0,$startA = "A"$i < $colnums$i++) {  
  74.             // 设置列数  
  75.             $temp = chr(intval(ord($startA))+$i);  
  76.             $objPHPExcel->getActiveSheet()->getColumnDimension($temp)->setWidth($width);  
  77.         }  
  78.   
  79.         //设置标题  
  80.         for($i = 0,$startA = "A"$i < $colnums$i++) {  
  81.             // 设置列数  
  82.             $temp = chr(intval(ord($startA))+$i).'1';  
  83.             $objPHPExcel->setActiveSheetIndex(0)->setCellValue($temp$headArr[$i]);  
  84.         }  
  85.   
  86.         // Miscellaneous glyphs, UTF-8  
  87.         $row=2;  
  88.         foreach($alist as $val){  
  89.             //设置行高  
  90.             $objPHPExcel->getActiveSheet()->getRowDimension($k)->setRowHeight(50);  
  91.   
  92.             $span = 0;  
  93.             $startA = 'A';  
  94.             //填充每一行的内容  
  95.             foreach($val as $factval){  
  96.                $temp = chr(intval(ord($startA))+$span).$row;  
  97.                //1.图片填充列  
  98.                if(in_array($span in $imgindexs)){  
  99.                     /*实例化插入图片类*/  
  100.                     $objDrawing = new PHPExcel_Worksheet_Drawing();  
  101.                     /*设置图片路径 切记:只能是本地图片*/  
  102.                     $objDrawing->setPath($dir.$factval);  
  103.                     /*设置图片高度*/  
  104.                     $objDrawing->setHeight(50);  
  105.                     $objDrawing->setWidth(50);  
  106.                     /*设置图片要插入的单元格*/  
  107.                     $objDrawing->setCoordinates($temp);  
  108.                     $objDrawing->setWorksheet($objPHPExcel->getActiveSheet());        
  109.                }else{  
  110.                    //2.非图片填充列  
  111.                    $objPHPExcel->setActiveSheetIndex(0)->setCellValue($temp$factval);  
  112.                }  
  113.                $span++;  
  114.             }  
  115.             $row++;   
  116.         }  
  117.   
  118.   
  119.         // 重命名 worksheet  
  120.         $date = date('Y-m-d');  
  121.         $objPHPExcel->getActiveSheet()->setTitle($filename);  
  122.   
  123.         // Set active sheet index to the first sheet, so Excel opens this as the first sheet  
  124.         $objPHPExcel->setActiveSheetIndex(0);  
  125.   
  126.         $filename = iconv("utf-8""gb2312"$filename.date('Y-m-d'));  
  127.   
  128.         header('Content-Type: application/vnd.ms-excel;');  
  129.         header('Content-Disposition: attachment;filename='.$filename.'.xls"');  
  130.         header('Cache-Control: max-age=0');  
  131.         // If you're serving to IE 9, then the following may be needed  
  132.         header('Cache-Control: max-age=1');  
  133.   
  134.         // If you're serving to IE over SSL, then the following may be needed  
  135.         header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past  
  136.         header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified  
  137.         header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1  
  138.         header ('Pragma: public'); // HTTP/1.0  
  139.   
  140.                //注意这里 第二个参数写成 'Excel2007' 会避免特殊字符或中文乱码  
  141.         $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel'Excel2007');  
  142.         $objWriter->save('php://output');  
  143.   
  144.         exit;  
  145.     } 
原创粉丝点击