您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

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

2025/10/30 12:46:58发布17次查看
php文件导出 之图像 和 文字同时导出
       其实之前写了个php文件导出,跟这个极为相似,因为项目需要对图像进行导出,查询一番,又写了一个,
这个能实现图像的导出(只能是本地图像,不能使用远程图像链接)
/** * 导出对应的活动投票记录 */ public function exportxls(){ $alist = datafrom db; //从数据库获取相应的数据 //1. 从数据库来获取对应的二维数组 $alist = array(...); $list = $alist; $data = array(); //2. 设置xls的 表头名 $headarr = array(排名,姓名,手机,获奖,参与时间); if(false === empty($list)){ $i=0; foreach ($list as $key => $val){ //组装对应的单元格a,b,c,d。。。 $data[$i] = array( ($i+1), //a $val['name'], //b $val['tel'], //c $val['award'], //d ... ); $i++; } }else{ $data[0] = array('暂无相关记录!'); } $imgindexs = array(5); //放入是 图片的列的索引 第一个是0 此为一维数组 $filename = your name-.date('y-m-d'); $this->output_customer($headarr,$data,$filename,$imgindexs); } public function output_customer($headarr,$alist,$filename,$imgindexs){ set_time_limit(0); ini_set('memory_limit', '-1'); $dir = $_server['document_root']; //定义网站根目录 /** 设置报错级别 */ error_reporting(e_all); ini_set('display_errors', true); ini_set('display_startup_errors', true); if (php_sapi == 'cli') die('this example should only be run from a web browser'); /** include phpexcel */ require_once $dir.'/public/phpexcel/phpexcel.php'; // create new phpexcel object $objphpexcel = new phpexcel(); // set document properties $objphpexcel->getproperties()->setcreator(maarten balliauw) ->setlastmodifiedby(maarten balliauw) ->settitle(office 2007 xlsx test document) ->setsubject(office 2007 xlsx test document) ->setdescription(test document for office 2007 xlsx, generated using php classes.) ->setkeywords(office 2007 openxml php) ->setcategory(test result file); /*实例化excel图片处理类*/ $objdrawing = new phpexcel_worksheet_drawing(); $width = 25; $colnums = count($headarr); for($i = 0,$starta = a; $i getactivesheet()->getcolumndimension($temp)->setwidth($width); } //设置标题 for($i = 0,$starta = a; $i setactivesheetindex(0)->setcellvalue($temp, $headarr[$i]); } // miscellaneous glyphs, utf-8 $row=2; foreach($alist as $val){ //设置行高 $objphpexcel->getactivesheet()->getrowdimension($k)->setrowheight(50); $span = 0; $starta = 'a'; //填充每一行的内容 foreach($val as $factval){ $temp = chr(intval(ord($starta))+$span).$row; //1.图片填充列 if(in_array($span in $imgindexs)){ /*实例化插入图片类*/ $objdrawing = new phpexcel_worksheet_drawing(); /*设置图片路径 切记:只能是本地图片*/ $objdrawing->setpath($dir.$factval); /*设置图片高度*/ $objdrawing->setheight(50); $objdrawing->setwidth(50); /*设置图片要插入的单元格*/ $objdrawing->setcoordinates($temp); $objdrawing->setworksheet($objphpexcel->getactivesheet()); }else{ //2.非图片填充列 $objphpexcel->setactivesheetindex(0)->setcellvalue($temp, $factval); } $span++; } $row++; } // 重命名 worksheet $date = date('y-m-d'); $objphpexcel->getactivesheet()->settitle($filename); // set active sheet index to the first sheet, so excel opens this as the first sheet $objphpexcel->setactivesheetindex(0); $filename = iconv(utf-8, gb2312, $filename.date('y-m-d')); header('content-type: application/vnd.ms-excel;'); header('content-disposition: attachment;filename='.$filename.'.xls'); header('cache-control: max-age=0'); // if you're serving to ie 9, then the following may be needed header('cache-control: max-age=1'); // if you're serving to ie over ssl, then the following may be needed header ('expires: mon, 26 jul 1997 05:00:00 gmt'); // date in the past header ('last-modified: '.gmdate('d, d m y h:i:s').' gmt'); // always modified header ('cache-control: cache, must-revalidate'); // http/1.1 header ('pragma: public'); // http/1.0 //注意这里 第二个参数写成 'excel2007' 会避免特殊字符或中文乱码 $objwriter = phpexcel_iofactory::createwriter($objphpexcel, 'excel2007'); $objwriter->save('php://output'); exit; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product