php excel文件导出之二 图像导出
PHP文件導(dǎo)出 之圖像 和 文字同一時(shí)候?qū)С?/span>
?????? 事實(shí)上之前寫了個(gè)php文件導(dǎo)出。跟這個(gè)極為相似,由于項(xiàng)目須要對(duì)圖像進(jìn)行導(dǎo)出。查詢一番。又寫了一個(gè),
這個(gè)能實(shí)現(xiàn)圖像的導(dǎo)出(僅僅能是本地圖像,不能使用遠(yuǎn)程圖像鏈接)
'); } $imgindexs = array(5); //放入是 圖片的列的索引 第一個(gè)是0 此為一維數(shù)組 $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']; //定義站點(diǎn)根文件夾 /** 設(shè)置報(bào)錯(cuò)級(jí)別 */ 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"); /*實(shí)例化excel圖片處理類*/ $objDrawing = new PHPExcel_Worksheet_Drawing(); $width = 25; $colnums = count($headArr); for($i = 0,$startA = "A"; $i < $colnums; $i++) { // 設(shè)置列數(shù) $temp = chr(intval(ord($startA))+$i); $objPHPExcel->getActiveSheet()->getColumnDimension($temp)->setWidth($width); } //設(shè)置標(biāo)題 for($i = 0,$startA = "A"; $i < $colnums; $i++) { // 設(shè)置列數(shù) $temp = chr(intval(ord($startA))+$i).'1'; $objPHPExcel->setActiveSheetIndex(0)->setCellValue($temp, $headArr[$i]); } // Miscellaneous glyphs, UTF-8 $row=2; foreach($alist as $val){ //設(shè)置行高 $objPHPExcel->getActiveSheet()->getRowDimension($k)->setRowHeight(50); $span = 0; $startA = 'A'; //填充每一行的內(nèi)容 foreach($val as $factval){ $temp = chr(intval(ord($startA))+$span).$row; //1.圖片填充列 if(in_array($span in $imgindexs)){ /*實(shí)例化插入圖片類*/ $objDrawing = new PHPExcel_Worksheet_Drawing(); /*設(shè)置圖片路徑 切記:僅僅能是本地圖片*/ $objDrawing->setPath($dir.$factval); /*設(shè)置圖片高度*/ $objDrawing->setHeight(50); $objDrawing->setWidth(50); /*設(shè)置圖片要插入的單元格*/ $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 //注意這里 第二個(gè)參數(shù)寫成 'Excel2007' 會(huì)避免特殊字符或中文亂碼 $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007'); $objWriter->save('php://output'); exit; }
總結(jié)
以上是生活随笔為你收集整理的php excel文件导出之二 图像导出的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 使用infinite-scroll实现G
- 下一篇: 嘿嘿,JAVA里第一次运行单元测试成功,