1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > tp框架 文件上传+excel表格导入

tp框架 文件上传+excel表格导入

时间:2018-11-05 22:18:44

相关推荐

tp框架 文件上传+excel表格导入

tp框架 文件上传+excel表格导入

poser下载扩展phpexcel2.配置前端页面html原生代码layui图片/文件上传模块 - layui.upload3.php文件上传代码tp6写法tp5&5.1写法4.php 导入的Excel文件解析

poser下载扩展phpexcel

根目录下执行命令 composer require phpoffice/phpexcel

2.配置前端页面

这里我写了两种方式供参考使用哪一种都可以

html原生代码

<form action="/index/upload" enctype="multipart/form-data" method="post"><input type="file" name="file" /> <br> <input type="submit" value="上传" /> </form>

layui图片/文件上传模块 - layui.upload

详细参数链接,注意这不是layui的官方文档站(官网文档站已关闭),不知道什么时候就没了

layui.use('upload', function(){var upload = layui.upload;//执行实例var uploadInst = upload.render({elem: '#excel_into' //绑定元素,url: '/dzqycms/customer/upload' //上传接口,accept:'file'//文件类型,acceptMime:"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"//限制文件类型,exts:'xlsx'//选择文件时显示的后缀,done: function(res){console.log(res);//上传完毕回调},error: function(){//请求异常回调}});});

3.php文件上传代码

tp6写法

tp6文件上传官网详细文档

public function upload(){$file = request()->file('file');$filename = \think\facade\Filesystem::disk('public')->putFile( 'excel', $file);}

tp5&5.1写法

[tp5.1文件上传官网详细文档](/manual/thinkphp5_1/354121)

public function upload(){// 获取表单上传文件 例如上传了001.jpg$file = request()->file('file');// 移动到框架应用根目录/public/uploads/ 目录下if($file){$info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');if($info){// 成功上传后 获取上传信息// 输出 jpgecho $info->getExtension();// 输出 0820/42a79759f284b767dfcb2a0197904287.jpgecho $info->getSaveName();// 输出 42a79759f284b767dfcb2a0197904287.jpgecho $info->getFilename(); }else{// 上传失败获取错误信息echo $file->getError();}}}

4.php 导入的Excel文件解析

use PHPExcel_IOFactory; //别忘了引用PHPExcel_IOFactory类public function parsing($filename)//$filename上传的文件路径{$objPHPExcelReader = PHPExcel_IOFactory::load($filename); //加载excel文件$sheet = $objPHPExcelReader->getSheet(0); // 读取第一個工作表$highestRow = $sheet->getHighestRow(); // 取得总行数$highestColumm = $sheet->getHighestColumn(); // 取得总列数$highestColumm++;$rowIndex = 2;for ($rowIndex; $rowIndex <= $highestRow; $rowIndex++) {//循环读取每个单元格的内容。注意行从1开始,列从A开始$a = 1;for ($colIndex = 'A'; $colIndex != $highestColumm; $colIndex++) {$addr = $colIndex . $rowIndex;echo $addr.PHP_EOL;$value = $sheet->getCell($addr)->getValue();if (empty($value)){return ['code'=>2,'msg'=>"位置:$addr,的数据不能为空"];}$data[$rowIndex][$a] =$value;$a++;}}var_dump($data);die;}

excel表格内容:

执行代码打印出的数据:

5分钟完活,剩下的就是根据自己代码逻辑来了。

(强烈安利大家一个动漫,《国外排名》👇👇,工作之余也要放松一下啊)

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。