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 < $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 neededheader('Cache-Control: max-age=1');// If you're serving to IE over SSL, then the following may be neededheader ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the pastheader ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modifiedheader ('Cache-Control: cache, must-revalidate'); // HTTP/1.1header ('Pragma: public'); // HTTP/1.0 //注意這裡 第二個參數寫成 'Excel2007' 會避免特殊字元或中文亂碼$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');$objWriter->save('php://output');exit;}
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。