/// <summary>
/// 将excel数据解析并插入数据库
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
[DirectMethod]
public void UpLoad_btnClick(object sender, DirectEventArgs e)
{
string jobID = Request.Cookies["jobId"].Value;
string jobName = ZLCP.Common.Function.GetCookieValue(this, "jobName");
int BatchID = Int32.Parse(Request.QueryString["batchid"]);
int parterID = CurrentUser.PartnerId;
ArrayList list = new ArrayList();
if (!FileUploadField.HasFile)
{
X.Msg.Alert("提示", "请选择上传的文件!").Show();
return;
}
//获取客户端FileUploadField文件的扩展名并验证
fileExtenSion = Path.GetExtension(FileUploadField.FileName).ToLower();
if (fileExtenSion.ToLower() != ".xlsx" && fileExtenSion.ToLower() != ".xls")
{
X.Msg.Alert("提示", "上传的文件格式不正确!").Show();
return;
}
DataTable dt = xsldata();
if (dt.Rows.Count > 0 && (dt.Rows[0][0].ToString() != "姓名" || dt.Rows[0][4].ToString() != "专业" || dt.Rows[0][6].ToString() != "邮箱" || dt.Rows[0][1].ToString() != "性别"))
{
X.Msg.Alert("提示", "请使用正确的模板!").Show();
return;
}
if (dt.Rows.Count==1)
{
X.Msg.Alert("提示", "导入的表格为空!").Show();
return;
}
for (int i = 1; i < dt.Rows.Count; i++)
{
Regex emailregex = new Regex(@"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$");
Regex telregex = new Regex(@"(\d{11})|^((\d{7,8})|(\d{4}|\d{3})-(\d{7,8})|(\d{4}|\d{3})-(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1})|(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1}))$");
Match em = emailregex.Match(dt.Rows[i][6].ToString());
string tel = dt.Rows[i][7].ToString().Replace(" ","");
Match t = telregex.Match(tel);
if (dt.Rows[i][0].ToString() != "" && dt.Rows[i][1].ToString() != "" && dt.Rows[i][2].ToString() != "" && dt.Rows[i][3].ToString() != "" && dt.Rows[i][4].ToString() != "" && dt.Rows[i][5].ToString() != "" && dt.Rows[i][6].ToString() != "" && dt.Rows[i][7].ToString() != ""&&em.Success&&t.Success)
{
string Name = dt.Rows[i][0].ToString();
bool Sex = dt.Rows[i][1].ToString() == "男" ? true : false;
int Age = Convert.ToInt32(dt.Rows[i][2].ToString());
string Degree = dt.Rows[i][3].ToString();
string Major = dt.Rows[i][4].ToString();
string Current = dt.Rows[i][5].ToString();
string Email = dt.Rows[i][6].ToString();
string Tel = dt.Rows[i][7].ToString();
AddToPerson atp = new AddToPerson();
//判断该职位下边的批次是否关闭
int batchID = atp.JobStatus(int.Parse(jobID), BatchID,CurrentUser.UserId);
//判断Email是否已经存在
int a = atp.selectInfo(Email);
if (a == 0)
{
DateTime datetime = DateTime.Now;
//给Person表赋值
Person ps = new Person();
ps.PartnerID = parterID;
ps.RegisterTime = datetime;
//给InfoToRecruitment表赋值
InfoToRecruitment info = new InfoToRecruitment();
info.Name = Name;
info.Age = Age;
info.Degree = atp.getDegree(Degree);
info.Gender = Sex;
info.Major = Major;
info.Career = Current;
info.Telephone = Tel;
info.Email = Email;
info.ResumeDeliverTime = datetime;
//给Batch_Person表赋值
Batch_Person bp = new Batch_Person();
bp.BatchID = batchID;
int m = atp.InsertTable(ps, info, bp);
if (m >= 3)
{
//Response.Redirect("~/CompanyPlatform/RecruitmentUI.aspx?JPID=" + Request.QueryString["JPID"] + "");
if (i == dt.Rows.Count - 1)
{
if (list.Count > 1)
{
X.Msg.Show(
new MessageBoxConfig
{
Title = "提示",
Message = "导入成功!" + " <br> " + "以下人员信息不完整或者" + " <br> " + "信息格式非法未能导入(" + list[0] + "," + list[1] + "...)" ,
Buttons = MessageBox.Button.OK,
MessageBoxButtonsConfig = new MessageBoxButtonsConfig
{
Ok = new MessageBoxButtonConfig
{
Text = "确定",
Handler = "window.location.href='RecruitmentUI.aspx?JPID=" + int.Parse(jobID) + "'"//Request.QueryString["JPID"] + "')"
}
}
});
}
else
{
X.Msg.Show(
new MessageBoxConfig
{
Title = "提示",
Message = list.Count > 0 ? "导入成功!" + " <br> " + "以下人员信息不完整或者" + " <br> " + "信息格式非法未能导入(" + list[0] + ")" : "导入成功!",
Buttons = MessageBox.Button.OK,
MessageBoxButtonsConfig = new MessageBoxButtonsConfig
{
Ok = new MessageBoxButtonConfig
{
Text = "确定",
Handler = "window.location.href='RecruitmentUI.aspx?JPID=" + int.Parse(jobID) + "'"//Request.QueryString["JPID"] + "')"
}
}
});
}
}
}
else
{
X.Msg.Alert("提示", "导入失败!").Show();
return;
}
}
else
{
//判断先插入的职位于之间是是否相同
int count = atp.GetjobIDCount(Email, int.Parse(jobID));
//给InfoToRecruitment表更新数据
int n = atp.UpdateTable(Name, Age, Degree, Sex, Major, Current, Tel, Email);
if (n == 0)
{
X.Msg.Alert("提示", "导入失败!").Show();
return;
}
if(count==0)
{
atp.InsertTableBP(batchID,Email);
}
if (i == dt.Rows.Count - 1)
{
if (list.Count > 1)
{
X.Msg.Show(
new MessageBoxConfig
{
Title = "提示",
Message = "导入成功!" + " <br> " + "以下人员信息不完整或者" + " <br> " + "信息格式非法未能导入(" + list[0] + "," + list[1] + "...)",
Buttons = MessageBox.Button.OK,
MessageBoxButtonsConfig = new MessageBoxButtonsConfig
{
Ok = new MessageBoxButtonConfig
{
Text = "确定",
Handler = "window.location.href='RecruitmentUI.aspx?JPID=" + int.Parse(jobID) + "'"//?JPID=" + Request.QueryString["JPID"] + "')"
}
}
});
}
else
{
X.Msg.Show(
new MessageBoxConfig
{
Title = "提示",
Message = list.Count > 0 ? "导入成功!" + " <br> " + "以下人员信息不完整或者" + " <br> " + "信息格式非法未能导入(" + list[0] + ")" : "导入成功!",
Buttons = MessageBox.Button.OK,
MessageBoxButtonsConfig = new MessageBoxButtonsConfig
{
Ok = new MessageBoxButtonConfig
{
Text = "确定",
Handler = "window.location.href='RecruitmentUI.aspx?JPID=" + int.Parse(jobID) + "'"//?JPID=" + Request.QueryString["JPID"] + "')"
}
}
});
}
}
}
}
else
{
if (dt.Rows[i][0].ToString() != "")
list.Add(dt.Rows[i][0].ToString());
if (i == dt.Rows.Count - 1)
{
if (list.Count > 1)
{
X.Msg.Show(
new MessageBoxConfig
{
Title = "提示",
Message = "导入成功!" + " <br> " + "以下人员信息不完整或者" + " <br> " + "信息格式非法未能导入(" + list[0] + "," + list[1] + "...)",
Buttons = MessageBox.Button.OK,
MessageBoxButtonsConfig = new MessageBoxButtonsConfig
{
Ok = new MessageBoxButtonConfig
{
Text = "确定",
Handler = "window.location.href='RecruitmentUI.aspx?JPID=" + int.Parse(jobID) + "'"//?JPID=" + Request.QueryString["JPID"] + "')"
}
}
});
}
else
{
X.Msg.Show(
new MessageBoxConfig
{
Title = "提示",
Message = list.Count > 0 ? "导入成功!" + " <br> " + "以下人员信息不完整或者" + " <br> " + "信息格式非法未能导入(" + list[0] + ")" : "导入成功!",
Buttons = MessageBox.Button.OK,
MessageBoxButtonsConfig = new MessageBoxButtonsConfig
{
Ok = new MessageBoxButtonConfig
{
Text = "确定",
Handler = "window.location.href='RecruitmentUI.aspx?JPID=" + int.Parse(jobID) + "'"//?JPID=" + Request.QueryString["JPID"] + "')"
}
}
});
}
}
}
}
}
public DataTable xsldata()
{
try
{
string FileName = "~/Download/" + Path.GetFileName(FileUploadField.FileName);
if (File.Exists(Server.MapPath(FileName)))
{
File.Delete(Server.MapPath(FileName));
}
FileUploadField.PostedFile.SaveAs(Server.MapPath(FileName));
//HDR=Yes,这代表第一行是标题,不做为数据使用 ,如果用HDR=NO,则表示第一行不是标题,做为数据来使用。系统默认的是YES
string connstr2003 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath(FileName) + ";Extended Properties='Excel 8.0;HDR=NO;IMEX=1;'";
string connstr2007 = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Server.MapPath(FileName) + ";Extended Properties=\"Excel 12.0;HDR=NO\"";
OleDbConnection conn;
if (fileExtenSion.ToLower() == ".xls")
{
conn = new OleDbConnection(connstr2003);
}
else
{
conn = new OleDbConnection(connstr2007);
}
conn.Open();
string sql = "select * from [Sheet1$]";
OleDbCommand cmd = new OleDbCommand(sql, conn);
DataTable dt = new DataTable();
OleDbDataReader sdr = cmd.ExecuteReader();
dt.Load(sdr);
sdr.Close();
conn.Close();
//删除服务器里上传的文件
if (File.Exists(Server.MapPath(FileName)))
{
File.Delete(Server.MapPath(FileName));
}
return dt;
}
catch (Exception e)
{
return null;
}
}