<% using (Html.BeginForm("XXX", "Account", FormMethod.Post, new { enctype = "multipart/form-data" }))
{%>
<div class="content">
<label >请选择要导入的数据文件:</label> <input type="file" name="FileUpload1" />
<input type="submit" name="Submit" id="Submit" value="确定" />
</div>
<% }%>
<%if (ViewData["ErrorMsg"] != null)
{ %>
<div class="content" style="color:Red"><%=ViewData["ErrorMsg"].ToString()%></div>
<%} %>
<table class="content">
<tr>
<td style="border-bottom:1px solid #bdd2ed; border-right:1px solid #bdd2ed;border-top:1px solid #bdd2ed;">
XX名
</td>
<td style="border-bottom:1px solid #bdd2ed; border-right:1px solid #bdd2ed;border-top:1px solid #bdd2ed;">
是否成功
</td>
<td style="border-bottom:1px solid #bdd2ed; border-right:1px solid #bdd2ed;border-top:1px solid #bdd2ed;">
说明
</td>
</tr>
<% if (ViewData["returnUserInfo"] != null)
{
List<GTA.MS.NNU.Web.XH.Models.BatchOpenUM_UserInfo> retnInfo = (List<GTA.MS.NNU.Web.XH.Models.BatchOpenUM_UserInfo>)ViewData["returnUserInfo"];
foreach (var item in retnInfo)
{
%>
<tr>
<td style="border-bottom:1px solid #bdd2ed; border-right:1px solid #bdd2ed;">
<%: Html.Label( item.LoginName) %>
</td>
<td style="border-bottom:1px solid #bdd2ed; border-right:1px solid #bdd2ed;">
<%: Html.Label( item.IsSuccess?"成功":"失败")%>
</td>
<td style="border-bottom:1px solid #bdd2ed; border-right:1px solid #bdd2ed;">
<%: Html.Label( item.Message)%>
</td>
</tr>
<% }%>
<% } %>
</table>
#region Oledb方式读取EXCEL
/// <summary>
/// Oledb方式读取EXCEL
/// </summary>
/// <param name="fileNamePath">文件路径</param>
/// <returns></returns>
private DataTable ReadExcelByOledb(string fileNamePath)
{
string connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=Excel 8.0;data source=" + fileNamePath;
//只读第一个表
OleDbConnection oledbconn1 = new OleDbConnection(connStr);
oledbconn1.Open();
DataTable _table = oledbconn1.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
string strTableName = string.Empty;
if (_table.Rows.Count > 0)
{
strTableName = _table.Rows[0]["TABLE_NAME"].ToString().Trim();
string sql = string.Format("SELECT * FROM [{0}]", strTableName);
_table = new DataTable();
OleDbDataAdapter da = new OleDbDataAdapter(sql, oledbconn1);
da.Fill(_table);
}
oledbconn1.Close();
return _table;
}
#endregion
public ActionResult BatchAddAccount()
{
return View();
}
[RequiresAuthenticationAttribute]
[HttpPost]
public ActionResult BatchAddAccount(object obj)
{
string error = string.Empty;
List<BatchOpenUserInfo> returnUserInfo = new List<BatchOpenUserInfo>();
ViewData["returnUserInfo"] = returnUserInfo;
ViewData["ErrorMsg"] = "";
DataTable contactTable;
try
{
foreach (string upload in Request.Files)
{
if (upload != null && upload.Trim() != "")
{
string path = AppDomain.CurrentDomain.BaseDirectory + "TempData\\";
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
System.Web.HttpPostedFileBase postedFile = Request.Files[upload];
string filename = Path.GetFileName(postedFile.FileName);
if (filename.Length > 4)
{
string strExName = filename.Substring(filename.Length - 4, 4);
if (strExName.ToLower() != ".xls")
{
error = "文件类型不正确,请重新操作";
ViewData["ErrorMsg"] = error;
//return View();
}
else
{
//string filePath = Path.Combine(path, filename);
string fileNamePath = path + DateTime.Now.Ticks.ToString() + ".xls";
postedFile.SaveAs(fileNamePath);
string fileExtension;
fileExtension = System.IO.Path.GetExtension(filename);
string FileType = postedFile.ContentType.ToString();//获取要上传的文件类型,验证文件头
//在上传文件不为空的情况下,验证文件名以及大小是否符合,如果不符合则不允许上传
if (postedFile.ContentLength / 1024 <= 5120)
{ //在这里通过检查文件头与文件名是否匹配 从而限制了文件上传类型 注:可上传的类型有XLS,且大小只能为5M一下
contactTable = ReadExcelByOledb(fileNamePath);
int i = contactTable.Rows.Count;
bool success = true;
string msg = string.Empty;
UserInfo userInfo;
List<ProfessionClass> ProfessionList = GetAllProfession();
List<ProfessionClass> ClassList = GetAllProfession();
if (contactTable.Rows.Count > 1000)
{
error = "导入数据不能大于1000条!";
ViewData["ErrorMsg"] = error;
}
else
{
foreach (DataRow item in contactTable.Rows)
{
userInfo = new Management.BLL.Common.Model.UM_UserInfo();
success = CheckValue(item[0], CheckType.StringEnChNumber, 20, 0, out msg, "登录名");
if (success) { success = CheckValue(item[1], CheckType.StringEnNumber, 20, 0, out msg, "密码"); }
if (success) { success = CheckValue(item[2], CheckType.StringEnChNumber, 16, 0, out msg, "专业"); }
if (success) { success = CheckValue(item[3], CheckType.StringEnChNumber, 16, 0, out msg, "班级"); }
userInfo.LoginName = item[0].ToString();
if (success)
{
userInfo.Password = item[1].ToString();
userInfo.ProfessionalID = FindProfessionIdByName(ProfessionList, item[2].ToString());
userInfo.ClassID = FindClassIdByName(ClassList, item[3].ToString());
if (userInfo.ProfessionalID == 0)
{
msg = "系统中不存在此专业:" + item[2].ToString() + "!";
success = false;
}
else
{
if (userInfo.ClassID == 0)
{
msg = "系统中不存在此班级:" + item[3].ToString() + "!";
success = false;
}
else
{
success = CreateAccount(userInfo, out msg);
}
}
}
returnUserInfo.Add(new BatchOpenUserInfo()
{
LoginName = userInfo.LoginName,
IsSuccess = success,
Message = msg
});
}
}
}
else
{
error = "数据文件过大,请重新操作";
ViewData["ErrorMsg"] = error;
//return View();
}
}
}
else
{
error = "请选择需要导入的文件!";
ViewData["ErrorMsg"] = error;
//return View();
}
}
}
}
catch (Exception ex)
{
ViewData["ErrorMsg"] = ex.Message;
}
//return Json(returnUserInfo);
ViewData["returnUserInfo"] = returnUserInfo;
return View();
}