Heim > Artikel > tägliche Programmierung > PHPExcel von ThinkPHP exportiert mehrere Bilddaten
Ich habe kürzlich Daten zum Exportieren von Bilddaten erstellt. Ich bin auf ein solches Projekt gestoßen und hoffe, es hiermit ein wenig zu inspirieren für Freunde in Not! : Die Back-End-Controller-Methode: 控
/*导入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');
Der Code wird geteilt. Diesmal muss der Code für den Erfolg in meinem Projekt geklärt werden Die Kernkategorie der PHPExcel-Datei ist der Schlüssel zum kostenlosen Download:
ThinkPHP implementiert die PHPExcel-Klassenbibliotheksdatei zum Exportieren von Daten in Excel-Dateien. Beachten Sie, dass Sie beim Exportieren mehrerer Bilder daran denken müssen, mehrere zu instanziieren Andernfalls kommt es zu Variablenkonflikten und Problemen beim Überschreiben von Bildern. Das letzte Problem besteht darin, dass beim Lesen von Datenbankdaten beurteilt werden muss, ob die Daten leer sind. Andernfalls tritt ein Programmfehler auf Ausnahmen, wenn diese beachtet werden, läuft unsere Funktion und stellt das Programm sehr gut dar.Das obige ist der detaillierte Inhalt vonPHPExcel von ThinkPHP exportiert mehrere Bilddaten. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!