eg:导入券码

获取导入的券码,并校验:

	@Override
	public List<String> importCouponCode(MultipartFile file) {
		List<CouponCodeVO> couponCodeVOList = new ArrayList<>();
		try {
			// 1.获取券码VO集合
			if (file == null || file.getSize() <= 0) {
				throw new BsException(CouponContants.C901, ErrorConstant.FORMATE(CouponContants.M901, "文件不能为空"));
			}
			InputStream input = file.getInputStream();
			Map<String, String> field = new HashMap<>(2);
			field.put("券码", "couponCode");
			couponCodeVOList = ExcelUtil.ExeclToList(input, CouponCodeVO.class, field);
		} catch (Exception ex) {
			throw new BsException(CouponContants.C901, ErrorConstant.FORMATE(CouponContants.M901, "文件导入异常"));
		}
		// 2.获取券码集合cardNoList
		Iterator<CouponCodeVO> iterator = couponCodeVOList.iterator();
		List<String> cardNoList = new ArrayList<>();
		// 去除空元素 + 校验
		while (iterator.hasNext()) {
			CouponCodeVO couponCodeVO = iterator.next();
			if (!StringUtil.isNullOrEmpty(couponCodeVO.getCouponCode())) {
				// 是纯数字,长度12
				Assert.isTrue(
						PatternUtil.isNum(couponCodeVO.getCouponCode()) && couponCodeVO.getCouponCode().length() == 12,
						CouponContants.C901, CouponContants.M901, "券码" + couponCodeVO.getCouponCode() + "不合法,请检查后再导入");
				// 有重复
				Assert.isTrue(!cardNoList.contains(couponCodeVO.getCouponCode()), CouponContants.C901,
						CouponContants.M901, "券码" + couponCodeVO.getCouponCode() + "存在重复,请检查后再导入");
				cardNoList.add(couponCodeVO.getCouponCode());
			} else {
				iterator.remove();
			}
		}
		return cardNoList;
	}

Excel转List

    /**
     * Excel转List <br>
     *
     * @param in 输入流  excel
     * @param entityClass excel中每一行数据的实体类 这里没法用泛型
     * @param fields 字段名字
     * @return java.util.List
     */
    @SuppressWarnings("AlibabaLowerCamelCaseVariableNaming")
    public static List ExeclToList(InputStream in, Class entityClass, Map<String, String> fields) throws Exception {

        List<Object> resultList = new ArrayList<Object>();

        XSSFWorkbook workbook = new XSSFWorkbook(in);

        // excel中字段的中英文名字数组
        String[] egtitles = new String[fields.size()];
        String[] cntitles = new String[fields.size()];
        Iterator<String> it = fields.keySet().iterator();
        int count = 0;
        while (it.hasNext()) {
            String cntitle = (String) it.next();
            String egtitle = fields.get(cntitle);
            egtitles[count] = egtitle;
            cntitles[count] = cntitle;
            count++;
        }

        // 得到excel中sheet总数
        int sheetcount = workbook.getNumberOfSheets();

        if (sheetcount == 0) {
            workbook.close();
            throw new Exception("Excel文件中没有任何数据");
        }
        sheetcount = 1;//sheet只取1

        // 数据的导出
        for (int i = 0; i < sheetcount; i++) {
            XSSFSheet sheet = workbook.getSheetAt(i);
            if (sheet == null) {
                continue;
            }
            // 每页中的第一行为标题行,对标题行的特殊处理
            XSSFRow firstRow = sheet.getRow(0);
            int celllength = firstRow.getLastCellNum();

            String[] excelFieldNames = new String[celllength];
            LinkedHashMap<String, Integer> colMap = new LinkedHashMap<String, Integer>();

            // 获取Excel中的列名
            for (int f = 0; f < celllength; f++) {
                XSSFCell cell = firstRow.getCell(f);
                excelFieldNames[f] = cell.getStringCellValue().trim();
                // 将列名和列号放入Map中,这样通过列名就可以拿到列号
                for (int g = 0; g < excelFieldNames.length; g++) {
                    colMap.put(excelFieldNames[g], g);
                }
            }
            // 由于数组是根据长度创建的,所以值是空值,这里对列名map做了去空键的处理
            colMap.remove(null);
            // 判断需要的字段在Excel中是否都存在
            // 需要注意的是这个方法中的map中:中文名为键,英文名为值
            boolean isExist = true;
            List<String> excelFieldList = Arrays.asList(excelFieldNames);
            for (String cnName : fields.keySet()) {
                if (!excelFieldList.contains(cnName)) {
                    isExist = false;
                    break;
                }
            }
            // 如果有列名不存在,则抛出异常,提示错误
            if (!isExist) {
                workbook.close();
                throw new Exception("Excel中缺少必要的字段,或字段名称有误");
            }
            // 将sheet转换为list
            for (int j = 1; j <= sheet.getLastRowNum(); j++) {
                XSSFRow row = sheet.getRow(j);
                // 根据Obj创建实体类
                if(row!=null){//过滤空行
                    Object entity =  entityClass.newInstance();
                    // 给对象中的字段赋值
                    for (Map.Entry<String, String> entry : fields.entrySet()) {
                        // 获取中文字段名
                        String cnNormalName = entry.getKey();
                        // 获取英文字段名
                        String enNormalName = entry.getValue();
                        // 根据中文字段名获取列号
                        int col = colMap.get(cnNormalName);
                        // 获取当前单元格中的内容
                        XSSFCell cell = row.getCell(col);
                        cell.setCellType(CellType.STRING);
                        String content = cell.toString().trim();
                        //去除数字的默认小数点 .0
                        int length = content.length();
                        if (length>=2) {  //这里大于等于2是防止有些列只有一个字符,到下面会报错
                            if (content.substring(length-2, length).equals(".0")){
                                content = content.substring(0,length-2);
                            } //通过截取最后两个字符,如果等于.0 就 去除最后两个字符
                        }
                        // 给对象赋值
                        setFieldValueByName(enNormalName, content, entity);
                    }
                    resultList.add(entity);
                }
            }
        }
        workbook.close();
        return resultList;
    }
