excel导入

$fileName = "1.xls";
$import_array = array();
//获取文件读取操作
$objReader = \PHPExcel_IOFactory::load($fileName);

//对象
$sheets = $objReader->getAllSheets();
$sheet = $sheets[0];
$allRow = $sheet->getHighestRow(); // 取得总行数
$allColumn = $sheet->getHighestColumn(); //取得总列数

//每行的数据
$val = array();
//第一行代变列头,这里做key值
$header = array();

for($currentColumn='A';$currentColumn<=$allColumn;$currentColumn++){
$header_address = $currentColumn."1";
$header_cell = $sheet->getCell($header_address);
$header_cell = $header_cell->getValue();
$header[$currentColumn] = htmlspecialchars($header_cell); //处理了一下字符串

}

for($currentRow=2;$currentRow<=$allRow;$currentRow++){
for($currentColumn='A';$currentColumn<=$allColumn;$currentColumn++){
$address=$currentColumn.$currentRow;//数据坐标:A1,B1....
$cell =$sheet->getCell($address);
$cell = $cell->getValue();

$val[$header[$currentColumn]] =htmlspecialchars($cell); //处理了一下字符串


}
$import_array[] = $val;
}
array_filter($import_array);


foreach ($import_array as $k=>$value){
$arr['title']=$value['title'];
$arr['entitle']=$value['entitle'];
$arr['image']=$value['image'];
$arr['photo']=$value['photo'];
$arr['developer']=$value['developer'];
$arr['os']=$value['os'];
$arr['downloadurl']=$value['downloadurl'];
$arr['android_updated']=$value['android_updated'];
$arr['android_size']=$value['android_size'];
$arr['android_current_version']=$value['android_current_version'];
$arr['downloadurlapple']=$value['downloadurlapple'];
$arr['apple_updated']=$value['apple_updated'];
$arr['apple_size']=$value['apple_size'];
$arr['apple_current_version']=$value['apple_current_version'];
$game=Db::name("game")->insert($arr);

}


echo "<pre>";
var_dump($game);

posted @ 2023-09-12 15:58  php毛宏历的博客  阅读(4)  评论(0编辑  收藏  举报