之前在項目上有做過數據導出Excel的實例,最近需要匯出圖片數據,沒遇過這樣的項目,所以在處理成功之際,特此記錄一下,希望可以給有需要的小夥伴一絲靈感!
後端控制器方法:
/*导入phpExcel核心类 */ require_once APP_PATH.'PHPExcel/PHPExcel.php'; require_once APP_PATH.'PHPExcel/PHPExcel/Writer/Excel5.php'; // 用于其他低版本xls require_once APP_PATH.'PHPExcel/PHPExcel/Writer/Excel2007.php'; // 用于 excel-2007 格式 //实例化PHPExcel类 $objPHPExcel = new PHPExcel(); $objPHPExcel->createSheet(0); $objPHPExcel->setActiveSheetIndex(0); $currentSheet = $objPHPExcel->getActiveSheet(); // $objPHPExcel->getActiveSheet()->getDefaultRowDimension()->setRowHeight(300);#设置单元格行高(此方法经过个人测试,发现会出现excel变形) // 设置内容居中 $objPHPExcel->setActiveSheetIndex(0)->getStyle('A')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel->setActiveSheetIndex(0)->getStyle('B')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel->setActiveSheetIndex(0)->getStyle('C')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel->setActiveSheetIndex(0)->getStyle('D')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel->setActiveSheetIndex(0)->getStyle('E')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel->setActiveSheetIndex(0)->getStyle('F')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel->setActiveSheetIndex(0)->getStyle('G')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel->setActiveSheetIndex(0)->getStyle('H')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel->setActiveSheetIndex(0)->getStyle('I')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel->setActiveSheetIndex(0)->getStyle('J')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel->setActiveSheetIndex(0)->getStyle('K')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel->setActiveSheetIndex(0)->getStyle('L')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel->setActiveSheetIndex(0)->getStyle('M')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel->setActiveSheetIndex(0)->getStyle('N')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel->setActiveSheetIndex(0)->getStyle('O')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel->setActiveSheetIndex(0)->getStyle('P')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER); // 设置excel宽度 $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(20); $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(20); $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(20); $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(20); $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(20); $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(20); $objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(20); $objPHPExcel->getActiveSheet()->getColumnDimension('H')->setWidth(20); $objPHPExcel->getActiveSheet()->getColumnDimension('I')->setWidth(20); $objPHPExcel->getActiveSheet()->getColumnDimension('J')->setWidth(20); $objPHPExcel->getActiveSheet()->getColumnDimension('K')->setWidth(20); $objPHPExcel->getActiveSheet()->getColumnDimension('L')->setWidth(20); // 设置头部单元格注释信息 $currentSheet->setCellValue('A1', "数据1"); $currentSheet->setCellValue('B1', "数据2"); $currentSheet->setCellValue('C1', "数据3"); $currentSheet->setCellValue('D1', "数据4"); $currentSheet->setCellValue('E1', "数据5"); $currentSheet->setCellValue('F1', "数据6"); $currentSheet->setCellValue('G1', "数据7"); $currentSheet->setCellValue('H1', "数据8"); $currentSheet->setCellValue('I1', "数据9"); $currentSheet->setCellValue('J1', "数据10"); $currentSheet->setCellValue('K1', "数据11"); $currentSheet->setCellValue('L1', "数据12"); $currentSheet->setCellValue('M1', "照片数据1"); $currentSheet->setCellValue('N1', "照片数据2"); $currentSheet->setCellValue('O1', "照片数据3"); $currentSheet->setCellValue('P1', "照片数据4"); $idx = 2; //查询出数据库内的数据信息 //查询出所需信息 $searchInfo = M('数据表')->select(); //组装查询条件 $year_time_result = 自定义设置的查询条件; $i = 0; foreach ($searchInfo as &$y){ $i++; //设置数据所在单元格 $currentSheet->setCellValue('A' . $idx, 数据1); $currentSheet->setCellValue('B' . $idx, 数据2); $currentSheet->setCellValue('C' . $idx, 数据3); $currentSheet->setCellValue('D' . $idx, 数据4); $currentSheet->setCellValue('E' . $idx, 数据5); $currentSheet->setCellValue('F' . $idx, 数据6); $currentSheet->setCellValue('G' . $idx, 数据7); $currentSheet->setCellValue('H' . $idx, 数据8); $currentSheet->setCellValue('I' . $idx, 数据9); $currentSheet->setCellValue('J' . $idx, 数据10); $currentSheet->setCellValue('K' . $idx, 数据11); $currentSheet->setCellValue('L' . $idx, 数据12); //设置单元格高度,这个是重点哦 $currentSheet->getRowDimension($idx) -> setRowHeight(100); //图片处理类,这个才是图片导出的关键哦 $objDrawing = new PHPExcel_Worksheet_Drawing(); //开始设置图片 //照片数据1 if(!empty(照片数据1)){ $objDrawing->setPath(照片数据1); // 设置图片宽度高度 $objDrawing->setHeight(100);//照片高度 $objDrawing->setWidth(100); //照片宽度 /*设置图片要插入的单元格*/ $objDrawing->setCoordinates('M'.$idx); // 图片偏移距离 $objDrawing->setOffsetX(0); $objDrawing->setOffsetY(0); $objDrawing->setWorksheet($objPHPExcel->getActiveSheet()); } //图片处理类,这个才是图片导出的关键哦 $objDrawing1 = new PHPExcel_Worksheet_Drawing(); //照片数据2 if(!empty(照片数据2)){ $objDrawing1->setPath(照片数据2); // 设置图片宽度高度 $objDrawing1->setHeight(100);//照片高度 $objDrawing1->setWidth(100); //照片宽度 /*设置图片要插入的单元格*/ $objDrawing1->setCoordinates('N'.$idx); // 图片偏移距离 $objDrawing1->setOffsetX(0); $objDrawing1->setOffsetY(0); $objDrawing1->setWorksheet($objPHPExcel->getActiveSheet()); } //图片处理类,这个才是图片导出的关键哦 $objDrawing2 = new PHPExcel_Worksheet_Drawing(); //照片数据3 if(!empty(照片数据3)){ $objDrawing2->setPath(照片数据3); // 设置图片宽度高度 $objDrawing2->setHeight(100);//照片高度 $objDrawing2->setWidth(100); //照片宽度 /*设置图片要插入的单元格*/ $objDrawing2->setCoordinates('O'.$idx); // 图片偏移距离 $objDrawing2->setOffsetX(0); $objDrawing2->setOffsetY(0); $objDrawing2->setWorksheet($objPHPExcel->getActiveSheet()); } //图片处理类,这个才是图片导出的关键哦 $objDrawing3 = new PHPExcel_Worksheet_Drawing(); //照片数据4 if(!empty(照片数据4)){ $objDrawing3->setPath(照片数据4); // 设置图片宽度高度 $objDrawing3->setHeight(100);//照片高度 $objDrawing3->setWidth(100); //照片宽度 /*设置图片要插入的单元格*/ $objDrawing3->setCoordinates('P'.$idx); // 图片偏移距离 $objDrawing3->setOffsetX(0); $objDrawing3->setOffsetY(0); $objDrawing3->setWorksheet($objPHPExcel->getActiveSheet()); } $idx++; } $write = new PHPExcel_Writer_Excel5($objPHPExcel); header("Pragma: public"); header("Expires: 0"); header("Cache-Control:must-revalidate, post-check=0, pre-check=0"); header("Content-Type:application/force-download"); header("Content-Type:application/vnd.ms-execl"); header("Content-Type:application/octet-stream"); header("Content-Type:application/download");; header('Content-Disposition:attachment;filename="导出excel的文件名(可自定义)"'); header("Content-Transfer-Encoding:binary"); $write->save('php://output');
代碼分享完了,需要小伙伴們注意的是,此次整理的這個是我項目實踐成功的代碼,首先需要注意的是,一定要導入phpExcel核心類,這個類庫檔案是關鍵,免費下載位址:ThinkPHP實作資料匯出為Excel檔案的PHPExcel類庫檔案,然後要注意的是,在匯出多個圖片的時候一定要記住實例化多個圖片變量,否則會出現變量衝突,圖片覆蓋的問題,最後一個問題,需要注意的是讀取數據庫數據的時候,如果數據庫內不存在圖片數據的話,需要進行數據是否為空的判斷,否則運行會出現程式錯誤異常,如果這些都注意做好,那麼我們的這項功能將會很好的進行程式運行和呈現。
以上是ThinkPHP之PHPExcel匯出多張圖片數據的詳細內容。更多資訊請關注PHP中文網其他相關文章!