springboot+bootStrap導入excel


<div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title title">導入</h4>
                </div>
                <div class="modal-body">
                    <div class="row">
                        <div class="col-lg-12">
                            <form id="defaultForm" method="" class="form-horizontal recoveryNodeForm" action="">
                                <div class="col-lg-12">
                                    <div class="form-group">
                                        <label class="col-lg-3 control-label">導入文件</label>
                                        <div class="col-lg-6">
                                            <input type="file" class="form-control" style="height:36px;" name="uploadFile" id="uploadFile"/>
                                        </div>
                                        <button type="button" class="btn btn-primary" id="uploadExcel">上傳</button>
                                    </div>
                                </div>
                                <input type="hidden" name="pkId" value="" />
                            </form>
                        </div>
                    </div>
                    <div>
                        <span><b>導入結果反饋</b></span>
                        <ul id="exportResult">

                        </ul>
                    </div>
                </div>

 

uploadExcel : function () {
            $("#uploadExcel").on("click","",function () {
                $(".recoveryNodeForm").data("bootstrapValidator").validate();
                var flag = $(".recoveryNodeForm").data("bootstrapValidator").isValid();
                if(!flag){
                    //未通過驗證
                    return false;
                }

                var fileObj = document.getElementById("uploadFile").files[0];
                var formFile = new FormData();
                formFile.append("file", fileObj);
                var data = formFile;
                $.ajax({
                    url: ctx +'/recovery/netStorage/uploadFile.mvc',
                    data: data,
                    type: "Post",
                    dataType: "json",
                    cache: false,//上傳文件無需緩存
                    processData: false,//用於對data參數進行序列化處理 這里必須false
                    contentType: false, //必須
                    success: function (result) {
                        var htmlstr = '';
                        if(result.result==false){
                            for(var i=0;i<result.data.length;i++){
                                htmlstr += '<li>'+result.data[i]+'</li>';
                            }
                        } else {
                            htmlstr = '<li>上傳成功</li>';
                        }

                        $('#exportResult').html(htmlstr);
                    },
                    error: function(XMLHttpRequest, textStatus, errorThrown){
                        DialogUtil.error("系統錯誤");
                    }
                });
            });
        }
validatorForm :function () {
$(".recoveryNodeForm").bootstrapValidator({
message: 'This value is not valid',
live: 'submitted',
fields: {/*驗證*/
uploadFile: {
message: '導入文件無效',
validators: {
notEmpty: {/*非空提示*/
message: '導入文件不能為空'
},
regexp: {
regexp: /.xlsx$/,
message: '導入文件類型必須是excel'
}
}
}
}
});

}
 
@RequestMapping("/uploadFile")
    @ResponseBody
    public StandardResult uploadFile(@RequestParam("file") MultipartFile file) throws IOException{
        SysUser sysUser = BaseUtil.getCurrentUser();
        XSSFWorkbook workbook = new XSSFWorkbook(file.getInputStream());
        StandardResult result = customerBatteryService.insertDataByExcel(workbook,sysUser);
        return result;
    }
