From 7270a1fc1c38a1d440342c942967e2e6b4f480b6 Mon Sep 17 00:00:00 2001 From: MaxKey Date: Wed, 17 Nov 2021 09:35:55 +0800 Subject: [PATCH] ExcelImport --- .../main/java/org/maxkey/util/ExcelUtils.java | 5 + .../contorller/OrganizationsController.java | 101 ++++---- .../web/contorller/UserInfoController.java | 216 +++++++++--------- 3 files changed, 161 insertions(+), 161 deletions(-) diff --git a/maxkey-common/src/main/java/org/maxkey/util/ExcelUtils.java b/maxkey-common/src/main/java/org/maxkey/util/ExcelUtils.java index ab2230a0e..85493018e 100644 --- a/maxkey-common/src/main/java/org/maxkey/util/ExcelUtils.java +++ b/maxkey-common/src/main/java/org/maxkey/util/ExcelUtils.java @@ -5,6 +5,7 @@ import java.text.SimpleDateFormat; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellType; +import org.apache.poi.ss.usermodel.Row; public class ExcelUtils { @@ -32,4 +33,8 @@ public class ExcelUtils { return String.valueOf(cell.getStringCellValue().trim()); } } + + public static String getValue(Row row,int i) { + return getValue(row.getCell(i)); + } } diff --git a/maxkey-webs/maxkey-web-mgt/src/main/java/org/maxkey/web/contorller/OrganizationsController.java b/maxkey-webs/maxkey-web-mgt/src/main/java/org/maxkey/web/contorller/OrganizationsController.java index 573cef7b5..3d306e963 100644 --- a/maxkey-webs/maxkey-web-mgt/src/main/java/org/maxkey/web/contorller/OrganizationsController.java +++ b/maxkey-webs/maxkey-web-mgt/src/main/java/org/maxkey/web/contorller/OrganizationsController.java @@ -184,7 +184,6 @@ public class OrganizationsController { public ModelAndView importing(@ModelAttribute("excelImportFile")ExcelImport excelImportFile) { if (excelImportFile.isExcelNotEmpty() ) { try { - int columnSize = 46; List orgsList = Lists.newArrayList(); Workbook workbook = excelImportFile.biuldWorkbook(); int sheetSize = workbook.getNumberOfSheets(); @@ -197,56 +196,7 @@ public class OrganizationsController { if (row == null || j <3 ) {//略过空行和前3行 continue; } else {//其他行是数据行 - Organizations organization =new Organizations(); - for (int k = 0; k < columnSize; k++) { - if (k == 0) {// 上级编码 - organization.setParentId(ExcelUtils.getValue(row.getCell(k))); - } else if (k == 1) {// 上级名称 - organization.setParentName(ExcelUtils.getValue(row.getCell(k))); - } else if (k == 2) {// 组织编码 - organization.setId(ExcelUtils.getValue(row.getCell(k))); - } else if (k == 3) {// 组织名称 - organization.setName(ExcelUtils.getValue(row.getCell(k))); - } else if (k == 4) {// 组织全称 - organization.setFullName(ExcelUtils.getValue(row.getCell(k))); - } else if (k == 5) {// 编码路径 - organization.setCodePath(ExcelUtils.getValue(row.getCell(k))); - } else if (k == 6) {// 名称路径 - organization.setNamePath(ExcelUtils.getValue(row.getCell(k))); - } else if (k == 7) { // 组织类型 - organization.setType(ExcelUtils.getValue(row.getCell(k))); - } else if (k == 8) {// 所属分支机构 - organization.setDivision(ExcelUtils.getValue(row.getCell(k))); - } else if (k == 9) {// 级别 - String level=ExcelUtils.getValue(row.getCell(k)); - organization.setLevel(level.equals("") ? "1" : level); - } else if (k == 10) {// 排序 - String sortIndex=ExcelUtils.getValue(row.getCell(k)); - organization.setSortIndex(sortIndex.equals("") ? 1 : Integer.parseInt(sortIndex)); - } else if (k == 11) {// 联系人 - organization.setContact(ExcelUtils.getValue(row.getCell(k))); - } else if (k == 12) {// 联系电话 - organization.setPhone(ExcelUtils.getValue(row.getCell(k))); - }else if (k == 13) {// 邮箱 - organization.setEmail(ExcelUtils.getValue(row.getCell(k))); - }else if (k == 14) {// 传真 - organization.setFax(ExcelUtils.getValue(row.getCell(k))); - }else if (k == 24) {// 工作-国家 - organization.setCountry(ExcelUtils.getValue(row.getCell(k))); - }else if (k == 25) {// 工作-省 - organization.setRegion(ExcelUtils.getValue(row.getCell(k))); - }else if (k == 26) {// 工作-城市 - organization.setLocality(ExcelUtils.getValue(row.getCell(k))); - }else if (k == 27) {// 工作-地址 - organization.setLocality(ExcelUtils.getValue(row.getCell(k))); - }else if (k == 28) {// 邮编 - organization.setPostalCode(ExcelUtils.getValue(row.getCell(k))); - }else if (k == 29) {// 详细描述 - organization.setDescription(ExcelUtils.getValue(row.getCell(k))); - } - } - organization.setStatus(1); - orgsList.add(organization); + orgsList.add(buildOrganizationsFromSheetRow(row)); } } } @@ -271,4 +221,53 @@ public class OrganizationsController { return new ModelAndView("/orgs/orgsImport"); } + public Organizations buildOrganizationsFromSheetRow(Row row) { + Organizations organization = new Organizations(); + // 上级编码 + organization.setParentId(ExcelUtils.getValue(row, 0)); + // 上级名称 + organization.setParentName(ExcelUtils.getValue(row, 1)); + // 组织编码 + organization.setId(ExcelUtils.getValue(row, 2)); + // 组织名称 + organization.setName(ExcelUtils.getValue(row, 3)); + // 组织全称 + organization.setFullName(ExcelUtils.getValue(row, 4)); + // 编码路径 + organization.setCodePath(ExcelUtils.getValue(row, 5)); + // 名称路径 + organization.setNamePath(ExcelUtils.getValue(row, 6)); + // 组织类型 + organization.setType(ExcelUtils.getValue(row, 7)); + // 所属分支机构 + organization.setDivision(ExcelUtils.getValue(row, 8)); + // 级别 + String level = ExcelUtils.getValue(row, 9); + organization.setLevel(level.equals("") ? "1" : level); + // 排序 + String sortIndex = ExcelUtils.getValue(row, 10); + organization.setSortIndex(sortIndex.equals("") ? 1 : Integer.parseInt(sortIndex)); + // 联系人 + organization.setContact(ExcelUtils.getValue(row, 11)); + // 联系电话 + organization.setPhone(ExcelUtils.getValue(row, 12)); + // 邮箱 + organization.setEmail(ExcelUtils.getValue(row, 13)); + // 传真 + organization.setFax(ExcelUtils.getValue(row, 14)); + // 工作-国家 + organization.setCountry(ExcelUtils.getValue(row, 15)); + // 工作-省 + organization.setRegion(ExcelUtils.getValue(row, 16)); + // 工作-城市 + organization.setLocality(ExcelUtils.getValue(row, 17)); + // 工作-地址 + organization.setLocality(ExcelUtils.getValue(row, 18)); + // 邮编 + organization.setPostalCode(ExcelUtils.getValue(row, 19)); + // 详细描述 + organization.setDescription(ExcelUtils.getValue(row, 20)); + organization.setStatus(1); + return organization; + } } diff --git a/maxkey-webs/maxkey-web-mgt/src/main/java/org/maxkey/web/contorller/UserInfoController.java b/maxkey-webs/maxkey-web-mgt/src/main/java/org/maxkey/web/contorller/UserInfoController.java index 82a99a6a6..0734e48b8 100644 --- a/maxkey-webs/maxkey-web-mgt/src/main/java/org/maxkey/web/contorller/UserInfoController.java +++ b/maxkey-webs/maxkey-web-mgt/src/main/java/org/maxkey/web/contorller/UserInfoController.java @@ -32,7 +32,6 @@ import java.util.stream.Collectors; import javax.validation.Valid; import org.apache.mybatis.jpa.persistence.JpaPageResults; -import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; @@ -289,13 +288,11 @@ public class UserInfoController { public ModelAndView importing(@ModelAttribute("excelImportFile")ExcelImport excelImportFile) { if (excelImportFile.isExcelNotEmpty() ) { try { - int columnSize = 46; List userInfoList = Lists.newArrayList(); Workbook workbook = excelImportFile.biuldWorkbook(); int recordCount = 0; int sheetSize = workbook.getNumberOfSheets(); - //遍历sheet页 - for (int i = 0; i < sheetSize; i++) { + for (int i = 0; i < sheetSize; i++) {//遍历sheet页 Sheet sheet = workbook.getSheetAt(i); int rowSize = sheet.getLastRowNum() + 1; for (int j = 1; j < rowSize; j++) {//遍历行 @@ -303,112 +300,8 @@ public class UserInfoController { if (row == null || j <3 ) {//略过空行和前3行 continue; } else {//其他行是数据行 - UserInfo userInfo = new UserInfo(); - userInfo.setCreatedDate(DateUtils.formatDateTime(new Date())); - - for (int k = 0; k < columnSize; k++) { - Cell cell = row.getCell(k); - if (k == 0) {// 登录账号 - userInfo.setUsername(ExcelUtils.getValue(cell)); - } else if (k == 1) {// 密码 - userInfo.setPassword(ExcelUtils.getValue(cell)); - } else if (k == 2) {// 用户显示 - userInfo.setDisplayName(ExcelUtils.getValue(cell)); - } else if (k == 3) {// 姓 - userInfo.setFamilyName(ExcelUtils.getValue(cell)); - } else if (k == 4) {// 名 - userInfo.setGivenName(ExcelUtils.getValue(cell)); - } else if (k == 5) {// 中间名 - userInfo.setMiddleName(ExcelUtils.getValue(cell)); - } else if (k == 6) {// 昵称 - userInfo.setNickName(ExcelUtils.getValue(cell)); - } else if (k == 7) {// 性别 - String gender = ExcelUtils.getValue(cell); - userInfo.setGender(gender.equals("")? 1 : Integer.valueOf(ExcelUtils.getValue(cell))); - } else if (k == 8) {// 语言偏好 - userInfo.setPreferredLanguage(ExcelUtils.getValue(cell)); - } else if (k == 9) {// 时区 - userInfo.setTimeZone(ExcelUtils.getValue(cell)); - } else if (k == 10) {// 用户类型 - userInfo.setUserType(ExcelUtils.getValue(cell)); - } else if (k == 11) {// 员工编码 - userInfo.setEmployeeNumber(ExcelUtils.getValue(cell)); - } else if (k == 12) {// AD域账号 - userInfo.setWindowsAccount(ExcelUtils.getValue(cell)); - }else if (k == 13) {// 所属机构 - userInfo.setOrganization(ExcelUtils.getValue(cell)); - }else if (k == 14) {// 分支机构 - userInfo.setDivision(ExcelUtils.getValue(cell)); - }else if (k == 15) {// 部门编号 - userInfo.setDepartmentId(ExcelUtils.getValue(cell)); - }else if (k == 16) {// 部门名称 - userInfo.setDepartment(ExcelUtils.getValue(cell)); - }else if (k == 17) {// 成本中心 - userInfo.setCostCenter(ExcelUtils.getValue(cell)); - }else if (k == 18) {// 职位 - userInfo.setJobTitle(ExcelUtils.getValue(cell)); - }else if (k == 19) { // 级别 - userInfo.setJobLevel(ExcelUtils.getValue(cell)); - }else if (k == 20) {// 上级经理 - userInfo.setManager(ExcelUtils.getValue(cell)); - }else if (k == 21) {// 助理 - userInfo.setAssistant(ExcelUtils.getValue(cell)); - }else if (k == 22) {// 入职时间 - userInfo.setEntryDate(ExcelUtils.getValue(cell)); - }else if (k == 23) { - // 离职时间 - userInfo.setQuitDate(ExcelUtils.getValue(cell)); - }else if (k == 24) {// 工作-国家 - userInfo.setWorkCountry(ExcelUtils.getValue(cell)); - }else if (k == 25) {// 工作-省 - userInfo.setWorkRegion(ExcelUtils.getValue(cell)); - }else if (k == 26) {// 工作-城市 - userInfo.setTimeZone(ExcelUtils.getValue(cell)); - }else if (k == 27) {// 工作-地址 - userInfo.setWorkLocality(ExcelUtils.getValue(cell)); - }else if (k == 28) {// 邮编 - userInfo.setWorkPostalCode(ExcelUtils.getValue(cell)); - }else if (k == 29) {// 传真 - userInfo.setWorkFax(ExcelUtils.getValue(cell)); - }else if (k == 30) {// 工作电话 - userInfo.setWorkPhoneNumber(ExcelUtils.getValue(cell)); - }else if (k == 31) {// 工作邮件 - userInfo.setWorkEmail(ExcelUtils.getValue(cell)); - }else if (k == 32) {// 证件类型 todo 现在数据库中存储的是tinyint -// userInfo.setIdType(ExcelUtils.getValue(cell)); - }else if (k == 33) {// 证件号码 - userInfo.setIdCardNo(ExcelUtils.getValue(cell)); - } else if (k == 34) { - // 出生日期 - userInfo.setBirthDate(ExcelUtils.getValue(cell)); - }else if (k == 35) {// 婚姻状态 todo 现在数据字段类型是 tinyint -// userInfo.setMarried(ExcelUtils.getValue(cell)); - }else if (k == 36) {// 开始工作时间 - userInfo.setStartWorkDate(ExcelUtils.getValue(cell)); - }else if (k == 37) {// 个人主页 - userInfo.setWebSite(ExcelUtils.getValue(cell)); - }else if (k == 38) {// 即时通讯 - userInfo.setDefineIm(ExcelUtils.getValue(cell)); - }else if (k == 39) {// 国家 - userInfo.setHomeCountry(ExcelUtils.getValue(cell)); - }else if (k == 40) {// 省 - userInfo.setHomeRegion(ExcelUtils.getValue(cell)); - }else if (k == 41) {// 城市 - userInfo.setHomeLocality(ExcelUtils.getValue(cell)); - }else if (k == 42) {// 家庭地址 - userInfo.setHomeStreetAddress(ExcelUtils.getValue(cell)); - }else if (k == 43) {// 家庭邮编 - userInfo.setHomePostalCode(ExcelUtils.getValue(cell)); - }else if (k == 44) {// 家庭传真 - userInfo.setHomeFax(ExcelUtils.getValue(cell)); - }else if (k == 45) {// 家庭电话 - userInfo.setHomePhoneNumber(ExcelUtils.getValue(cell)); - }else if (k == 46) {// 家庭邮箱 - userInfo.setHomeEmail(ExcelUtils.getValue(cell)); - } - } - userInfo.setStatus(1); - userInfoList.add(userInfoService.passwordEncoder(userInfo)); + UserInfo userInfo = buildUserFromSheetRow(row); + userInfoList.add(userInfo); recordCount ++; _logger.debug("record {} user {} account {}",recordCount,userInfo.getDisplayName(),userInfo.getUsername()); } @@ -454,5 +347,108 @@ public class UserInfoController { binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true)); } + + public UserInfo buildUserFromSheetRow(Row row) { + UserInfo userInfo = new UserInfo(); + userInfo.setCreatedDate(DateUtils.formatDateTime(new Date())); + // 登录账号 + userInfo.setUsername(ExcelUtils.getValue(row, 0)); + // 密码 + userInfo.setPassword(ExcelUtils.getValue(row, 1)); + // 用户显示 + userInfo.setDisplayName(ExcelUtils.getValue(row, 2)); + // 姓 + userInfo.setFamilyName(ExcelUtils.getValue(row, 3)); + // 名 + userInfo.setGivenName(ExcelUtils.getValue(row, 4)); + // 中间名 + userInfo.setMiddleName(ExcelUtils.getValue(row, 5)); + // 昵称 + userInfo.setNickName(ExcelUtils.getValue(row, 6)); + // 性别 + String gender = ExcelUtils.getValue(row, 7); + userInfo.setGender(gender.equals("") ? 1 : Integer.valueOf(gender)); + // 语言偏好 + userInfo.setPreferredLanguage(ExcelUtils.getValue(row, 8)); + // 时区 + userInfo.setTimeZone(ExcelUtils.getValue(row, 9)); + // 用户类型 + userInfo.setUserType(ExcelUtils.getValue(row, 10)); + // 员工编码 + userInfo.setEmployeeNumber(ExcelUtils.getValue(row, 11)); + // AD域账号 + userInfo.setWindowsAccount(ExcelUtils.getValue(row, 12)); + // 所属机构 + userInfo.setOrganization(ExcelUtils.getValue(row, 13)); + // 分支机构 + userInfo.setDivision(ExcelUtils.getValue(row, 14)); + // 部门编号 + userInfo.setDepartmentId(ExcelUtils.getValue(row, 15)); + // 部门名称 + userInfo.setDepartment(ExcelUtils.getValue(row, 16)); + // 成本中心 + userInfo.setCostCenter(ExcelUtils.getValue(row, 17)); + // 职位 + userInfo.setJobTitle(ExcelUtils.getValue(row, 18)); + // 级别 + userInfo.setJobLevel(ExcelUtils.getValue(row, 19)); + // 上级经理 + userInfo.setManager(ExcelUtils.getValue(row, 20)); + // 助理 + userInfo.setAssistant(ExcelUtils.getValue(row, 21)); + // 入职时间 + userInfo.setEntryDate(ExcelUtils.getValue(row, 22)); + // 离职时间 + userInfo.setQuitDate(ExcelUtils.getValue(row, 23)); + // 工作-国家 + userInfo.setWorkCountry(ExcelUtils.getValue(row, 24)); + // 工作-省 + userInfo.setWorkRegion(ExcelUtils.getValue(row, 25)); + // 工作-城市 + userInfo.setTimeZone(ExcelUtils.getValue(row, 26)); + // 工作-地址 + userInfo.setWorkLocality(ExcelUtils.getValue(row, 27)); + // 邮编 + userInfo.setWorkPostalCode(ExcelUtils.getValue(row, 28)); + // 传真 + userInfo.setWorkFax(ExcelUtils.getValue(row, 29)); + // 工作电话 + userInfo.setWorkPhoneNumber(ExcelUtils.getValue(row, 30)); + // 工作邮件 + userInfo.setWorkEmail(ExcelUtils.getValue(row, 31)); + // 证件类型 todo 现在数据库中存储的是tinyint +// userInfo.setIdType(ExcelUtils.getValue(row, 32)); + // 证件号码 + userInfo.setIdCardNo(ExcelUtils.getValue(row, 33)); + // 出生日期 + userInfo.setBirthDate(ExcelUtils.getValue(row, 34)); + // 婚姻状态 todo 现在数据字段类型是 tinyint +// userInfo.setMarried(ExcelUtils.getValue(row, 35)); + // 开始工作时间 + userInfo.setStartWorkDate(ExcelUtils.getValue(row, 36)); + // 个人主页 + userInfo.setWebSite(ExcelUtils.getValue(row, 37)); + // 即时通讯 + userInfo.setDefineIm(ExcelUtils.getValue(row, 38)); + // 国家 + userInfo.setHomeCountry(ExcelUtils.getValue(row, 39)); + // 省 + userInfo.setHomeRegion(ExcelUtils.getValue(row, 40)); + // 城市 + userInfo.setHomeLocality(ExcelUtils.getValue(row, 41)); + // 家庭地址 + userInfo.setHomeStreetAddress(ExcelUtils.getValue(row, 42)); + // 家庭邮编 + userInfo.setHomePostalCode(ExcelUtils.getValue(row, 43)); + // 家庭传真 + userInfo.setHomeFax(ExcelUtils.getValue(row, 44)); + // 家庭电话 + userInfo.setHomePhoneNumber(ExcelUtils.getValue(row, 45)); + // 家庭邮箱 + userInfo.setHomeEmail(ExcelUtils.getValue(row, 46)); + userInfoService.passwordEncoder(userInfo); + userInfo.setStatus(1); + return userInfo; + } }