phpexecel 图片读取
piaoling 2014-03-13 15:05:49
function importExcel($inputFileName){ $objPHPExcel = PHPExcel_IOFactory::load($inputFileName); $drawing = new PHPExcel_Writer_Excel2007_Drawing(); $drawingHashTable = new PHPExcel_HashTable(); $drawingHashTable->addFromSource($drawing->allDrawings($objPHPExcel)); // var_dump($drawingHashTable->count()); $pics = array(); for ($i = 0; $i < $drawingHashTable->count(); ++$i) { $memoryDrawing = $drawingHashTable->getByIndex($i); //var_dump($memoryDrawing); $coordinate = $memoryDrawing->getCoordinates(); $hashCode = $memoryDrawing->getHashCode(); //var_dump($coordinate); //var_dump($hashCode); if ($memoryDrawing instanceof PHPExcel_Worksheet_MemoryDrawing) { $src = $memoryDrawing->getPath(); $filename = 'images/'.$memoryDrawing->getHashCode().$memoryDrawing->getFilename(); if(file_exists($filename)){ $filename ='images/'.rand(100000, 999999).'_'.$memoryDrawing->getHashCode().$memoryDrawing->getFilename(); } copy($src, $filename); $pics[$coordinate][] =$filename; /* $filename = 'images/' . $memoryDrawing->getCoordinates() . '_' . $memoryDrawing->getHashCode() . '.jpg'; // 将图片存到指定的目录 imagejpeg($memoryDrawing->getImageResource(), $filename); // 获得该图片所在的单元格 $cell = $memoryDrawing->getWorksheet()->getCell($memoryDrawing->getCoordinates()); // 将该单元格的值设置为单元格的文本加上图片的 img 标签 var_dump($filename); //$cell->setValue($cell->getValue() . $filename ); * */ }elseif($memoryDrawing instanceof PHPExcel_Worksheet_Drawing){ $src = $memoryDrawing->getPath(); $filename = 'images/'.$memoryDrawing->getHashCode().$memoryDrawing->getFilename(); if(file_exists($filename)){ $filename ='images/'.rand(100000, 999999).'_'.$memoryDrawing->getHashCode().$memoryDrawing->getFilename(); } copy($src, $filename); $pics[$coordinate][] =$filename; } } //var_dump($pics); //echo '<hr />'; $sheetData = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true); //var_dump($sheetData); $products = array(); foreach($sheetData[4] as $colkey=>$sku){ if($colkey=='A'){ continue; } if(!isset($sheetData[5][$colkey]) || !isset($sheetData[6][$colkey])|| !isset($sheetData[7][$colkey]) || !isset($sheetData[1][$colkey]) || !isset($pics[$colkey.'2']) || !isset($pics[$colkey.'3']) ){ echo $inputFileName.'<br />'; echo $colkey.' not import<br />'; continue; } $product = array(); $product['sku']=$sku; $product['name'] = $sheetData[5][$colkey]; $product['color'] = $sheetData[6][$colkey]; $product['size'] = $sheetData[7][$colkey]; $product['collection'] = $sheetData[1][$colkey]; $product['picture'] = $pics[$colkey.'2']; $product['picture_zoom'] = $pics[$colkey.'3']; $products[] = $product; } return $products; }
发表评论(评论将通过邮件发给作者):