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

相关推荐
新知图书6 天前
ThinkPHP 8 操作JSON数据
php·thinkphp
新知图书6 天前
ThinkPHP 8模型与数据的插入、更新、删除
php·thinkphp
新知图书13 天前
ThinkPHP 8的多对多关联
php·thinkphp
新知图书20 天前
ThinkPHP 8的一对一关联
php·thinkphp
新知图书23 天前
PHP与ThinkPHP连接数据库示例
开发语言·数据库·php·thinkphp
胡萝卜的兔1 个月前
thinnkphp5.1和 thinkphp6以及nginx,apache 解决跨域问题
运维·nginx·apache·thinkphp
新知图书1 个月前
ThinkPHP 8开发环境安装
thinkphp·thinkphp8
一一程序1 个月前
Thinkphp 使用workerman消息实现消息推送完整示例
websocket·gateway·thinkphp·workerman
非凡的世界1 个月前
PHP高性能webman管理系统EasyAdmin8
php·thinkphp·高性能·webman
ETO_冬1 个月前
ThinkPHP接入PayPal支付
thinkphp·paypal