当前位置: 首页 > news >正文

thinkphp6入门(23)-- 如何导入excel

1. 安装phpexcel

composer require phpoffice/phpexcel

图片

composer update

2. 前端

<form class="forms-sample" action="../../xxxx/xxxx/do_import_users" method="post" enctype="multipart/form-data"><div class="control-group row"> <label>Excel表格:</label> <input type="file"  name="users_excel"/> </div> <br><button type="submit" class="btn btn-primary mr-2">导入</button>
</form>

图片

3. 后端

use PHPExcel_IOFactory;  //通过composer加载的第三方类,直接在头部引入一下就可以/*** 批量新增用户*/public function do_import_users()
{// users_excel为变量名if(!request()->file('users_excel')){return  $this->error('请上传excel文件');}$path = request()->file('users_excel');//实例化PHPExcel类$PHPExcel = new \PHPExcel();//默认用excel2007读取excel,若格式不对,则用之前的版本进行读取$PHPReader = new \PHPExcel_Reader_Excel2007();if (!$PHPReader->canRead($path)) {$PHPReader = new \PHPExcel_Reader_Excel5();if (!$PHPReader->canRead($path)) {return  $this->error('请上传excel文件');}}//读取Excel文件$PHPExcel = $PHPReader->load($path);//读取excel文件中的第一个工作表$sheet = $PHPExcel->getSheet(0);//取得最大的列号,注意,是列号,不是有多少列,比如Q$allColumn = $sheet->getHighestColumn();//取得最大的行号$allRow = $sheet->getHighestRow();// 第一行是列名,从第二行开始插入for ($currentRow = 2; $currentRow <= $allRow; $currentRow++) {//获取B列的值$data = ['number'=>$PHPExcel->getActiveSheet()->getCell("A" . $currentRow)->getValue(),'nickName'=>$PHPExcel->getActiveSheet()->getCell("B" . $currentRow)->getValue(),'name'=>$PHPExcel->getActiveSheet()->getCell("C" . $currentRow)->getValue(),'tel'=>$PHPExcel->getActiveSheet()->getCell("D" . $currentRow)->getValue(),'money'=>$PHPExcel->getActiveSheet()->getCell("E" . $currentRow)->getValue(),'time'=>self::get_date_by_excel($PHPExcel->getActiveSheet()->getCell("F" . $currentRow)->getValue()),'is_pay'=>$PHPExcel->getActiveSheet()->getCell("G" . $currentRow)->getValue(),'shop_name'=>$PHPExcel->getActiveSheet()->getCell("H" . $currentRow)->getValue(),'remarks'=>$PHPExcel->getActiveSheet()->getCell("I" . $currentRow)->getValue(),'status'=>0,'created_at'=>date('Y-m-d')];// 其它操作,比如插入数据库}}/*** excel里的字符串时间转时间*/public static function get_date_by_excel($date){if (!$date || $date == '0000-00-00') return null;$unix_time = \PHPExcel_Shared_Date::ExcelToPHP($date);return gmdate('Y-m-d H:i',$unix_time);}

注:现在主要使用phpoffice/phpspreadsheet库,phpoffice/phpexcel已经不再维护,但本文由于环境无法更新,所以就安装了phpoffice/phpexcel。基本操作一样。

参考:https://www.tpxhm.com/fdetail/725.html

注:

一、在ubuntu环境下提示 Class 'ZipArchive' not found

 查看对应的php版本

php -v

比如得到的版本是php7.4

那么

sudo apt-get install php7.4-zip

安装之后重启服务

sudo /etc/init.d/apache2 restart

最后通过 php -m | grep zip查看是否安装成功

之前有将在php.ini中将

zlib.output_compression = Off 改为 zlib.output_compression = On

不知道是否有影响

二、如果报错Call to undefined function simplexml_load_string()

 sudo apt-get install php7.4-xml php7.4-xmlrpc
sudo /etc/init.d/apache2 restart

三、如果报错Trying to access array offset on value of type int

请看

Trying to access array offset on value of type int-CSDN博客文章浏览阅读1.1w次,点赞8次,收藏6次。问题描述:出现报错信息 先百度翻译''试图访问int类型值的数组偏移量''通过翻译得知 int型的数据被其他不能使用的类型使用了(个人理解)关于这块 php7.4升级之后会有这个bug 网上大多人是说7.4 版本的向后不兼容更改,非数组的数组样式访问,现在,尝试将 null,bool,int,float 或 resource 类型的值用作数组 ( 例如 $null[“key”] ) 会产生一个通知。解决问题:我这个报错在83行 我先打印查询$pValue查看是什么_trying to access array offset on value of type inthttps://blog.csdn.net/qq5201314wx/article/details/124317321

by: 软件工程小施同学

http://www.lryc.cn/news/340925.html

相关文章:

  • 【数据结构3-栈和队列】
  • STL--list双向链表
  • ElasticSearch入门篇
  • MAXHUB会议解决方案持续进化,以“高效”为核心推动行业发展
  • CentOS 7安装Redis
  • Kubernetes (K8s) 部署前后端分离项目
  • MLT媒体程序框架01:概述
  • 9【原型模式】复制一个已存在的对象来创建新的对象
  • 谷粒商城实战(013 业务-认证服务-短信验证)
  • Unity中支持泰语--没有版权限制
  • C语言传统垃圾收集器的缺陷以及我的思路
  • 数据交换格式
  • 电力系统卫星授时信号安全隔离装置防护方案
  • 【编程TOOL】VC++6.0下载安装配置使用保姆式教程
  • 组织机构代码是哪几位?营业执照怎么看组织机构代码?
  • nginx禁止ip访问,只允许域名访问
  • 10组Python面试高频问题与详尽解答指南
  • 使用SpringBoot将中国地震台网数据保存PostGIS数据库实践
  • 后端返回树结构
  • C语言——通讯录的实现
  • Next-Scale Prediction、InstantStyle、Co-Speech Gesture Generation
  • class中 padding和margin的用法;
  • 单独使用YOLOV9的backbone网络
  • WordPress JS Support Ticket插件 RCE漏洞复现
  • 加盟代理短视频无人直播项目,开启互联网线上经营新模式
  • spring高级篇(一)
  • 免费的GPT-3.5 API服务aurora
  • 突破编程_C++_网络编程(Windows 套接字(处理 TCP 粘包问题))
  • 【训练营】DateWhale——动手学大模型应用开发(更新中)
  • 【学习笔记十九】EWM Yard Management概述及后台配置