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: 软件工程小施同学

相关推荐
张小勇2 天前
thinkphp通过with查询,并通过关联表进行筛选
thinkphp·fastadmin
一一程序6 天前
ThinkPHP-导入Excel表格(通用版)
php·excel·thinkphp·thinkphp导入excel
张小勇12 天前
thinkphp单独为某个接口设置缓存
thinkphp
洪、20 天前
记录一下PHP使用微信小程序支付
微信小程序·php·thinkphp
Z3r4y23 天前
【代码审计】star7th/showdoc:v3.2.4 Phar反序列化写webshell
web·代码审计·thinkphp·漏洞复现·showdoc·xve-2023-28617·guzzlehttp
521源码1 个月前
521源码-网站源码-Thinkphp聊天室H5实时聊天室群聊聊天室自动分配账户完群组/私聊/禁言等功能/全开源运营版本
php·thinkphp·对话·客服·聊天·群聊
poem-rain1 个月前
thinkphp6 queue队列的maxTries自定义
数据库·sql·php·thinkphp
图图爱上壮壮妈1 个月前
读取器 thinkphp 文件预处理
php·thinkphp·读取器
Yehger1 个月前
Thinkphp 使用Model来增删改查
运维·服务器·thinkphp
SuperherRo1 个月前
代码审计-PHP框架开发篇&ThinkPHP&版本缺陷&不安全写法&路由访问&利用链
php·thinkphp