使用composer安装phpspreadsheet
composer require phpoffice/phpspreadsheet
github下载:
https://github.com/phpoffice/phpspreadsheet
(免费视频教程推荐:php视频教程)
excel图片如下图:
项目实例:
use phpoffice\phpspreadsheet\cell\coordinate;use phpoffice\phpspreadsheet\iofactory;$imagefilepath = './uploads/imgs/'; //图片本地存储的路径if (!file_exists($imagefilepath)) { //如果目录不存在则递归创建 mkdir($imagefilepath, 0777, true);}try { $inputfilename = './files/1.xlsx'; //包含图片的excel文件 $objread = iofactory::createreader('xlsx'); $objspreadsheet = $objread->load($inputfilename); $objworksheet = $objspreadsheet->getsheet(0); $data = $objworksheet->toarray(); foreach ($objworksheet->getdrawingcollection() as $drawing) { list($startcolumn, $startrow) = coordinate::coordinatefromstring($drawing->getcoordinates()); $imagefilename = $drawing->getcoordinates() . mt_rand(1000, 9999); switch ($drawing->getextension()) { case 'jpg': case 'jpeg': $imagefilename .= '.jpg'; $source = imagecreatefromjpeg($drawing->getpath()); imagejpeg($source, $imagefilepath . $imagefilename); break; case 'gif': $imagefilename .= '.gif'; $source = imagecreatefromgif($drawing->getpath()); imagegif($source, $imagefilepath . $imagefilename); break; case 'png': $imagefilename .= '.png'; $source = imagecreatefrompng($drawing->getpath()); imagepng($source, $imagefilepath, $imagefilename); break; } $startcolumn = abc2decimal($startcolumn); $data[$startrow-1][$startcolumn] = $imagefilepath . $imagefilename; } dump($data);die();} catch (\exception $e) { throw $e;}public function abc2decimal($abc){ $ten = 0; $len = strlen($abc); for($i=1;$i<=$len;$i++){ $char = substr($abc,0-$i,1);//反向获取单个字符 $int = ord($char); $ten += ($int-65)*pow(26,$i-1); } return $ten;}
结果如图:
相关文章教程推荐:php教程
以上就是利用php实现读取excel中的图片的详细内容。