$file = $_FILES['file'];
if(!$file['size']){
message('檔案上傳不能為空!', '', 'error');
}
if( $file['name'] && $file['error'] == 0){
$type = @end( explode('.', $file['name']));
$type = strtolower($type);
if( !in_array($type, array('xls','xlsx','csv')) ){
message('檔案類型錯誤!', '', 'error');
}
set_time_limit(0);
include_once IA_ROOT . '/framework/library/phpexcel/PHPExcel.php';
include_once IA_ROOT . '/framework/library/phpexcel/PHPExcel/IOFactory.php';
if( $type == 'xls' ){
$inputFileType = 'Excel5'; //這個是讀 xls的
}else{
$inputFileType = 'Excel2007';//這個是計xlsx的
}
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objPHPExcel = $objReader->load($file['tmp_name']);
$sheet = $objPHPExcel->getSheet(0);
//擷取行數與列數,注意列數需要轉換
$highestRow = $sheet->getHighestRow();
$highestColumn = $sheet->getHighestColumn();
$highestColumnNum = PHPExcel_Cell::columnIndexFromString($highestColumn);
$usefullColumnNum = $highestColumnNum;
for ($row = 2;$row <= $highestRow;$row++){
$user_name = $sheet->getCellByColumnAndRow(0, $row)->getValue();//姓名
$user_code = $sheet->getCellByColumnAndRow(1, $row)->getValue();//抽獎碼
$user_phone = $sheet->getCellByColumnAndRow(2, $row)->getValue();//手機号
if(count(explode('-', $user_code))>1){
$user_status=2;
}else{
$user_status=1;
}
$data = array(
'user_name' => $user_name,
'user_phone' => $user_phone,
'user_activity_id' => $user_activity_id,
'user_status' => $user_status,
'user_code' => $user_code,
'user_creat_time' => date("Y-m-d H:i:s",time())
);
pdo_insert('draw_users',$data);
}
itoast('導入成功', $this->createWebUrl('users'));
}else{
itoast('導入失敗', referer());
}