问题:如何批量导入用户和组织?
解答:
进入后台管理,点开组织机构下面的同步设置。
如果没有现成的AD用户,就做个EXCEL表导入数据到数据库
Excel导入组织架构的模板和关键代码
前台代码:
组织信息导入
后台代码:
protected void Button7_Click(object sender, EventArgs e)
{
IWorkbook workbook = null;
if (FileUpload7.HasFile)
{
string upfile = Server.MapPath("") + "/Temp/" + FileUpload7.PostedFile.FileName;
if (!Directory.Exists(Server.MapPath("") + "/Temp/"))
{
Directory.CreateDirectory(Server.MapPath("") + "/Temp/");
}
FileUpload7.PostedFile.SaveAs(upfile);
string newpath = upfile;
using (FileStream fs = File.OpenRead(newpath)) //打开myxls.xls文件
{
if (FileUpload7.PostedFile.FileName.IndexOf(".xlsx") > 0) // 2007版本
workbook = new XSSFWorkbook(fs);
else if (FileUpload7.PostedFile.FileName.IndexOf(".xls") > 0) // 2003版本
workbook = new HSSFWorkbook(fs);
#region 导入组织信息
ISheet sheet = workbook.GetSheetAt(0); //读取当前表数据
for (int j = 1; j <= sheet.LastRowNum; j++) //LastRowNum 是当前表的总行数
{
IRow row = sheet.GetRow(j); //读取当前行数据
if (row != null&&row.Cells.Count>0)
{
#region 导入单位信息
var code = row.Cells[0].StringCellValue.Trim();
var parentid = OThinker.H3.WorkSheet.AppUtility.Engine.Organization.Company.CompanyID;
var name = row.Cells[2].StringCellValue.Trim();
var unit = new OThinker.Organization.OrganizationUnit()
{
ObjectID = Guid.NewGuid().ToString(),
Code = code,
CompanyID = OThinker.H3.WorkSheet.AppUtility.Engine.Organization.Company.CompanyID,
Name = name,
ParentID = parentid,
// Sort Key
SortKey = j,
// 类型
CategoryID = null
};
// 写入服务器
var result = OThinker.Organization.HandleResult.SUCCESS;
if (OThinker.H3.WorkSheet.AppUtility.Engine.Organization.GetUnitByCode(code) != null)
{
result = OThinker.H3.WorkSheet.AppUtility.Engine.Organization.UpdateUnit(null, unit);
}
else
{
result = OThinker.H3.WorkSheet.AppUtility.Engine.Organization.AddUnit(null, unit);
}
if (result != OThinker.Organization.HandleResult.SUCCESS)
{
OThinker.H3.WorkSheet.AppUtility.Engine.LogWriter.Write(name);
}
#endregion
}
}
#endregion
#region 导入用户信息
sheet = workbook.GetSheetAt(1); //读取当前表数据
for (int j = 1; j <= sheet.LastRowNum; j++) //LastRowNum 是当前表的总行数
{
IRow row = sheet.GetRow(j); //读取当前行数据
if (row != null && row.Cells.Count > 0)
{
#region 导入单位信息
var oucode = row.Cells[0].StringCellValue.Trim();
var code = row.Cells[1].StringCellValue.Trim();
var parentou = OThinker.H3.WorkSheet.AppUtility.Engine.Organization.GetUnitByCode(oucode);
var name = row.Cells[2].StringCellValue.Trim();
var employeenumber = row.Cells[3].ToString().Trim().Replace(" ", "");
var birthday = row.Cells[4].DateCellValue;
var email = row.Cells[5].ToString().Trim();
var officemobile = row.Cells[6]==null?"": row.Cells[6].ToString().Trim();
var unit = new OThinker.Organization.User()
{
ObjectID = Guid.NewGuid().ToString(),
Code = code,
CompanyID = OThinker.H3.WorkSheet.AppUtility.Engine.Organization.Company.CompanyID,
Name = name,
ParentID = parentou == null ?
OThinker.H3.WorkSheet.AppUtility.Engine.Organization.Company.CompanyID :
parentou.ObjectID,
// Sort Key
SortKey = j,
// 类型
CategoryID = null,
EmployeeNumber = employeenumber,
OfficePhone = officemobile,
Email = email,
Birthday = birthday
};
// 写入服务器
OThinker.Organization.HandleResult result = OThinker.Organization.HandleResult.SUCCESS;
if (OThinker.H3.WorkSheet.AppUtility.Engine.Organization.GetUserByEmployeeNumber(code) != null)
{
result = OThinker.H3.WorkSheet.AppUtility.Engine.Organization.UpdateUnit(null, unit);
}
else
{
result = OThinker.H3.WorkSheet.AppUtility.Engine.Organization.AddUnit(null, unit);
}
if (result != OThinker.Organization.HandleResult.SUCCESS)
{
OThinker.H3.WorkSheet.AppUtility.Engine.LogWriter.Write(name);
}
#endregion
}
}
#endregion
File.Delete(upfile);
}
}
}
Excel整理模板