excel 数据导入(附工具类)
生活随笔
收集整理的這篇文章主要介紹了
excel 数据导入(附工具类)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
// 導(dǎo)入excel中的數(shù)據(jù)到數(shù)據(jù)庫中@PostMapping(value = "importHDangerRecord")@Overridepublic RestMessage importHDangerRecord(@RequestParam("file") MultipartFile file) {try {// 隱患分類 隱患級別 隱患描述 報告時間 隱患報告人String qyid = UserUtil.getCurrQyId();
// String userid = UserUtil.getCurrUserId();
// SeUser user = userFeign.getUserById(userid).getData();SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");StringBuilder errorMsg = new StringBuilder("");List<Hiddendanger> res = new ArrayList<>();Map<Integer, String> map = ImportExcelUtil.getDataFromExcel(file);if(map.size()>1){for (Map.Entry<Integer, String> entry : map.entrySet()) {if (entry.getKey() > 1) {Integer row = entry.getKey()+1;String[] s = entry.getValue().split("#&");Hiddendanger hiddendanger = new Hiddendanger();//企業(yè)idTbQybaseinfo qyInfo = userFeign.getQyByUserId(UserUtil.getCurrUserId()).getData();
// hiddendanger.beforeInsert();
// hiddendanger.setQyid(qyInfo.getId());
// hiddendanger.setQyname(qyInfo.getName());
// hiddendanger.setZygsid(qyInfo.getZygsid());
// hiddendanger.setSybid(qyInfo.getSybid());//隱患分類if(StringUtils.isNoneBlank(s[0]) && StringUtils.isNoneBlank(s[1])) {Thirtytwoelements firstCategory = hDangerService.getFirstCategory(s[0]);if(firstCategory != null && StringUtils.isNoneBlank(firstCategory.getId())){Thirtytwoelements secondCategoryCategory = hDangerService.getSecondCategory(s[1],firstCategory.getId());if(secondCategoryCategory != null){hiddendanger.setCategory(secondCategoryCategory.getId());hiddendanger.setCatecode(secondCategoryCategory.getCode());hiddendanger.setCategoryname(secondCategoryCategory.getName());}else{errorMsg.append("第" + row + "行隱患分類有誤 \n");}}else{errorMsg.append("第" + row + "行隱患分類有誤 \n");}}else {errorMsg.append("第" + row + "行隱患分類為空 \n");}//隱患級別if(StringUtils.isNoneBlank(s[2])) {RestMessage<TbDicItem> rm = userFeign.getItemByNameFieldCode(s[2], "DANGER_YHDJ");TbDicItem item = rm.getData();if(item != null){hiddendanger.setLevel(item.getId());hiddendanger.setLevelcode(item.getCode());hiddendanger.setLevelname(item.getName());}else{errorMsg.append("第" + row + "行隱患等級有誤 \n");}}else {errorMsg.append("第" + row + "行隱患等級為空 \n");}//隱患描述if(StringUtils.isNoneBlank(s[3])) {hiddendanger.setDescribe(s[3]);}else {errorMsg.append("第" + row + "行隱患描述為空 \n");}//隱患報告時間if(StringUtils.isNoneBlank(s[4])) {if(isValidDate(s[4])){hiddendanger.setReporttime(s[4]);}else {errorMsg.append("第" + row + "行隱患報告時間格式有誤 請按照 \" yyyy-MM-dd\" 格式填寫 \n");}}else {errorMsg.append("第" + row + "行隱患報告時間為空 \n");}//隱患報告人
// hiddendanger.setReportUsername(user.getRealname());
// hiddendanger.setReportUser(userid);if(StringUtils.isNoneBlank(s[5])) {SeUser seUser = userFeign.getUserByEmail(s[5]).getData();if(seUser != null){hiddendanger.setReportUser(seUser.getId());hiddendanger.setReportUsername(seUser.getRealname());TbQybaseinfo qybaseinfo = userFeign.getQyById(seUser.getQyId()).getData();if(qybaseinfo.getHierarchy() == ConstantCode.DW){hiddendanger.setHiddenSource(1);hiddendanger.setQyid(qybaseinfo.getId());hiddendanger.setQyname(qybaseinfo.getName());hiddendanger.setZygsid(qybaseinfo.getZygsid());hiddendanger.setSybid(qybaseinfo.getSybid());hiddendanger.setReportlevel(1);}else if(qybaseinfo.getHierarchy() == ConstantCode.ZYGS){hiddendanger.setHiddenSource(2);hiddendanger.setReportlevel(2);hiddendanger.setQyid(qybaseinfo.getId());hiddendanger.setQyname(qybaseinfo.getName());if(StringUtils.isNoneBlank(qybaseinfo.getZygsid())){hiddendanger.setZygsid(qybaseinfo.getZygsid());}else {hiddendanger.setZygsid(qybaseinfo.getId());}hiddendanger.setSybid(qybaseinfo.getSybid());}}else {errorMsg.append("第" + row + "行隱患報告人郵箱參數(shù)有誤 \n");}}else {errorMsg.append("第" + row + "行隱患報告人郵箱為空 \n");}res.add(hiddendanger);}}}else{errorMsg.append("導(dǎo)入模板為空 \n");}if (errorMsg.length() <= 0) {for (Hiddendanger hiddendanger: res) {hiddendanger.beforeInsert();hDangerService.addHDanger(hiddendanger);}return new RestMessage();} else {return new RestMessage(0, errorMsg.toString());}}catch (Exception e) {e.printStackTrace();return new RestMessage(RespCodeAndMsg.FAIL);}}
/**
*
*/
package com.zhjt.utils;
總結(jié)
以上是生活随笔為你收集整理的excel 数据导入(附工具类)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 后台excel导出(单sheet 多s
- 下一篇: sqlserver yml配置文件