您现在的位置是: 首页 > ThinkPHP8 ThinkPHP8
thinkphp6导入带图片的 Excel 表格
冬寂
2020-08-22 16:30:14
【ThinkPHP8】
6066人已围观
一、安装 phpoffice
代码仓库地址:https://packagist.org/packages/phpoffice/phpspreadsheet
或直接执行composer命令
composer require phpoffice/phpspreadsheet
出现下图所示,表示安装成功
安装不成功,可能是权限问题,检查composer.json和vendor,如果用centos的话不能用root用户,需新建用户执行composer命令
二、PHP代码
实现思路,先将excle上传保存到服务器,在读取excle处理数据和图片
//引入参考
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
use PhpOffice\PhpSpreadsheet\IOFactory;
use think\facade\Db;
use think\facade\Filesystem;
use think\facade\Request;
use think\facade\Session;
use think\response\Json;
use app\admin\model\Insurance as InsuranceModel;
/**
* 上传**数据
* Method upload_excel
* @return void
* author LIU 1278175138@qq.com
* Time 2020/8/21 17:46
* 版权 **集团
*/
Public function upload_excel(){
if($this->app->request->isPost()) {
$insuranceModel = new InsuranceModel();
try {
$file = $this->app->request->file('file');
validate([
'file' => [
'fileExt' => 'xlsx,xls'
]
],
[
'file.fileExt' => '不支持的文件',
]
)->check(['file' => $file]);
$savename = Filesystem::disk('public')->putFile('excel', $file);
$import_path = root_path() . 'public/upload/' . $savename;
$spreadsheet = IOFactory::load($import_path);
$sheet = $spreadsheet->getActiveSheet();
$sheetData = $sheet->toArray();
if (empty($sheetData) || !is_array($sheetData)) {
$this->error('上传失败');
}
/*************图片单独处理开始*****************/
$imageFilePath = root_path() . '/public/upload/excelimages/';//图片保存目录
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 = $insuranceModel->ABCToDecimal($startColumn);//由于图片所在位置的列号为字母,转化为数字
$sheetData[$startRow - 1][$startColumn] = '/upload/excelimages/' . $imageFileName;//把图片插入到数组中
}
/*************图片单独处理结束*****************/
foreach ($sheetData as $key=>$vo){
unset($sheetData[0]);
if($vo[0] == '') unset($sheetData[$key]);
}
if($sheetData == []) return json(['code'=>202,'msg'=>'没有数据!']);
//到这里已经得到了导入的数据(数组),根据自己的业务逻辑,定义存储方法
$res = $this->uploadExcel($sheetData);
if ($res > 0) return json(['code'=>200,'msg'=>'成功导入' . $res . '条数据']);
return json(['code'=>202,'msg'=>'导入失败!']);
} catch (\think\exception\ValidateException $e) {
return json(['code'=>202,'msg'=>$e->getMessage()]);
}
}
}
//上述的一个外链方法
/**
* 上传文件列名转换为数字
* Method ABC2decimal
* @param $startColumn
* @return int|string
* author LIU 1278175138@qq.com
* Time 2020/8/21 11:35
* 版权 **集团
*/
Public function ABCToDecimal($startColumn){
$arr = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'];
foreach ($arr as $k=>$vo){
if($vo == $startColumn){
return $k;
}
}
}
下一篇: ThinkPHP插件集合
相关文章
随机图文
-
耳鸣的处理思路
耳鸣真正造成的困扰,主要来自大脑对异常声音的警觉和情绪放大,进而影响睡眠、情绪和注意力,而不是耳鸣声音本身在“持续伤害身体”。 -
韦伯太空望远镜将改写宇宙历史(The Webb Space Telescope Will Rewrite Cosmic History. If It Works.)
韦伯太空望远镜将改写宇宙历史(The Webb Space Telescope Will Rewrite Cosmic History. If It Works.),韦伯首张深空场 -
写了个“张雪峰”提示词,让 AI 免费做志愿规划
辅助填写志愿的提示词,主要作用是引导学生发掘出个人特点、家庭背景、优/劣势学科、行业前景等因素,然后给学生推荐适合的行业/职业,目前使用市面上主流的 AI 测试了一波,Google Gemini 、通义千问跟 ChatGPT 表现还不错 -
执行40秒断开,90秒断开,自动断开,解决 PHP 执行时间过长导致断开连接的问题
当遇到 PHP 脚本执行时间超过40秒导致断开连接的问题时,通常需要调整相关的服务器和 PHP-FPM(FastCGI Process Manager)配置。
