tp5 Composer安装与加载phpexcel插件与使用

Composer下载地址https://getcomposer.org/download/

安装好以后切换到项目根目录输入这串命令

使用 Composer 安装 ThinkPHP5

composer create-project topthink/think tp5 dev-master --prefer-dist

composer require phpoffice/phpexcel -vvv等待安装

加载步骤:

use PHPExcel_IOFactory;
use PHPExcel

 public function inserExcel()
    {
        //获取表单上传文件
        $file = request()->file('excel');
        $info = $file->validate(['ext' => 'xlsx'])->move(ROOT_PATH . 'public' . DS . 'uploads'.DS.'excel');
        if ($info) {
//            echo $info->getFilename();
            $exclePath = $info->getSaveName();  //获取文件名
            $file_name = ROOT_PATH . 'public' . DS . 'uploads' .DS.'excel'.DS. $exclePath;   //上传文件的地址
            //dump($file_name);
            $objReader = \PHPExcel_IOFactory::createReader('Excel2007');

            //dump($objReader);
            $obj_PHPExcel = $objReader->load($file_name, $encode = 'utf-8');  //加载文件内容,编码utf-8
            echo "
";
            $excel_array = $obj_PHPExcel->getsheet(0)->toArray();   //转换为数组格式
            array_shift($excel_array);  //删除第一个数组(标题);
            $city = [];
            dump($excel_array);
            foreach ($excel_array as $k => $v) {
                $city[$k]['Id'] = $v[0];
                $city[$k]['code'] = $v[1];
                $city[$k]['path'] = $v[2];
                $city[$k]['pcode'] = $v[3];
                $city[$k]['name'] = $v[4];
            }
            Db::name('city')->insertAll($city); //批量插入数据
        } else {
            echo $file->getError();
        }
如果报

ZipArchive::getFromName(): Invalid or uninitialized Zip object

为excel后缀问题,转换成xlsx后缀

Could not open xxx for reading! File does not exist.

为获取路径的问题


你可能感兴趣的:(php,Composer,excel)