public function actionImportDo()
{
ini_set('memory_limit', '-1');
set_time_limit(0);
$postData = Yii::$app->request->post();
$type = $postData['type'];
if (!isset($_FILES['file'])) {
PdUtils::ajaxReturn('', '请单击"选择文件"选择信息文件', 2);
}
$fileType = substr($_FILES['file']['name'], strrpos($_FILES['file']['name'], '.') + 1);
if (!in_array($fileType, array('xlsx', 'xls', 'docx', 'doc'))) {
PdUtils::ajaxReturn('', '信息文件格式错误,应为<font color="red">xlsx或xls或者docx</font>文件', 2);
}
require_once Url::to('@common') . '/extensions/PHPExcel-1.8/Classes/PHPExcel.php';
$objPHPExcel = new \PHPExcel();
$ver = $fileType == 'xlsx' ? 'Excel2007' : 'Excel5';
$objReader = \PHPExcel_IOFactory::createReader($ver);
$excel = $objReader->load($_FILES['file']['tmp_name']);
$data = $excel->setActiveSheetIndex(0)->toArray();
try {
$transaction = Yii::$app->db->beginTransaction();
$data = array_slice($data, 1);
if (!$data) {
PdUtils::ajaxReturn('', '文件中没有要上传的数据,请重新选择', 2);
}
$error_str = '';
foreach ($data as $key => $value) {
if (empty($value)) {
continue;
}
$line = $key + 2;
$type = trim($value[1]);
$pro = trim($value[2]);
$city = trim($value[3]);
$init_id = trim($value[4]);
$event_name = trim($value[5]);
$date = trim($value[9]);
if (!in_array($value[7], ['男', '女'])) {
$error_str .="第" . $line . "行性别必须是男、女其中一个<br>";
}
$sex_type = trim($value[7]) == "男" ? '1' : '2';
// 工作单位+姓名+出生年月+性别 验证唯一
$expert_exists = ExpertInfo1::find()->where(['phone'=>trim($value[16]),'status'=>1])->exists();
if($expert_exists) {
$error_str .= "第" . $line . "行".trim($value[6])."手机号已存在<br>";
}
$expert_exists = ExpertInfo1::find()->where(['name'=>trim($value[6]),'sex'=>$sex_type,'unit'=>trim($value[15]),'status'=>1])->exists();
if ($expert_exists) {
$error_str .= "第" . $line . "行的姓名+工作单位+性别 信息重复<br>";
}
$model = new ExpertInfo1();
//添加学校表
$model->uuid = Uuid::uuid();
$model->recommend_unit_id = $unitname['uuid'];
$model->event_id = $event['uuid'];
$model->province = $province['uuid'];
$model->city = $city;
$model->name = trim($value[6]);
$model->sex = $sex_type;//判断男女
$model->political_orientation = trim($value[8]);
$model->birthday = trim($value[9]);
$model->nation = trim($value[10]);
$model->diploma = trim($value[11]);
$model->degree = trim($value[12]);
$model->maxhigh_degree_school = trim($value[13]);
$model->maxhigh_degree_major = trim($value[14]);
$model->professional_qualification_level=trim($value[22]);
$model->lecturing_major=trim($value[23]);
$model->professional_working_years=trim($value[24]);
$model->achievements=trim($value[25]);
$model->competition_experiences=trim($value[26]);
$model->awards=trim($value[27]);
$model->status=1;
$model->create_users_id = Yii::$app->user->identity->uuid;
$model->update_users_id = Yii::$app->user->identity->uuid;
$model->create_time = AppUtil::get_format_time();
$model->update_time = AppUtil::get_format_time();
if (!$model->save()) {
$err = AppUtil::get_error_msg($model);
$error_str .= '第' . $line . "行".$err."<br>";
}
// 日志
Logs::addLog($model->uuid, '专家信息汇总表1-导入');
}
if($error_str != '') {
PdUtils::ajaxReturn('', $error_str, 2);
$transaction->rollBack();
}else {
$transaction->commit();
PdUtils::ajaxReturn('', '导入成功',1);
}
} catch (\Exception $exc) {
if (isset($transaction)) {
$transaction->rollBack();
PdUtils::ajaxReturn('', '导入失败',2);
}
}
}
<?php
use yii\helpers\Url;
use common\components\PdUtils;
//$this->registerJsFile('@web/js/common.js', ['position'=>$this::POS_HEAD]);
?>
<script type="text/javascript" src="<?php echo Yii::$app->request->baseUrl; ?>/js/common.js"></script>
<div class="teams-create" style="padding: 20px;">
<form id="dataForm" action="" method="post" enctype="multipart/form-data">
<div class="form-group">
<div class="aa" style="margin-top: -1px;text-align: center; line-height: 50px;margin-bottom: 10px;">
</div>
<table class="table table-bordered table-condensed" style="width: 100%;">
<tr>
<td>上传说明</td>
<td>上传文件必须是.xlsx文件。</td>
</tr>
<tr>
<td>模板下载</td>
<td><a href="<?php echo Url::to('@web/files/expertuser.xlsx');?>" style="color: blue">模板下载</a></td>
</tr>
<tr>
<td>上传文件</td>
<td>
<input type="file" name="file" id="exampleInputFile">
<input type="hidden" name="submit" value="1">
<input type="hidden" name="type" value="<?php echo $type;?>">
<input type="hidden" name="school_major_id" value="<?php echo $school_major_id;?>">
</td>
</tr>
</table>
</div>
</br>
<div class="footer" style="height: 50px;text-align: center; line-height: 50px">
<input class="btn btn-primary" onclick="postData()" type="button" value="上传" />
<a href="#" class="btn btn-primary" data-dismiss="modal" aria-hidden="true" style="">关闭</a>
</div>
</form>
</div>
<script>
function postData(){
var formData = new FormData();
formData.append("file",$("#exampleInputFile")[0].files[0]);
formData.append("service",'App.Passion.UploadFile');
formData.append("submit",1);
formData.append("type","<?php echo $type; ?>");
formData.append("school_major_id","<?php echo $school_major_id; ?>");
$.ajax({
url:'<?= Url::to(['import-do']) ?>',
type:'post',
data: formData,
contentType: false,
processData: false,
success:function(res){
var obj = JSON.parse(res);
myAlert(obj["info"], null, function(){
if(obj["status"]==1){
location.reload();
}
});
}
})
}
</script>