Search in sources :

Example 1 with ImportParams

use of org.jeecgframework.poi.excel.entity.ImportParams in project jeecg-boot by jeecgboot.

the class QuartzJobController method importExcel.

/**
 * 通过excel导入数据
 *
 * @param request
 * @param response
 * @return
 */
@RequestMapping(value = "/importExcel", method = RequestMethod.POST)
public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) throws IOException {
    MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
    Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
    // 错误信息
    List<String> errorMessage = new ArrayList<>();
    int successLines = 0, errorLines = 0;
    for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
        // 获取上传文件对象
        MultipartFile file = entity.getValue();
        ImportParams params = new ImportParams();
        params.setTitleRows(2);
        params.setHeadRows(1);
        params.setNeedSave(true);
        try {
            List<QuartzJob> listQuartzJobs = ExcelImportUtil.importExcel(file.getInputStream(), QuartzJob.class, params);
            // add-begin-author:taoyan date:20210909 for:导入定时任务,并不会被启动和调度,需要手动点击启动,才会加入调度任务中 #2986
            for (QuartzJob job : listQuartzJobs) {
                job.setStatus(CommonConstant.STATUS_DISABLE);
            }
            List<String> list = ImportExcelUtil.importDateSave(listQuartzJobs, IQuartzJobService.class, errorMessage, CommonConstant.SQL_INDEX_UNIQ_JOB_CLASS_NAME);
            // add-end-author:taoyan date:20210909 for:导入定时任务,并不会被启动和调度,需要手动点击启动,才会加入调度任务中 #2986
            errorLines += list.size();
            successLines += (listQuartzJobs.size() - errorLines);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Result.error("文件导入失败!");
        } finally {
            try {
                file.getInputStream().close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return ImportExcelUtil.imporReturnRes(errorLines, successLines, errorMessage);
}
Also used : ArrayList(java.util.ArrayList) IOException(java.io.IOException) SchedulerException(org.quartz.SchedulerException) IOException(java.io.IOException) MultipartFile(org.springframework.web.multipart.MultipartFile) QuartzJob(org.jeecg.modules.quartz.entity.QuartzJob) ImportParams(org.jeecgframework.poi.excel.entity.ImportParams) Map(java.util.Map) MultipartHttpServletRequest(org.springframework.web.multipart.MultipartHttpServletRequest)

Example 2 with ImportParams

use of org.jeecgframework.poi.excel.entity.ImportParams in project jeecg-boot by jeecgboot.

the class JoaDemoController method importExcel.

/**
 * 通过excel导入数据
 *
 * @param request
 * @param response
 * @return
 */
@RequestMapping(value = "/importExcel", method = RequestMethod.POST)
public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
    MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
    Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
    for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
        // 获取上传文件对象
        MultipartFile file = entity.getValue();
        ImportParams params = new ImportParams();
        params.setTitleRows(2);
        params.setHeadRows(1);
        params.setNeedSave(true);
        try {
            List<JoaDemo> listJoaDemos = ExcelImportUtil.importExcel(file.getInputStream(), JoaDemo.class, params);
            for (JoaDemo joaDemoExcel : listJoaDemos) {
                joaDemoService.save(joaDemoExcel);
            }
            return Result.ok("文件导入成功!数据行数:" + listJoaDemos.size());
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Result.error("文件导入失败:" + e.getMessage());
        } finally {
            try {
                file.getInputStream().close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return Result.ok("文件导入失败!");
}
Also used : JoaDemo(org.jeecg.modules.demo.test.entity.JoaDemo) MultipartFile(org.springframework.web.multipart.MultipartFile) ImportParams(org.jeecgframework.poi.excel.entity.ImportParams) IOException(java.io.IOException) Map(java.util.Map) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) MultipartHttpServletRequest(org.springframework.web.multipart.MultipartHttpServletRequest) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 3 with ImportParams

use of org.jeecgframework.poi.excel.entity.ImportParams in project jeecg-boot by jeecgboot.

the class SysPositionController method importExcel.

/**
 * 通过excel导入数据
 *
 * @param request
 * @param response
 * @return
 */
@RequestMapping(value = "/importExcel", method = RequestMethod.POST)
public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) throws IOException {
    MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
    Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
    // 错误信息
    List<String> errorMessage = new ArrayList<>();
    int successLines = 0, errorLines = 0;
    for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
        // 获取上传文件对象
        MultipartFile file = entity.getValue();
        ImportParams params = new ImportParams();
        params.setTitleRows(2);
        params.setHeadRows(1);
        params.setNeedSave(true);
        try {
            List<Object> listSysPositions = ExcelImportUtil.importExcel(file.getInputStream(), SysPosition.class, params);
            List<String> list = ImportExcelUtil.importDateSave(listSysPositions, ISysPositionService.class, errorMessage, CommonConstant.SQL_INDEX_UNIQ_CODE);
            errorLines += list.size();
            successLines += (listSysPositions.size() - errorLines);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Result.error("文件导入失败:" + e.getMessage());
        } finally {
            try {
                file.getInputStream().close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return ImportExcelUtil.imporReturnRes(errorLines, successLines, errorMessage);
}
Also used : ArrayList(java.util.ArrayList) IOException(java.io.IOException) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) MultipartFile(org.springframework.web.multipart.MultipartFile) ImportParams(org.jeecgframework.poi.excel.entity.ImportParams) Map(java.util.Map) MultipartHttpServletRequest(org.springframework.web.multipart.MultipartHttpServletRequest)

Example 4 with ImportParams

use of org.jeecgframework.poi.excel.entity.ImportParams in project jeecg-boot by jeecgboot.

the class SysRoleController method importExcel.

/**
 * 通过excel导入数据
 * @param request
 * @param response
 * @return
 */
@RequestMapping(value = "/importExcel", method = RequestMethod.POST)
public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
    MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
    Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
    for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
        // 获取上传文件对象
        MultipartFile file = entity.getValue();
        ImportParams params = new ImportParams();
        params.setTitleRows(2);
        params.setHeadRows(1);
        params.setNeedSave(true);
        try {
            return sysRoleService.importExcelCheckRoleCode(file, params);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Result.error("文件导入失败:" + e.getMessage());
        } finally {
            try {
                file.getInputStream().close();
            } catch (IOException e) {
                log.error(e.getMessage(), e);
            }
        }
    }
    return Result.error("文件导入失败!");
}
Also used : MultipartFile(org.springframework.web.multipart.MultipartFile) ImportParams(org.jeecgframework.poi.excel.entity.ImportParams) IOException(java.io.IOException) Map(java.util.Map) HashMap(java.util.HashMap) IOException(java.io.IOException) MultipartHttpServletRequest(org.springframework.web.multipart.MultipartHttpServletRequest) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 5 with ImportParams

