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

  1. 安装phpexcel

    composer require phpoffice/phpexcel

复制代码
composer update
  1. 前端
html 复制代码
<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>
  1. 后端
php 复制代码
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: 软件工程小施同学

相关推荐
vipxieliang2 天前
ThinkORM 3.0 入门:强类型模型定义与查询构造器
后端·php·thinkphp
vipxieliang7 天前
ThinkPHP 8.0 注解路由实战:PHP 8 原生 Attribute 方案
php·thinkphp
vipxieliang9 天前
ThinkPHP 8.0 路由完全重写:新写法与迁移适配指南
php·thinkphp
vipxieliang19 天前
ThinkPHP MVC 模式入门:从控制器到视图的完整实践
后端·php·thinkphp
2301_32241428041 个月前
基于web的校园失物招领管理系统59695-计算机毕设 原创(免费领源码+部署教程+开发环境)
mysql·nginx·php·apache·thinkphp·phpstudy·wampserver
VX_bysjlw9851 个月前
基于PHP的论坛系统设计与实现 35136-计算机毕设原创(免费领源码+带部署教程)
开发语言·php·apache·laravel·thinkphp·phpstudy·wampserver
ymwlchina2 个月前
phpStudy 如何部署符号象客户关系管理系统(ThinkPHP 8)记录
crm·thinkphp·phpstudy·网站部署·小皮面板·客户关系管理系统
在呢3 个月前
ThinkPHP 事务机制详解
thinkphp
安妮的小熊呢5 个月前
CRMEB BZ v6.0 使用教程:从安装部署到后台基础配置
php·thinkphp·电商系统·crmeb
withoutfear5 个月前
Fastadmin中获取IP和手机号归属地信息
php·thinkphp·fastadmin·ip归属地·手机号归属地