php excel文件导出之二 图像导出
PHP文件導出 之圖像 和 文字同一時候導出
?????? 事實上之前寫了個php文件導出。跟這個極為相似,由于項目須要對圖像進行導出。查詢一番。又寫了一個,
這個能實現圖像的導出(僅僅能是本地圖像,不能使用遠程圖像鏈接)
'); } $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 < $colnums; $i++) { // 設置列數 $temp = chr(intval(ord($startA))+$i); $objPHPExcel->getActiveSheet()->getColumnDimension($temp)->setWidth($width); } //設置標題 for($i = 0,$startA = "A"; $i < $colnums; $i++) { // 設置列數 $temp = chr(intval(ord($startA))+$i).'1'; $objPHPExcel->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; }
總結
以上是生活随笔為你收集整理的php excel文件导出之二 图像导出的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 使用infinite-scroll实现G
- 下一篇: 火狐已阻止载入混合活动内容“http:/