zoukankan      html  css  js  c++  java
  • 若依项目实现导入功能

    首先该导入功能的实现是基于EasyPoi。

    下面记录一下在若依项目中如何去集成与使用。

    1、在pom.xml文件中加入easyPoi相关依赖。此处需要注意的点,可参考:https://www.cnblogs.com/conswin/p/9766366.html 中的第七条说明。

    2、编写实体类,创建导入导出公共方法,参考https://www.jianshu.com/p/5d67fb720ece即可。

    3、在ry-ui.js中封装页面调用方法,代码如下:

                //使用EasyPoi方式导入
                easyImportExcel: function(fileId) {
                    var currentId = $.common.isEmpty(fileId) ? 'file' : fileId;
                    $("#" + currentId).trigger("click");  
                },
                //执行EasyPoi方式导入
                easyImportExcelDo: function(fileId) {
                    var currentId = $.common.isEmpty(fileId) ? 'file' : fileId;
                    var file =$("#" + currentId)[0].files[0];  
                    if(file == 'undefined' || file == undefined){
                        return ;
                    }
                    var myform = new FormData();
                    myform.append('file',file);
                    
                    $.modal.loading("正在导入数据,请稍后...");
                    $.ajax({
                        url:  $.table._option.importUrl,
                        type: "POST",
                        data: myform,
                        contentType: false,
                        processData: false,
                        success: function (result) {
                            
                            if (result.code == web_status.SUCCESS) {
                                $.modal.msgSuccess(result.msg);
                                $.treeTable.refresh();
                            } else {
                                $.modal.msgError(result.msg);
                            }
                            
                            //清空附件
                            $("#" + currentId).val("");
                            $.modal.closeLoading();
                            
                        },
                        error:function(data){
                            console.log(data);
                            //清空附件
                            $("#" + currentId).val("");
                            $.modal.closeLoading();
                        }
                    });
                },    

    4、前端页面代码如下:此处需要先定义一个隐藏的file类型的标签,导入按钮上直接调用 onclick="$.table.easyImportExcel()"即可。

    <input type="file" id="file" onchange="$.table.easyImportExcelDo()" style="filter:alpha(opacity=0);opacity:0; 0;height: 0;"/>  
              <div class="btn-group-sm hidden-xs" id="toolbar" role="group">
                 <a class="btn btn-success" onclick="$.operate.add(0)" shiro:hasPermission="finance:budgetItem:add">
                    <i class="fa fa-plus"></i> 新增
                 </a>
                 <a class="btn btn-primary" onclick="$.operate.editTree()" shiro:hasPermission="finance:budgetItem:edit">
                    <i class="fa fa-edit"></i> 修改
                    </a>
                    <a class="btn btn-warning" onclick="$.table.easyExportExcel()" shiro:hasPermission="finance:budgetItem:export">
                    <i class="fa fa-upload"></i> 导出
                 </a>
                 <a class="btn btn-warning" onclick="$.table.easyImportExcel()" shiro:hasPermission="finance:budgetItem:import">
                    <i class="fa fa-download"></i> 导入
                 </a>
            </div>

    初始化表格的设置相关参数的时候需要增加:

     importUrl: prefix + "/import",

    5、controller层

        @PostMapping("/import")
        @ResponseBody
        public AjaxResult importExcel(@RequestParam("file") MultipartFile file) {
           
            
                List<BudgetItem> itemList = EasyExcelUtil.importExcel(file,1,1,BudgetItem.class);
                if(itemList == null  ) {
                      return error(1, "请选择正确的导入模板");
                }
        
                int insertCount = 0;
                int updateCount = 0;
                for (BudgetItem item : itemList) {
                    
                     /**此处进行相应数据库的保存操作**/
                    
                }
       
                
                if(insertCount + updateCount  == itemList.size()) {
                    return success("导出成功,新增【" + insertCount + "】条科目,更新【"+updateCount+"】条科目");
                }else {
                    return error(1, "导入失败");
                }
        }
  • 相关阅读:
    关闭弹出窗体,刷新父页面
    Oracle 导出部分表结构,以及导入
    ORCLE报错解决(ora01747:无效的用户.表.列,表.列)
    PL/SQL Developer使用技巧
    自定义table
    Array查询数组中是否包含指定字符
    水晶报表去掉多余小数点
    HttpHandler HttpModule入门篇
    窗口类名无效 错误 解决方法
    2020.10.15
  • 原文地址:https://www.cnblogs.com/conswin/p/10008822.html
Copyright © 2011-2022 走看看