use of org.jeecgframework.poi.excel.entity.ImportParams in project jeecg-boot by jeecgboot.

the class SysUserController method importExcel.

/**
 * 通过excel导入数据
 *
 * @param request
 * @param response
 * @return
 */
// @RequiresRoles({"admin"})
// @RequiresPermissions("user:import")
@RequestMapping(value = "/importExcel", method = RequestMethod.POST)
public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) throws IOException {
    MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
    Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
    // 错误信息
    List<String> errorMessage = new ArrayList<>();
    int successLines = 0, errorLines = 0;
    for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
        // 获取上传文件对象
        MultipartFile file = entity.getValue();
        ImportParams params = new ImportParams();
        params.setTitleRows(2);
        params.setHeadRows(1);
        params.setNeedSave(true);
        try {
            List<SysUser> listSysUsers = ExcelImportUtil.importExcel(file.getInputStream(), SysUser.class, params);
            for (int i = 0; i < listSysUsers.size(); i++) {
                SysUser sysUserExcel = listSysUsers.get(i);
                if (StringUtils.isBlank(sysUserExcel.getPassword())) {
                    // 密码默认为 “123456”
                    sysUserExcel.setPassword("123456");
                }
                // 密码加密加盐
                String salt = oConvertUtils.randomGen(8);
                sysUserExcel.setSalt(salt);
                String passwordEncode = PasswordUtil.encrypt(sysUserExcel.getUsername(), sysUserExcel.getPassword(), salt);
                sysUserExcel.setPassword(passwordEncode);
                try {
                    sysUserService.save(sysUserExcel);
                    successLines++;
                } catch (Exception e) {
                    errorLines++;
                    String message = e.getMessage().toLowerCase();
                    int lineNumber = i + 1;
                    // 通过索引名判断出错信息
                    if (message.contains(CommonConstant.SQL_INDEX_UNIQ_SYS_USER_USERNAME)) {
                        errorMessage.add("第 " + lineNumber + " 行:用户名已经存在,忽略导入。");
                    } else if (message.contains(CommonConstant.SQL_INDEX_UNIQ_SYS_USER_WORK_NO)) {
                        errorMessage.add("第 " + lineNumber + " 行:工号已经存在,忽略导入。");
                    } else if (message.contains(CommonConstant.SQL_INDEX_UNIQ_SYS_USER_PHONE)) {
                        errorMessage.add("第 " + lineNumber + " 行:手机号已经存在,忽略导入。");
                    } else if (message.contains(CommonConstant.SQL_INDEX_UNIQ_SYS_USER_EMAIL)) {
                        errorMessage.add("第 " + lineNumber + " 行:电子邮件已经存在,忽略导入。");
                    } else if (message.contains(CommonConstant.SQL_INDEX_UNIQ_SYS_USER)) {
                        errorMessage.add("第 " + lineNumber + " 行:违反表唯一性约束。");
                    } else {
                        errorMessage.add("第 " + lineNumber + " 行:未知错误,忽略导入");
                        log.error(e.getMessage(), e);
                    }
                }
                // 批量将部门和用户信息建立关联关系
                String departIds = sysUserExcel.getDepartIds();
                if (StringUtils.isNotBlank(departIds)) {
                    String userId = sysUserExcel.getId();
                    String[] departIdArray = departIds.split(",");
                    List<SysUserDepart> userDepartList = new ArrayList<>(departIdArray.length);
                    for (String departId : departIdArray) {
                        userDepartList.add(new SysUserDepart(userId, departId));
                    }
                    sysUserDepartService.saveBatch(userDepartList);
                }
            }
        } catch (Exception e) {
            errorMessage.add("发生异常:" + e.getMessage());
            log.error(e.getMessage(), e);
        } finally {
            try {
                file.getInputStream().close();
            } catch (IOException e) {
                log.error(e.getMessage(), e);
            }
        }
    }
    return ImportExcelUtil.imporReturnRes(errorLines, successLines, errorMessage);
}
Also used : IOException(java.io.IOException) IOException(java.io.IOException) MultipartFile(org.springframework.web.multipart.MultipartFile) ImportParams(org.jeecgframework.poi.excel.entity.ImportParams) MultipartHttpServletRequest(org.springframework.web.multipart.MultipartHttpServletRequest)