public StandardResult insertDataByExcel(XSSFWorkbook workbook, SysUser sysUser){
        StandardResult result = checkExcel(workbook);
        if(result.getResult() == false){
            return result;
        }
        //A-1訂單信息sheet 5列
        XSSFSheet sheet0 = workbook.getSheetAt(0);
        CustomerBattery customerBattery;
        String excelId = BaseUtil.getUUID();
        String departmentId = sysUser.getDepartmentId();
       for(int i=3;i<=sheet0.getLastRowNum();i++){//從第4行讀起,讀到最后一行
           customerBattery = new CustomerBattery();
            //讀取指定索引行的值
            XSSFRow row = sheet0.getRow(i);
            if(row == null) continue;
            if(checkAllCellEmpty(row,0,4)) continue;
           customerBattery.setPkId(BaseUtil.getUUID());
           customerBattery.setExcelId(excelId);
           customerBattery.setDepartmentId(departmentId);
           customerBattery.setOrderNumber(row.getCell(0).toString());//訂單號
           customerBattery.setKsName(row.getCell(1).toString());//客戶名稱
           customerBattery.setSupplyName(row.getCell(2).toString());//供貨電池廠名稱
           customerBattery.setPackNumber((int)row.getCell(3).getNumericCellValue());//所含電池包數
           customerBattery.setModelNumber((int)row.getCell(4).getNumericCellValue());//所含電池模塊數
           customerBattery.setDelFlag("0");
           customerBattery.setStatus("0");
           customerBattery.setCreateUser(sysUser.getTruename());
           customerBattery.setCreateDate(new Date());
           if("上汽通用五菱".equals(row.getCell(1).toString().toString())){
               customerBattery.setInterfaceName("五菱");
           }else if("江鈴汽車".equals(row.getCell(1).toString().toString())){
               customerBattery.setInterfaceName("江鈴");
           }
            customerBatteryDao.insert(customerBattery);
        }
        //A-2售后電池信息sheet 3列
        XSSFSheet sheet1 = workbook.getSheetAt(1);
        CustomerBatteryDetail customerBatteryDetail;
        for(int i=3;i<=sheet1.getLastRowNum();i++){//從第4行讀起,讀到最后一行
            customerBatteryDetail = new CustomerBatteryDetail();
            //讀取指定索引行的值
            XSSFRow row = sheet1.getRow(i);
            if(row == null) continue;
            if(checkAllCellEmpty(row,0,2)) continue;
            customerBatteryDetail.setPkId(BaseUtil.getUUID());
            customerBatteryDetail.setExcelId(excelId);
            customerBatteryDetail.setCustomerType(row.getCell(0).toString());//售后產品類型(電池包/電池模塊)
            customerBatteryDetail.setBatteryModel(row.getCell(1).toString());//電池型號
            customerBatteryDetail.setBatteryCode(row.getCell(2).toString());//電池編碼
            customerBatteryDetail.setDelFlag("0");
            customerBatteryDetail.setCreateUser(sysUser.getTruename());
            customerBatteryDetail.setCreateDate(new Date());
            customerBatteryDetailDao.insert(customerBatteryDetail);
        }
        //A-3 電池模塊編碼信息sheet 3列
        XSSFSheet sheet2 = workbook.getSheetAt(2);
        ModulePackRelation modulePackRelation;
        for(int i=3;i<sheet2.getLastRowNum();i++){//從第4行讀起,讀到最后一行
            XSSFRow row = sheet2.getRow(i);
            if(row == null) continue;
            if(checkAllCellEmpty(row,0,2)) continue;
            modulePackRelation = new ModulePackRelation();
            modulePackRelation.setPkId(BaseUtil.getUUID());
            modulePackRelation.setExcelId(excelId);
            modulePackRelation.setModuleCode(row.getCell(0).toString());//電池模塊編碼
            modulePackRelation.setModuleModel(row.getCell(1).toString());//電池模塊型號
            modulePackRelation.setPackCode(row.getCell(2).toString());//所屬電池包編碼
            modulePackRelation.setDelFlag("0");
            modulePackRelation.setCreateDate(new Date());
            modulePackRelation.setCreateUser(sysUser.getTruename());
            modulePackRelation.setType("2");
            modulePackRelationDao.insert(modulePackRelation);
        }
        //A-4 單體電池編碼信息
        XSSFSheet sheet3 = workbook.getSheetAt(3);
        MonomerModuleRelation monomerModuleRelation;
        for(int i=3;i<=sheet3.getLastRowNum();i++) {//從第4行讀起,讀到最后一行
            XSSFRow row = sheet3.getRow(i);
            if (row == null) continue;
            if (checkAllCellEmpty(row, 0, 2)) continue;
            monomerModuleRelation = new MonomerModuleRelation();
            monomerModuleRelation.setPkId(BaseUtil.getUUID());
            monomerModuleRelation.setExcelId(excelId);
            monomerModuleRelation.setMonomerCode(row.getCell(0).toString());//單體電池編碼
            monomerModuleRelation.setModuleCode(row.getCell(1).toString());//所屬電池模塊編碼
            monomerModuleRelation.setDelFlag("0");
            monomerModuleRelation.setCreateDate(new Date());
            monomerModuleRelation.setCreateUser(sysUser.getTruename());
            monomerModuleRelation.setType("2");
            monomerModuleRelationDao.insert(monomerModuleRelation);
        }

        return result;
    }
