PHPExcel读取excel数据

<?php
namespace app\index\controller;

class Index
{

    public function index()
    {
        $res = $this->goods_import('./ceshi.xlsx', 'xlsx');//读取excel表中的数据
    }

    //读取excel表中的数据
    protected function goods_import($filename, $exts = 'xls')
    {
        header("Content-Type:text/html;charset=utf-8");
        ini_set('max_execution_time','0');
        //导入PHPExcel类库,因为PHPExcel没有用命名空间,只能inport导入
        import("phpexcel/PHPExcel",EXTEND_PATH);
        // Vendor('PHPExcel');
        //创建PHPExcel对象,注意,不能少了\
        $PHPExcel = new \PHPExcel();

        //如果excel文件后缀名为.xls,导入这个类
        if ($exts == 'xls') {
            Vendor("PHPExcel.Reader.Excel5");
            $PHPReader = new \PHPExcel_Reader_Excel5();

        } else if ($exts == 'xlsx') {
            Vendor("PHPExcel.Reader.Excel2007");
            $PHPReader = new \PHPExcel_Reader_Excel2007();
        }
        //载入文件
        $PHPExcel = $PHPReader->load($filename, $encode = 'utf-8');

        //获取表中的第一个工作表,如果要获取第二个,把0改为1,依次类推
        $currentSheet = $PHPExcel->getSheet(0);
        //获取总列数
        $allColumn = $currentSheet->getHighestColumn();
        dump($allColumn);
        //获取总行数
        $allRow = $currentSheet->getHighestRow();
        dump($allRow);
        //循环获取表中的数据,$currentRow表示当前行,从哪行开始读取数据,索引值从0开始
        for ($currentRow = 1; $currentRow <= $allRow; $currentRow++) {
            //从哪列开始,A表示第一列
            for ($currentColumn = 'A'; $currentColumn <= $allColumn; $currentColumn++) {
                //数据坐标
                $address = $currentColumn . $currentRow;
                //读取到的数据,保存到数组$arr中
                $data[$currentRow][$currentColumn] = $currentSheet->getCell($address)->getValue();
            }

        }
       // return $data;
       // dump($data);die;
        $r = $this->save_import($data);
        return $r;
    }

    //将读取到的excel数据导入数据库
    public function save_import($data)
    {
        header("Content-Type:text/html;charset=utf-8");
        //dump($data);exit;
        $Goods = M('test_video');
        $video = M('kqy_video');
        foreach ($data as $k => $v) {
            $newdata['title'] = $v['A'];
            $newdata['cate'] = $v['B'];
            $newdata['industry'] = $v['C'];
            $newdata['pictures'] = $v['D'];
//            $ve =$v['E'].'xn ('.$k.').jpg';
//            $newdata['avatar']         =$ve;
            $newdata['videourl'] = $v['E'];
            $newdata['MP4'] = $v['F'];
            $newdata['prourl'] = $v['G'];
            $newdata['newvideotime'] = gmdate(" H:i:s", (new \PHPExcel_Shared_Date)->ExcelToPHP($v['H']));//从excel读取的时间转换
            $newdata['content'] = $v['I'];
            $newdata['label'] = $v['J'];
            $newdata['createtime'] = randomDate('2018-10-23 9:00:00', '2018-10-23 18:00:00');//添加时间为任意时间
            $newdata['createtimes'] = randomTime('2018-10-23 9:00:00', '2018-10-23 18:00:00');//添加时间戳
            $newdata['username'] = '13588145470';
            $newdata['userid'] = '4495';
            $newdata['videotype'] = '2';
            $newdata['result'] = '2';
            $newdata['statu'] = '1';
            $newdata['notshow'] = '1';
//            $newdata['MP4'] = $v['mp4'];
            $newdata['score'] = rand(45, 75) / 10;
            $newdata['is_automatic'] = 1;//手动上传
            $result = $video->add($newdata);
            // echo $Goods->getLastSql();
        }
        if ($result) {
            $res = '数据导入成功';
        } else {
            $res = '数据导入失败';
        }
        return $res;
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值