public function test(){ $exts = ‘xlsx‘; //导入PHPExcel类库,因为PHPExcel没有用命名空间,只能inport导入 import("Org.Util.PHPExcel");//导入Excel类 我这里是ThinkPHP框架写法 根据自己的框架修改 $fileName = "tel.xls";//excel文件路径 $objReader = \PHPExcel_IOFactory::createReader(‘Excel5‘); $objPHPExcel = $objReader->load($fileName,$encode=‘utf-8‘); $drawing = new \PHPExcel_Writer_Excel2007_Drawing(); $drawingHashTable = new \PHPExcel_HashTable(); $drawingHashTable->addFromSource($drawing->allDrawings($objPHPExcel)); for ($i = 0; $i < $drawingHashTable->count(); ++$i) { $memoryDrawing = $drawingHashTable->getByIndex($i); if ($memoryDrawing instanceof \PHPExcel_Worksheet_MemoryDrawing) { $filename = ‘./Public/‘ . $memoryDrawing->getCoordinates() . ‘_‘ . $memoryDrawing->getHashCode() . ‘.jpg‘; // 将图片存到指定的目录 imagejpeg($memoryDrawing->getImageResource(), $filename); // 获得该图片所在的单元格 $cell = $memoryDrawing->getWorksheet()->getCell($memoryDrawing->getCoordinates()); // 将该单元格的值设置为单元格的文本加上图片的 img 标签 $cell->setValue($cell->getValue() . $filename); } } $sheet = $objPHPExcel->getSheet(0); $highestRow = $sheet->getHighestRow(); // 取得总行数 $highestColumn = $sheet->getHighestColumn(); // 取得总列数 for($i=2;$i<=$highestRow;$i++) { $data[$i][‘truename‘] = $objPHPExcel->getActiveSheet()->getCell("A".$i)->getValue(); $data[$i][‘account‘]= $objPHPExcel->getActiveSheet()->getCell("B".$i)->getValue(); $data[$i][‘image‘][] = $objPHPExcel->getActiveSheet()->getCell("C".$i)->getValue(); $data[$i][‘image‘][] = $objPHPExcel->getActiveSheet()->getCell("D".$i)->getValue(); $data[$i][‘image‘][] = $objPHPExcel->getActiveSheet()->getCell("E".$i)->getValue(); } print_r($data);die; }
时间: 2024-10-11 11:00:00