如何使用ThinkPHP6导入Excel表格并嵌入图片?

2026-04-29 13:159阅读0评论SEO资源
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计597个文字,预计阅读时间需要3分钟。

如何使用ThinkPHP6导入Excel表格并嵌入图片?

Excel导入导出编写了不少,但一直没导入过图片,正好要用到,所以研究并记录一下。安装phpoffice和composer仓库,直接搜索phpoffice,下载次数最多的第一个就是。编辑版本数据,根据自己PHP版本选择合适的版本。


excle导入导出写了不少,但是一直没导入过图片,正好要用到,所以研究并记录一下

安装phpoffice

composer仓库直接搜索phpoffice,下载次数最多的,第一个就是

编辑

版本根据自己PHP版本而定,我用的thinkphp6,php7.3,就用最新版本了

项目根目录输入以下命令

composer require phpoffice/phpspreadsheet

出现下图所示,表示安装成功

编辑

安装不成功,可能是权限问题,检查composer.json和vendor,如果用centos的话不能用root用户,需新建用户执行composer命令

PHP代码

大概实现思路,先将excle上传保存到服务器,在读取excle处理数据和图片



if($this->app->request->isPost()){ try { $file = $this->app->request->file('inputFile'); validate([ 'file' => [ // 限制文件大小(单位b),这里限制为4M //fileSize' => 4 * 1024 * 1024, 'fileExt' => 'xlsx,xls' ] ], [ //'file.fileSize' => '文件太大', 'file.fileExt' => '不支持的文件', ] )->check(['file'=>$file]); $savename = \think\facade\Filesystem::putFile( 'import', $file); $import_path = root_path() . 'runtime/storage/' . $savename; $spreadsheet = IOFactory::load($import_path); $sheet = $spreadsheet->getActiveSheet(); $sheetData = $sheet->toArray(); if(empty($sheetData) || !is_array($sheetData)){ $this->error('上传失败'); } /*************图片单独处理开始*****************/ $imageFilePath=root_path().'/public/uploads/images/' ;//图片保存目录 if (!file_exists ( $imageFilePath )) { mkdir("$imageFilePath", 0777, true); } //处理图片 foreach($sheet->getDrawingCollection() as $img) { list($startColumn,$startRow)= Coordinate::coordinateFromString($img->getCoordinates());//获取图片所在行和列 $imageFileName = date('YmdHis').mt_rand(1000,9999);//图片名字随机生成 switch($img->getMimeType()) { case 'image/jpg': case 'image/jpeg': $imageFileName.='.jpg'; imagejpeg($img->getImageResource(),$imageFilePath.$imageFileName); break; case 'image/gif': $imageFileName.='.gif'; imagegif($img->getImageResource(),$imageFilePath.$imageFileName); break; case 'image/png': $imageFileName.='.png'; imagepng($img->getImageResource(),$imageFilePath.$imageFileName); break; } $startColumn = $this->ABC2decimal($startColumn);//由于图片所在位置的列号为字母,转化为数字 $sheetData[$startRow-1][$startColumn]='/uploads/images/'.$imageFileName;//把图片插入到数组中 } /*************图片单独处理结束*****************/ $res = $this->upload_excle($sheetData);//自定义入库方法 if($res > 0)$this->success('成功导入'.$res.'条数据'); $this->error('导入失败!!!'); } catch (\think\exception\ValidateException $e) { $this->error($e->getMessage()); } }

来一张成功的图片

编辑




如何使用ThinkPHP6导入Excel表格并嵌入图片?

本文共计597个文字,预计阅读时间需要3分钟。

如何使用ThinkPHP6导入Excel表格并嵌入图片?

Excel导入导出编写了不少,但一直没导入过图片,正好要用到,所以研究并记录一下。安装phpoffice和composer仓库,直接搜索phpoffice,下载次数最多的第一个就是。编辑版本数据,根据自己PHP版本选择合适的版本。


excle导入导出写了不少,但是一直没导入过图片,正好要用到,所以研究并记录一下

安装phpoffice

composer仓库直接搜索phpoffice,下载次数最多的,第一个就是

编辑

版本根据自己PHP版本而定,我用的thinkphp6,php7.3,就用最新版本了

项目根目录输入以下命令

composer require phpoffice/phpspreadsheet

出现下图所示,表示安装成功

编辑

安装不成功,可能是权限问题,检查composer.json和vendor,如果用centos的话不能用root用户,需新建用户执行composer命令

PHP代码

大概实现思路,先将excle上传保存到服务器,在读取excle处理数据和图片



if($this->app->request->isPost()){ try { $file = $this->app->request->file('inputFile'); validate([ 'file' => [ // 限制文件大小(单位b),这里限制为4M //fileSize' => 4 * 1024 * 1024, 'fileExt' => 'xlsx,xls' ] ], [ //'file.fileSize' => '文件太大', 'file.fileExt' => '不支持的文件', ] )->check(['file'=>$file]); $savename = \think\facade\Filesystem::putFile( 'import', $file); $import_path = root_path() . 'runtime/storage/' . $savename; $spreadsheet = IOFactory::load($import_path); $sheet = $spreadsheet->getActiveSheet(); $sheetData = $sheet->toArray(); if(empty($sheetData) || !is_array($sheetData)){ $this->error('上传失败'); } /*************图片单独处理开始*****************/ $imageFilePath=root_path().'/public/uploads/images/' ;//图片保存目录 if (!file_exists ( $imageFilePath )) { mkdir("$imageFilePath", 0777, true); } //处理图片 foreach($sheet->getDrawingCollection() as $img) { list($startColumn,$startRow)= Coordinate::coordinateFromString($img->getCoordinates());//获取图片所在行和列 $imageFileName = date('YmdHis').mt_rand(1000,9999);//图片名字随机生成 switch($img->getMimeType()) { case 'image/jpg': case 'image/jpeg': $imageFileName.='.jpg'; imagejpeg($img->getImageResource(),$imageFilePath.$imageFileName); break; case 'image/gif': $imageFileName.='.gif'; imagegif($img->getImageResource(),$imageFilePath.$imageFileName); break; case 'image/png': $imageFileName.='.png'; imagepng($img->getImageResource(),$imageFilePath.$imageFileName); break; } $startColumn = $this->ABC2decimal($startColumn);//由于图片所在位置的列号为字母,转化为数字 $sheetData[$startRow-1][$startColumn]='/uploads/images/'.$imageFileName;//把图片插入到数组中 } /*************图片单独处理结束*****************/ $res = $this->upload_excle($sheetData);//自定义入库方法 if($res > 0)$this->success('成功导入'.$res.'条数据'); $this->error('导入失败!!!'); } catch (\think\exception\ValidateException $e) { $this->error($e->getMessage()); } }

来一张成功的图片

编辑




如何使用ThinkPHP6导入Excel表格并嵌入图片?