Aggregations

ImportParams (org.jeecgframework.poi.excel.entity.ImportParams)32 MultipartFile (org.springframework.web.multipart.MultipartFile)32 MultipartHttpServletRequest (org.springframework.web.multipart.MultipartHttpServletRequest)32 IOException (java.io.IOException)28 Map (java.util.Map)17 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)8 UnsupportedEncodingException (java.io.UnsupportedEncodingException)7 ArrayList (java.util.ArrayList)6 HashMap (java.util.HashMap)6 LambdaQueryWrapper (com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper)3 QueryWrapper (com.baomidou.mybatisplus.core.conditions.query.QueryWrapper)3 SysAnnouncement (org.jeecg.modules.system.entity.SysAnnouncement)3 SysCategory (org.jeecg.modules.system.entity.SysCategory)3 SysDepart (org.jeecg.modules.system.entity.SysDepart)3 SysDict (org.jeecg.modules.system.entity.SysDict)3 SysUserAgent (org.jeecg.modules.system.entity.SysUserAgent)3 SysDictPage (org.jeecg.modules.system.vo.SysDictPage)3 SchedulerException (org.quartz.SchedulerException)3 CacheEvict (org.springframework.cache.annotation.CacheEvict)3 JeecgOrderMain (org.jeecg.modules.demo.test.entity.JeecgOrderMain)1