private StandardResult checkExcel(XSSFWorkbook workbook) {
        StandardResult result = new StandardResult();
        //錯誤信息匯總
        LinkedHashSet hs1 = new LinkedHashSet();
        //找不到型號匯總
        LinkedHashSet hs2 = new LinkedHashSet();
        //客戶名稱輸入錯誤
        LinkedHashSet hsProductor = new LinkedHashSet();
        //模塊對應包不存在
        LinkedHashSet hs3 = new LinkedHashSet();
        //所含電池包數
        int packCountAll = 0;
        //所含模塊數
        int moduleCountAll= 0;
        boolean errorFlag = true;
        //電池包編碼對應的電池包型號
        Map<String, String> packMap = new HashMap<>();
        List<Map<String, String>> listPack = new ArrayList<Map<String, String>>();
        //電池編碼錯誤
        LinkedHashSet hasPack = new LinkedHashSet();
        //電池模塊編碼錯誤
        LinkedHashSet hasModule = new LinkedHashSet();
        //單體電池編碼錯誤
        LinkedHashSet hasMonomer = new LinkedHashSet();
        //A-1訂單信息sheet 5列
        EntityWrapper<PackRecord> ewpr;
        List<PackRecord> packRecords;
        EntityWrapper<CustomerBattery> ewppi;
        List<CustomerBattery> customerBatteryInfos  ;
        if(workbook.getNumberOfSheets() < 4) {
            result.setResult(false);
            LinkedHashSet sheetHs = new LinkedHashSet();
            sheetHs.add("數據sheet缺失,請檢查");
            result.setData(sheetHs);
            return result;
        }
        XSSFSheet sheet0 = workbook.getSheetAt(0);
        for(int i=3;i<=sheet0.getLastRowNum();i++){//從第4行讀起,讀到最后一行
            //讀取指定索引行的值
            XSSFRow row = sheet0.getRow(i);
            if(row == null) continue;
            if(checkAllCellEmpty(row,0,4)) continue;
            if(i == 4){
                if(checkAllCellEmpty(row,0,4) == false){
                    result.setResult(false);
                    LinkedHashSet sheetHs = new LinkedHashSet();
                    sheetHs.add("只能填寫一個訂單,請檢查");
                    result.setData(sheetHs);
                    return result;
                }
            }
            if(checkCellEmpty(row.getCell(0)))
                hs1.add("訂單號不能為空");
            if(checkCellEmpty(row.getCell(1))) {
                hs1.add("客戶名稱不能為空");
            }else if(!"上汽通用五菱".equals(row.getCell(1).toString()) && !"江鈴汽車".equals(row.getCell(1).toString())){
                hs1.add("客戶名稱錯誤");
            }else{
                hsProductor.add(row.getCell(1).toString());
                if(!hsProductor.contains(row.getCell(1).toString())){
                    hs1.add("客戶名稱必須相同");
                }else{
                    //如果同一訂單號,客戶名稱必須相同
                    EntityWrapper<CustomerBattery> ewprcb;
                    ewprcb = new EntityWrapper<>();
                    List<CustomerBattery> customerBatteryList =customerBatteryDao.selectPage(new Page<CustomerBattery>(1,1),ewprcb.eq("ORDER_NUMBER",row.getCell(0).toString()).eq("del_flag", CommonConstants.DEL_FLAG));
                    if(customerBatteryList.size() >0 && ! customerBatteryList.isEmpty()){
                        String kaName = customerBatteryList.get(0).getKsName();
                        if(! kaName.equals(row.getCell(1).toString())){
                            hs1.add("同一訂單號客戶名稱必須相同");
                        }
                    }
                }

            };
            if(checkCellEmpty(row.getCell(2))){
                hs1.add("供貨電池廠名稱不能為空");
            }else{
                EntityWrapper<BatteryProductor> ewprbp;
                ewprbp = new EntityWrapper<>();
                List<BatteryProductor> batteryProductorList =batteryProductorDao.selectPage(new Page<BatteryProductor>(1, 1),ewprbp.eq("del_flag", CommonConstants.DEL_FLAG));
                boolean flag = false;
                for(BatteryProductor batteryProductor :batteryProductorList){
                    if(row.getCell(2).toString().equals(batteryProductor.getProductorName())){
                        flag = true;
                        break;
                    }
                }
                if(flag == false){
                    hs1.add("供貨電池廠名稱在電池廠商備案中不存在");
                }else{
                    //如果同一訂單號,客戶名稱必須相同
                    EntityWrapper<CustomerBattery> ewprcb;
                    ewprcb = new EntityWrapper<>();
                    List<CustomerBattery> customerBatteryList =customerBatteryDao.selectPage(new Page<CustomerBattery>(1,1),ewprcb.eq("ORDER_NUMBER",row.getCell(0).toString()).eq("del_flag", CommonConstants.DEL_FLAG));
                    if(customerBatteryList.size() >0 && ! customerBatteryList.isEmpty()){
                        String supplyName = customerBatteryList.get(0).getSupplyName();
                        if(! supplyName.equals(row.getCell(2).toString())){
                            hs1.add("同一訂單號供貨電池廠名稱必須相同");
                        }
                    }
                }
            }

            if(checkCellEmpty(row.getCell(3))){
                hs1.add("所含電池包數不能為空");
            }else if(row.getCell(3).getCellType() != 0){
                errorFlag = false;
                hs1.add("所含電池包數必須為數字");
            }else{
                packCountAll = (int)row.getCell(3).getNumericCellValue();
            }
            System.out.print(row.getCell(4).getCellType());
            if(checkCellEmpty(row.getCell(4))) {
                hs1.add("所含電池模塊數不能為空");
            }else if(row.getCell(4).getCellType() != 0){
                errorFlag = false;
                hs1.add("所含電池模塊數必須為數字");
            }else{
                moduleCountAll = (int)row.getCell(4).getNumericCellValue();
            }
        }

        hs2 = new LinkedHashSet();
        //A-2售后電池信息3列
        XSSFSheet sheet1 = workbook.getSheetAt(1);
        //電池包計數
        int packCounts = 0;
        //模塊計數
        int moduleCounts = 0;
        for(int i=3;i<=sheet1.getLastRowNum();i++){//從第4行讀起,讀到最后一行
            //讀取指定索引行的值
            XSSFRow row = sheet1.getRow(i);
            if(row == null) continue;
            if(checkAllCellEmpty(row,0,2)) continue;
            if(checkCellEmpty(row.getCell(0))){
                hs1.add("售后產品類型(電池包/電池模塊)不能為空");
            }else if(! "電池包".equals(row.getCell(0).toString()) &&
                    ! "電池模塊".equals(row.getCell(0).toString())){
                hs1.add("售后產品類型(電池包/電池模塊)填寫錯誤");
            }else{
                if("電池包".equals(row.getCell(0).toString())){
                    if(packCountAll == 0 && errorFlag != false){
                        hs1.add("訂單中不存在電池包");
                    }else{
                        packCounts++;
                    }

                }else{
                    if(moduleCountAll == 0 && errorFlag != false){
                        hs1.add("訂單中不存在電池模塊");
                    }else{
                        moduleCounts++;
                    }
                }

            }

            if(checkCellEmpty(row.getCell(1))) {
                hs1.add("電池型號不能為空");
            }else {
                if("電池包".equals(row.getCell(0).toString())){
                    EntityWrapper<PackRecord> prewmpi = new EntityWrapper<PackRecord>();
                    List<PackRecord> packRecordList= packDao.selectPage(new Page<PackRecord>(1,1),prewmpi.eq("PACK_MODEL",row.getCell(1).toString()).eq("del_flag", CommonConstants.DEL_FLAG));
                    if(packRecordList.isEmpty()){
                        hs2.add(row.getCell(1).toString());
                    }
                }else {
                    EntityWrapper<ModuleRecord> mrewmpi = new EntityWrapper<ModuleRecord>();
                    List<ModuleRecord> moduleRecordList= moduleDao.selectPage(new Page<ModuleRecord>(1,1),mrewmpi.eq("MODULE_MODEL",row.getCell(1).toString()).eq("del_flag", CommonConstants.DEL_FLAG));
                    if(moduleRecordList.isEmpty()){
                        hs2.add(row.getCell(1).toString());
                    }
                }
            };
            if(checkCellEmpty(row.getCell(2))) {
                hs1.add("電池編碼不能為空");
            }else {
                EntityWrapper<CustomerBatteryDetail> ewmpcb;
                ewmpcb = new EntityWrapper<>();
                List<CustomerBatteryDetail> customerBatteryDetailList = customerBatteryDetailDao.selectPage(new Page<CustomerBatteryDetail>(1,1),ewmpcb.eq("BATTERY_CODE",row.getCell(2).toString()).eq("del_flag", CommonConstants.DEL_FLAG));
                if(hasPack.contains(row.getCell(2).toString())){
                    hs1.add("電池編碼重復");
                }else if(! customerBatteryDetailList.isEmpty()){
                    hs1.add("電池編碼已存在");
                }
                hasPack.add(row.getCell(2).toString());
                if(!checkCellEmpty(row.getCell(0)) && "電池模塊".equals(row.getCell(0).toString())){
                    hasModule.add(row.getCell(2).toString());
                }
            }
            //電池型號和對應電池編碼
            if(! checkCellEmpty(row.getCell(1)) && ! checkCellEmpty(row.getCell(2)) && !checkCellEmpty(row.getCell(0))){
                if("電池包".equals(row.getCell(0).toString())){
                    packMap.put(row.getCell(2).toString(),row.getCell(1).toString());
                }
                listPack.add(packMap);
            }

        }
        if(packCounts != packCountAll){
            hs1.add("電池包個數與訂單信息中所含電池包數不對應");
        }
        if(moduleCounts != moduleCountAll){
            hs1.add("電池模塊數與訂單信息中所含電池模塊數不對應");
        }
        if(hs2.size() > 0){
            hs1.add("電池型號"+hs2.toString()+"在備案中不存在");
        }
        hs2 = new LinkedHashSet();
        //A-3電池模塊編碼信息 3列
        XSSFSheet sheet2 = workbook.getSheetAt(2);
        for(int i=3;i<=sheet2.getLastRowNum();i++) {//從第4行讀起,讀到最后一行
            XSSFRow row = sheet2.getRow(i);
            if(row == null) continue;
            if(checkAllCellEmpty(row,0,2)) continue;
            if(packCountAll==0 && i  > 2 && errorFlag != false){
                if(checkAllCellEmpty(row,0,4) == false){
                    result.setResult(false);
                    LinkedHashSet sheetHs = new LinkedHashSet();
                    sheetHs.add("電池模塊編碼信息不該有數據,請檢查");
                    result.setData(sheetHs);
                    return result;
                }
            }
            if(checkCellEmpty(row.getCell(0))) {
                hs1.add("電池模塊編碼不能為空");
            }else{
                EntityWrapper<ModulePackRelation> ewmpmp;
                ewmpmp = new EntityWrapper<>();
                List<ModulePackRelation> modulePackRelationList = modulePackRelationDao.selectPage(new Page<ModulePackRelation>(1,1),ewmpmp.eq("MODULE_CODE",row.getCell(0).toString()).eq("del_flag", CommonConstants.DEL_FLAG));
                if(hasModule.contains(row.getCell(0).toString())){
                    hs1.add("電池模塊編碼重復");
                }else if(! modulePackRelationList.isEmpty()){
                    hs1.add("電池模塊編碼已存在");
                }
                hasModule.add(row.getCell(0).toString());
            }
            if(checkCellEmpty(row.getCell(1))) {
                hs1.add("電池模塊型號不能為空");
            }else{
                EntityWrapper<ModuleRecord> mrewmpi = new EntityWrapper<ModuleRecord>();
                List<ModuleRecord> moduleRecordList= moduleDao.selectPage(new Page<ModuleRecord>(1,1),mrewmpi.eq("MODULE_MODEL",row.getCell(1).toString()).eq("del_flag", CommonConstants.DEL_FLAG));
                if(moduleRecordList.isEmpty()){
                    hs2.add(row.getCell(1).toString());
                }
            }
            if(checkCellEmpty(row.getCell(2))) {
                hs1.add("所屬電池包編碼不能為空");
            }else{
                //是否找到對應的電池包編碼
                if(! hasPack.contains(row.getCell(2).toString())){
                    hs1.add("模塊所屬電池包編碼不存在");
                }else{
                    //判斷該模塊型號對應的excel包是否在備案信息里
                    String packCode = null;
                    if(!checkCellEmpty(row.getCell(1))){
                        String moduleExcel = row.getCell(1).toString();
                        for(int m=0;m<listPack.size();m++){
                            packCode = listPack.get(m).get(row.getCell(2).toString());
                        }
                        //查詢該包下的所有模塊
                        List<String> moduleRecordList = moduleDao.selectModuleByPackCode(packCode);
                        boolean flag = false;
                        for(String moduleCode: moduleRecordList){
                            if(moduleCode.equals(moduleExcel)){
                                flag = true;
                                break;
                            }
                        }
                        if(flag == false){
                            hs3.add(row.getCell(1).toString());
                            hs1.add("模塊"+hs3.toString()+"與包型號不對應");
                        }
                    }
                }

            }
        }
        if(hs2.size() > 0){
            hs1.add("電池模塊型號"+hs2.toString()+"在備案中不存在");
        }
        //A-4單體電池編碼信息 2列
        XSSFSheet sheet3 = workbook.getSheetAt(3);
        for(int i=3;i<=sheet3.getLastRowNum();i++) {//從第4行讀起,讀到最后一行
            XSSFRow row = sheet3.getRow(i);
            if (row == null) continue;
            if (checkAllCellEmpty(row, 0, 1)) continue;
            if (checkCellEmpty(row.getCell(0))) {
                hs1.add("單體電池編碼不能為空");
            }else{
                EntityWrapper<MonomerModuleRelation> ewmpmp;
                ewmpmp = new EntityWrapper<>();
                List<MonomerModuleRelation> monomerModuleRelationList = monomerModuleRelationDao.selectPage(new Page<MonomerModuleRelation>(1,1),ewmpmp.eq("MONOMER_CODE",row.getCell(0).toString()).eq("del_flag", CommonConstants.DEL_FLAG));
                if(hasMonomer.contains(row.getCell(0).toString())){
                    hs1.add("單體電池編碼重復");
                }else if(! monomerModuleRelationList.isEmpty()){
                    hs1.add("單體電池編碼已存在");
                }
                hasMonomer.add(row.getCell(0).toString());
            }
            if (checkCellEmpty(row.getCell(1))) {
                hs1.add("所屬電池模塊編碼不能為空");
            }else{
                if(! hasModule.contains(row.getCell(1).toString())){
                    hs1.add("單體所屬電池模塊編碼不存在");
                }
            }
        }
        result.setData(hs1);
        if(hs1.size() > 0) result.setResult(false);
        else result.setResult(true);
        return result;
    }
/**
     * @param row 一行數據
     * @param begin 開始位置
     * @param end 結束位置
     * @return 全為空返回true
     */
    public boolean checkAllCellEmpty(XSSFRow row,int begin,int end){
        boolean flag = true;
        for (int i = begin; i <  end; i++) {
            if(row.getCell(i)==null||"".equals(row.getCell(i))||row.getCell(i).getCellType() == XSSFCell.CELL_TYPE_BLANK){

            }else{
                flag = false;
                break;
            }
        }
        return flag;
    }
/**
     * @param cell 單個cell校驗
     * @return 為空返回true
     */
    public boolean checkCellEmpty(XSSFCell cell){
        if(cell==null||"".equals(cell)||cell.getCellType() == XSSFCell.CELL_TYPE_BLANK){
            return true;
        }
        return false;
    }

 




免責聲明!

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



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