JAVA若依框架数据导入导出

后端controller

根据自己的实体类下载Excel文件,在实体类中添加注释

@Excel(name="")

App 是自己的实体类类名

    /**
     * 导入住宿地点
     * 
     */

    @Log(title = "app", businessType = BusinessType.IMPORT) 
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<App> util = new ExcelUtil<>(App.class); 
        List<App> ac = util.importExcel(file.getInputStream()); 
        String operName = getUsername();
        String message = appService.importUser(ac, updateSupport, operName); // todo
        return AjaxResult.success(message);
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<App> util = new ExcelUtil<>(App.class); // todo
        util.importTemplateExcel(response, "地点");
    }

后端Service层

  /**
     * 导入数据
     *
     * @param isUpdateSupport 是否更新支持,如果已存在,则进行更新数据
     * @param operName 操作用户
     * @return 结果
     */
    public String importUser(List<App> AC, Boolean isUpdateSupport, String operName);

后端Impl

 /**
     * 导入信息
     *
     * @param
     * @return 结果
     */

    @Override
    public String importUser(List<App> AC, Boolean isUpdateSupport, String operName) {
        if (StringUtils.isNull(AC) || AC.size() == 0){
            throw new ServiceException("导入信息不能为空");
        }
        int successNum = 0;
        int failureNum = 0;
        Long n = 2023L;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for(AppCanteen apps: AC) {
            try {
                    String abc = app.getappName(); //拿到唯一的名称
                    appac = appMapper.selectAppCC(abc);
                    if (StringUtils.isNull(ac)) {
                        BeanValidators.validateWithException(validator, canteen);
                        apps.setCreateBy(operName);
                        apps.setPlayYear(n);
                        this.insertApp(apps);
                        successNum++;
                        successMsg.append("<br/>" + successNum + "、地点 " + app.getappName() + " 导入成功");
                    } else if (isUpdateSupport) {
                        BeanValidators.validateWithException(validator, canteen);
                        canteen.setCreateBy(operName);
                        this.updateAppCanteen(canteen);
                        successNum++;
                        successMsg.append("<br/>" + successNum + "、地点 " + app.getAppName() + " 更新成功");
                    } else {
                        failureNum++;
                        failureMsg.append("<br/>" + failureNum + "、地点 " + app.getAppName() + " 已存在");
                    }

            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + "、地点 " + app.getAppName() + " 导入失败:";
                failureMsg.append(msg);
            }
        }
        if (failureNum > 0){
            failureMsg.insert(0,"很抱歉导入失败 共" + failureNum +"条格式不对,错误如下");
            throw new ServiceException(failureMsg.toString());

        }else {
            successMsg.insert(0,"共有"+ successNum + "导入成功");
        }
        return  successMsg.toString();
    }

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
若依框架提供了方便的主子表数据导入导出功能。具体步骤如下: 1.在实体类中使用@Excel注解标注需要导入导出的字段,同时使用@ExcelCollection注解标注子表对应的集合字段。 2.在Controller中使用EasyExcel工具类进行导入导出操作。具体代码如下: ```java // 导出主子表数据 @RequestMapping("/export") public void export(HttpServletResponse response) throws IOException { // 查询数据 List<MainTable> list = mainTableService.getList(); // 导出操作 EasyExcelUtil.exportExcel(response, list, MainTable.class, "主子表数据"); } // 导入主子表数据 @RequestMapping("/import") public void importData(MultipartFile file) throws IOException { // 导入操作 List<MainTable> list = EasyExcelUtil.importExcel(file, MainTable.class); // 保存数据 mainTableService.saveList(list); } ``` 3.在EasyExcelUtil工具类中封装了导入导出的具体实现。具体代码如下: ```java /** * 导出Excel * * @param response HttpServletResponse * @param list 数据列表 * @param clazz 实体类 * @param fileName 文件名 * @throws IOException IO异常 */ public static void exportExcel(HttpServletResponse response, List<?> list, Class<?> clazz, String fileName) throws IOException { // 设置响应头 response.setContentType("application/vnd.ms-excel"); response.setCharacterEncoding("utf-8"); response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8") + ".xlsx"); // 导出操作 EasyExcel.write(response.getOutputStream(), clazz).sheet("Sheet1").doWrite(list); } /** * 导入Excel * * @param file 文件 * @param clazz 实体类 * @return 数据列表 * @throws IOException IO异常 */ public static List<?> importExcel(MultipartFile file, Class<?> clazz) throws IOException { // 导入操作 return EasyExcel.read(file.getInputStream(), clazz, new ExcelListener()).sheet().doReadSync(); } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值