首页后端开发PHP利用php实现读取excel中的图片

利用php实现读取excel中的图片

时间2024-02-02 00:14:03发布访客分类PHP浏览116
导读:收集整理的这篇文章主要介绍了利用php实现读取excel中的图片,觉得挺不错的,现在分享给大家,也给大家做个参考。要实现读取excel中的图片,可以通过phpsPReadsheet来实现。phpspreadsheet是一个纯php编写的库,...
收集整理的这篇文章主要介绍了利用php实现读取excel中的图片,觉得挺不错的,现在分享给大家,也给大家做个参考。

要实现读取excel中的图片,可以通过phpsPReadsheet来实现。phpspreadsheet是一个纯php编写的库,并引入了命名空间、PSR规范等。

使用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中的图片的详细内容,更多请关注其它相关文章!

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!


若转载请注明出处: 利用php实现读取excel中的图片
本文地址: https://pptw.com/jishu/596106.html
Mac+Apache+PHP安装Xdebug方法步骤 laravel输出xml数据,php输出xml格式数据

游客 回复需填写必要信息