PHPExcel导入Excel数据 不指定

jed , 2018-3-14 09:37 , 代码编程 , 评论(0) , 阅读(101) , Via 本站原创 | |

public function importFromExcel($excel_url)
{
    //Loader::import('phpexcel.PHPExcel');
    Loader::import('phpexcel.PHPExcel.IOFactory');
    // $phpExcel = new \PHPExcel();

    $fileExt = getExt($excel_url);
    if ($fileExt == 'xls') {
        $reader = \PHPExcel_IOFactory::createReader('Excel5');
    } else {
        $reader = \PHPExcel_IOFactory::createReader('Excel2007');
    }
    //$reader = \PHPExcel_IOFactory::createReader('Excel2007');
    //$reader = \PHPExcel_IOFactory::createReader('Excel5');
    $allSheets = $reader->load('/www/FenFa/public'.$excel_url);
    $excel_arr = $allSheets->getSheet(0)->toArray();
    array_shift($excel_arr);
    $data = [];
    foreach ($excel_arr as $k=>$v) {
        if ($v[1]) {
            $data[$k]['data_time']        = strtotime(str_replace('.', '-', $v[0]));
            $data[$k]['cooperation_id']   = $v[1];
            $data[$k]['settlement_price'] = $v[2];
            $data[$k]['settlement_ratio'] = $v[3];
            $data[$k]['settlement_num']   = $v[4];
        }
    }

    foreach ($data as $v) {
        if ($v['cooperation_id']) {
            Db::name('data')->insert($v);
        }
    }
}
发表评论

昵称

网址

电邮

打开HTML 打开UBB 打开表情 隐藏 记住我 [登入] [注册]