栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java excel导入

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java excel导入

前端jsp

根据需求添加至想要的地方

导入审批清单

根据需求选择需要的数据

<
!--导入数据模态框-->

    
        
            
                
                

导入数据前请先下载审批清单导入样表 审批清单样表下载

前端js

需要定义的

//新选择文件集合
var selectedPics = new Array();
//原始文件集合
var originalPics = new Array();
var allowedFileExtensions = ['xls', 'xlsx'];// 接收的文件后缀

我的项目中还有这些需求 

//导入数据方法

function initPutExcelUpload() {
    showLoading();//调用加载数据方法
    //上传的路径
    // uploadFileSetting['uploadUrl'] = basePath + "baseCommunicationInfo/importbaseCommunicationInfo";
    uploadFileSetting['allowedFileExtensions'] = allowedFileExtensions;
    uploadFileSetting['dropZoneEnabled'] = false;
    uploadFileSetting['showPreview'] = true;
    uploadFileSetting['showRemove'] = true;
    uploadFileSetting['showUpload'] = false;
    uploadFileSetting['showCancel'] = true;
    uploadFileSetting['maxFileCount'] = 1;
    initUploadFile("excelContent", selectedPics, originalPics, "phoneInstallationExcel");
    var fileObj = document.getElementsByClassName('file-caption-name')[0];
    if (fileObj) fileObj.innerHTML = '';
    $('.file-preview').addClass('hide'); //隐藏文件预览组件
    $('.kv-upload-progress').hide();//隐藏进度条
    $(".fileinput-cancel-button").hide();

    //文件上传成功
    $("#excelContent").on("fileuploaded", function (event, data, previewId, index) {
        $("#putExcel").modal("hide");
        var attachmentPath = getFileUrl(selectedPics, originalPics);
        initBizParam();
        showLoading();
        putBizParam("filePath", attachmentPath);
        ajaxRequest("baseCommunicationInfo/importbaseCommunicationInfo", getBizString(), function (data) {//路径
            hideLoading();
            if (data.returnData) {
                //直接清空
                $("#addPersonnelList").html("");
                for (var i = 0; i < data.returnData.length; i++) {
                    var item = data.returnData[i];
                    parseDetailInfo1("#addPersonnelList", ".SB_1","",item.useUserName,item.dutyUserName,
                        item.depName,"",item.serviceProviderName,item.communicationNum,item.sysUserName,
                        item.useApplyTypeName,item.useStartTime,item.isCollectionFlag,"",item.useRemark)//需上传的字段名
                }
            }
        },"POST");
        // 刷新表格
        info("上传成功");
    });
    //文件上传失败
    $("#excelContent").on("fileuploaderror", function (event, data, msg) {
        hideLoading();
        bootstrapQ.alert({
            id:"model1",
            title: "提示",
            msg: msg,
            close: true,
            keyboard: false
        }, function aa() {
            var obj = document.getElementsByClassName('file-caption-name')[0];
            obj.innerHTML = '';
            obj.title = '';
            $("#putExcel").modal("show");
        });
    });


    $("#putExcelOK").on("click", function () {
        var attachmentPath = $("#excelContent").val()
        if (attachmentPath == undefined || attachmentPath == null || attachmentPath == '') {
            info("请选择文件!");
            return;
        }
        $("#putExcel").modal("hide");
        showLoading();
        $("#excelContent").fileinput("upload");
    });
}

// 获取上传的文件地址
function getFileUrl(selectedPics, originalPics) {
    var urls = "";
    $.each(selectedPics, function (index, item) {
        item = JSON.parse(item);
        if (item.hasUpload) {
            urls += ";"+item.picUrl;
        }
    });
    $.each(originalPics, function (index, item) {
        urls += ";"+item;
    });
    if(urls&&urls!=""){
        urls=urls.substring(1);
    }
    return urls;
}

controller层

@PostMapping("/importbaseCommunicationInfo")
public String importbaseCommunicationInfo(@RequestParam(value = "data") String data) throws Exception {
    Map bizmap = (Map) JacksonUtils.json2map(data).get("biz");
    return baseCommunicationService.importbaseCommunicationInfo(bizmap);
}

实体类(pojo层)

@Excel(name = "系统用户名称"):对应excel表列名
@Excel(name = "是否托收",replace = "1_是,-1_否")

        replace:把数字替换成文本

service层

接口

String importbaseCommunicationInfo(Map bizMap) throws Exception;

class类

//上传
@Override
public String importbaseCommunicationInfo(Map bizMap){
    try {
        String path = rootPath + MapUtils.getValueString(bizMap, "filePath");
        String companyId = MapUtils.getValueString(bizMap, "companyId");
        List list =null;
        try {
            ExcelimportResult result = EasyPoiUtils.importExcelMore(path, 1, 1, baseCommunicationInfo.class);
            list = result.getList();
            //部门名称传到前端
            for (baseCommunicationInfo baseCommunicationInfo: list) {
                HashMap stringObjectHashMap = new HashMap<>();
                stringObjectHashMap.put("companyId",companyId);
                stringObjectHashMap.put("departmentName",baseCommunicationInfo.getDeptName());
                List department = departmentMapper.queryByDepName(stringObjectHashMap);
                if(department.size() == 1){
                    baseCommunicationInfo.setDepId(new BigDecimal(department.get(0).getDepartmentNo()));
                    baseCommunicationInfo.setDeptName(department.get(0).getDepartmentName());
                }else{
                    baseCommunicationInfo.setDepId(null);
                    baseCommunicationInfo.setDeptName("");
                }
            }
            return ResultInfoUtils.success(list);
        }catch (ExcelimportException e){
            e.printStackTrace();
            return ResultInfoUtils.businessError("1001","Excel模板不合法,请下载示例模板并按要求填写数据","");
        }
    } catch (Exception e) {
        e.printStackTrace();
        return ResultInfoUtils.systemError();
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/358392.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号