当前位置 : 主页 > 网络编程 > PHP >

PHPExcel导入Excel数据

来源:互联网 收集:自由互联 发布时间:2021-06-28
gistfile1.txt 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
gistfile1.txt
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);
        }
    }
}
网友评论