### CSS `position: absolute` 的作用与用法 #### 绝对定位的概念 当元素被赋予 `position: absolute` 属性时,该元素将脱离标准文档流,并相对于最近的已定位祖先元素(即其父级容器具有除 static 外其他 position 值)进行定位[^2]。如果不存在这样的祖先,则依据初始包含块——通常是视口。 #### 设置方式及其影响因素 为了使绝对定位生效并按照预期工作,需注意以下几点: - **参照物的选择**:默认情况下,绝对定位基于整个文档坐标系;然而,一旦某个上级元素设置了非静态位置 (`relative`, `absolute`, 或者 `fixed`) ,那么子元素就会相对这个新的上下文来进行布局调整。 - **偏移量设定**:通过指定四个方向上的距离(`top`, `right`, `bottom`, 和/或 `left`) 来精确定义目标对象的位置。这些值可以是具体的像素数、百分比或是其它长度单位[^1]。 #### 实际应用案例分析 考虑如下 HTML 结构中的 `.child` 类型节点,在不同情境下如何实现特定视觉效果: ```html <div class="parent"> <div class="child"></div> </div> ``` ##### 场景一:简单固定于页面角落 假设希望`.child`始终位于屏幕右下方而不受滚动条的影响,此时只需为其添加样式声明即可达成目的: ```css .child { position: fixed; bottom: 0; right: 0; } ``` 请注意这里使用了 `fixed` 而不是 `absolute`,因为前者能够确保即使用户浏览网页也不会改变此组件的位置。 ##### 场景二:居中展示内容区域 为了让某模块在其直接包裹层内部水平垂直居心显示,有多种途径可供选择,以下是三种常见做法之一: ```css /* 方法A */ .main_center{ position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%); } /* 方法B */ .main_center{ position: absolute; width: fit-content; /* 自适应宽度 */ height: fit-content; /* 自适应高度 */ margin: auto; top: 0; left: 0; right: 0; bottom: 0; } ``` 上述两种方案均能有效处理大多数情况下的居中需求,其中第二种更适用于未知尺寸的对象[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值