laravel PhpSpreadsheet 导入类库

setReadDataOnly(TRUE);
        $spreadsheet = IOFactory::load($path);// 载入excel表格
        $worksheet = $spreadsheet->getActiveSheet();
        $highestRow = $worksheet->getHighestRow(); // 总行数
        $highestColumn = $worksheet->getHighestColumn(); // 总列数
        $highestColumnIndex = Coordinate::columnIndexFromString($highestColumn);
        $data = [];
        for ($row = $readnum; $row <= $highestRow; ++$row) { // 从第几行开始读取
            $row_data = [];
            for ($column = 1; $column <= $highestColumnIndex; $column++) {
                $row_data[] = $worksheet->getCellByColumnAndRow($column, $row)->getValue();
            }
            $data[] = $row_data; //获取
        }
        return $data;
    }

}

 

引用:

 $excel= new Excel()

 $data = $excel->readExcel($path,2)

DB::beginTransaction();
foreach ($data as $key=>$val){
    $result = ['a'=>$val[0],'b'=>$val[1]]
    \DB::table($table)->insert($result);
    if($key%10000 == 0){
         DB::commit();
        DB::beginTransaction();
    }

}
DB::commit(); // 每隔10000条数据 提交一次

你可能感兴趣的:(laravel PhpSpreadsheet 导入类库)