Bootstrap

RUOYI 框架教程 5 |若依Excell导入这么做,0经验小白都能写!

 若依教程独家分享!点赞+关注,一起从「蒟蒻」变成「大佬」~

1 excell导入功能实现,你get了多少?

本期内容

前端页面怎么调?

后台逻辑怎么写?

2 ,前端页面应该怎么调,你学会了么?

在主html中表格的工具栏toolbar div层添加代码



   批量导入

在表格操作头的script下的function 中添加两个连接url参数

importUrl: prefix + "/importData",importTemplateUrl: prefix + "/importTemplate",

在主html中标签前引入导入区

3 后端页面应该怎么写,你学会了么?

Controller层下载模板

@GetMapping ( "/importTemplate" )
  @ResponseBody
  public AjaxResult importTemplate ( ) {
    ExcelUtil < TSylx > util = new ExcelUtil < TSylx > ( TSylx.class );
    return util.importTemplateExcel ( "商业公司流向导入模板" );
  }

Controller 层导入数据处理,其中调用了插入数据的方法


@PostMapping ( "/importData" )
@ResponseBody
public AjaxResult importData ( MultipartFile file , boolean updateSupport ) throws Exception {
    ExcelUtil < TZdfx > util = new ExcelUtil < TZdfx > ( TZdfx.class );
    List < TZdfx > list = util.importExcel ( file.getInputStream ( ) );
    String message = importTSylx ( list, updateSupport );
    return AjaxResult.success ( message );
}

public String importTSylx ( List < TZdfx > list , Boolean isUpdateSupport ) {
    if ( StringUtils.isNull ( list ) || list.size ( ) == 0 ) {
        throw new BusinessException ( "导入商业流向数据不能为空!" );
    }
    int successNum = 0;
    int failureNum = 0;
    StringBuilder successMsg = new StringBuilder ( );
    StringBuilder failureMsg = new StringBuilder ( );
    for ( TZdfx tZdfx : list ) {

        log.warn ( tZdfx.toString () );
        addSave(tZdfx);

    }
    if ( failureNum > 0 ) {
        failureMsg.insert ( 0 , "很抱歉,导入失败!共 " + failureNum + " 条数据格式不正确,错误如下:" );
        throw new BusinessException ( failureMsg.toString ( ) );
    } else {
        successMsg.insert ( 0 , "恭喜您,数据已全部导入成功!共 " + list.size () + " 条,数据如下:" +"\n"+list.toString ());
    }
    return successMsg.toString ( );
}

如果想围观票圈,可以扫这个二维码加我微信~