I have done an example of data export to Excel in a previous project. I recently needed to export image data. I have never encountered such a project, so when the process is successful, I hereby record it. I hope it can give some inspiration to friends in need!
Back -end controller method:
/*導(dǎo)入phpExcel核心類(lèi) */ 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 格式 //實(shí)例化PHPExcel類(lèi) $objPHPExcel = new PHPExcel(); $objPHPExcel->createSheet(0); $objPHPExcel->setActiveSheetIndex(0); $currentSheet = $objPHPExcel->getActiveSheet(); // $objPHPExcel->getActiveSheet()->getDefaultRowDimension()->setRowHeight(300);#設(shè)置單元格行高(此方法經(jīng)過(guò)個(gè)人測(cè)試,發(fā)現(xiàn)會(huì)出現(xiàn)excel變形) // 設(shè)置內(nèi)容居中 $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); // 設(shè)置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); // 設(shè)置頭部單元格注釋信息 $currentSheet->setCellValue('A1', "數(shù)據(jù)1"); $currentSheet->setCellValue('B1', "數(shù)據(jù)2"); $currentSheet->setCellValue('C1', "數(shù)據(jù)3"); $currentSheet->setCellValue('D1', "數(shù)據(jù)4"); $currentSheet->setCellValue('E1', "數(shù)據(jù)5"); $currentSheet->setCellValue('F1', "數(shù)據(jù)6"); $currentSheet->setCellValue('G1', "數(shù)據(jù)7"); $currentSheet->setCellValue('H1', "數(shù)據(jù)8"); $currentSheet->setCellValue('I1', "數(shù)據(jù)9"); $currentSheet->setCellValue('J1', "數(shù)據(jù)10"); $currentSheet->setCellValue('K1', "數(shù)據(jù)11"); $currentSheet->setCellValue('L1', "數(shù)據(jù)12"); $currentSheet->setCellValue('M1', "照片數(shù)據(jù)1"); $currentSheet->setCellValue('N1', "照片數(shù)據(jù)2"); $currentSheet->setCellValue('O1', "照片數(shù)據(jù)3"); $currentSheet->setCellValue('P1', "照片數(shù)據(jù)4"); $idx = 2; //查詢出數(shù)據(jù)庫(kù)內(nèi)的數(shù)據(jù)信息 //查詢出所需信息 $searchInfo = M('數(shù)據(jù)表')->select(); //組裝查詢條件 $year_time_result = 自定義設(shè)置的查詢條件; $i = 0; foreach ($searchInfo as &$y){ $i++; //設(shè)置數(shù)據(jù)所在單元格 $currentSheet->setCellValue('A' . $idx, 數(shù)據(jù)1); $currentSheet->setCellValue('B' . $idx, 數(shù)據(jù)2); $currentSheet->setCellValue('C' . $idx, 數(shù)據(jù)3); $currentSheet->setCellValue('D' . $idx, 數(shù)據(jù)4); $currentSheet->setCellValue('E' . $idx, 數(shù)據(jù)5); $currentSheet->setCellValue('F' . $idx, 數(shù)據(jù)6); $currentSheet->setCellValue('G' . $idx, 數(shù)據(jù)7); $currentSheet->setCellValue('H' . $idx, 數(shù)據(jù)8); $currentSheet->setCellValue('I' . $idx, 數(shù)據(jù)9); $currentSheet->setCellValue('J' . $idx, 數(shù)據(jù)10); $currentSheet->setCellValue('K' . $idx, 數(shù)據(jù)11); $currentSheet->setCellValue('L' . $idx, 數(shù)據(jù)12); //設(shè)置單元格高度,這個(gè)是重點(diǎn)哦 $currentSheet->getRowDimension($idx) -> setRowHeight(100); //圖片處理類(lèi),這個(gè)才是圖片導(dǎo)出的關(guān)鍵哦 $objDrawing = new PHPExcel_Worksheet_Drawing(); //開(kāi)始設(shè)置圖片 //照片數(shù)據(jù)1 if(!empty(照片數(shù)據(jù)1)){ $objDrawing->setPath(照片數(shù)據(jù)1); // 設(shè)置圖片寬度高度 $objDrawing->setHeight(100);//照片高度 $objDrawing->setWidth(100); //照片寬度 /*設(shè)置圖片要插入的單元格*/ $objDrawing->setCoordinates('M'.$idx); // 圖片偏移距離 $objDrawing->setOffsetX(0); $objDrawing->setOffsetY(0); $objDrawing->setWorksheet($objPHPExcel->getActiveSheet()); } //圖片處理類(lèi),這個(gè)才是圖片導(dǎo)出的關(guān)鍵哦 $objDrawing1 = new PHPExcel_Worksheet_Drawing(); //照片數(shù)據(jù)2 if(!empty(照片數(shù)據(jù)2)){ $objDrawing1->setPath(照片數(shù)據(jù)2); // 設(shè)置圖片寬度高度 $objDrawing1->setHeight(100);//照片高度 $objDrawing1->setWidth(100); //照片寬度 /*設(shè)置圖片要插入的單元格*/ $objDrawing1->setCoordinates('N'.$idx); // 圖片偏移距離 $objDrawing1->setOffsetX(0); $objDrawing1->setOffsetY(0); $objDrawing1->setWorksheet($objPHPExcel->getActiveSheet()); } //圖片處理類(lèi),這個(gè)才是圖片導(dǎo)出的關(guān)鍵哦 $objDrawing2 = new PHPExcel_Worksheet_Drawing(); //照片數(shù)據(jù)3 if(!empty(照片數(shù)據(jù)3)){ $objDrawing2->setPath(照片數(shù)據(jù)3); // 設(shè)置圖片寬度高度 $objDrawing2->setHeight(100);//照片高度 $objDrawing2->setWidth(100); //照片寬度 /*設(shè)置圖片要插入的單元格*/ $objDrawing2->setCoordinates('O'.$idx); // 圖片偏移距離 $objDrawing2->setOffsetX(0); $objDrawing2->setOffsetY(0); $objDrawing2->setWorksheet($objPHPExcel->getActiveSheet()); } //圖片處理類(lèi),這個(gè)才是圖片導(dǎo)出的關(guān)鍵哦 $objDrawing3 = new PHPExcel_Worksheet_Drawing(); //照片數(shù)據(jù)4 if(!empty(照片數(shù)據(jù)4)){ $objDrawing3->setPath(照片數(shù)據(jù)4); // 設(shè)置圖片寬度高度 $objDrawing3->setHeight(100);//照片高度 $objDrawing3->setWidth(100); //照片寬度 /*設(shè)置圖片要插入的單元格*/ $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="導(dǎo)出excel的文件名(可自定義)"'); header("Content-Transfer-Encoding:binary"); $write->save('php://output');
code to share. It needs to be noticed that this time this is the code for the success of my project. You must import the phpExcel core class. This class library file is the key. Free download address: ThinkPHP implements the PHPExcel class library file for exporting data to Excel files. Then it should be noted that when exporting multiple pictures Be sure to remember to instantiate multiple image variables, otherwise variable conflicts and image coverage problems will occur. The last problem is that when reading database data, if the image data does not exist in the database, you need to read the data. Determine whether it is empty, otherwise a program error will occur during operation. If these are paid attention to, then our function will run and present the program well.
The above is the detailed content of ThinkPHP's PHPExcel exports multiple image data. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)
