若依項目實現導入功能


首先該導入功能的實現是基於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;width: 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, "導入失敗");
            }
    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM