[HttpPost]
[LoginFilter]
public async Task<IHttpActionResult> ImportUseByXlsx([FromUri] string departmentId)
{
List<Users> list = new List<Users>();
if (string.IsNullOrWhiteSpace(departmentId))
{
return Json(new Result("departmentId部门不可 为空"));
}
var result = await departmentBusiness.GetAsync(departmentId);
if (result.Data == null)
{
return Json(new Result("根据departmentId没有找到部门"));
}
HttpFileCollection fileCollection = HttpContext.Current.Request.Files;
Stream stream = fileCollection[0].InputStream;
NPOI.SS.UserModel.IWorkbook wb = new NPOI.XSSF.UserModel.XSSFWorkbook(stream);
NPOI.SS.UserModel.ISheet sheet = wb.GetSheetAt(0);
for (int i = 1; i < sheet.LastRowNum + 1; i++)
{
NPOI.SS.UserModel.IRow row = sheet.GetRow(i);
if (row == null)
{
break;
}
string account = string.Empty;
NPOI.SS.UserModel.ICell accountCell = row.GetCell(0);
if (accountCell != null)
{
account = accountCell.ToString();
}
if (string.IsNullOrWhiteSpace(account))
{
break;
}
string realName = string.Empty;
NPOI.SS.UserModel.ICell realNameCell = row.GetCell(1);
if (realNameCell != null)
{
realName = realNameCell.ToString();
}
string phone = string.Empty;
NPOI.SS.UserModel.ICell phoneCell = row.GetCell(2);
if (phoneCell != null && phoneCell.CellType == NPOI.SS.UserModel.CellType.String)
{
phone = phoneCell.StringCellValue;
}
else {
phone = phoneCell.ToString();
}
string email = string.Empty;
NPOI.SS.UserModel.ICell emailCell = row.GetCell(3);
if (emailCell != null)
{
email = emailCell.ToString();
}
string idcard = string.Empty;
NPOI.SS.UserModel.ICell idcardCell = row.GetCell(4);
if (idcardCell != null)
{
idcard = idcardCell.ToString();
}
string job = string.Empty;
NPOI.SS.UserModel.ICell jobCell = row.GetCell(5);
if (jobCell != null)
{
job = jobCell.ToString();
}
list.Add(new Users()
{
Department_id = departmentId,
Account = account,
Real_name = realName,
Phone=phone,
Email = email,
Sf_card = idcard,
Job= job
});
}
var result5 = await userBusiness.AddListAsync(list, CurrentLoginUser);
return Json(result5);
}