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:
/*导入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');
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!