获取导入的券码,并校验:
@Override
public List<String> importCouponCode(MultipartFile file) {
List<CouponCodeVO> couponCodeVOList = new ArrayList<>();
try {
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, "文件导入异常"));
}
Iterator<CouponCodeVO> iterator = couponCodeVOList.iterator();
List<String> cardNoList = new ArrayList<>();
while (iterator.hasNext()) {
CouponCodeVO couponCodeVO = iterator.next();
if (!StringUtil.isNullOrEmpty(couponCodeVO.getCouponCode())) {
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
@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);
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++;
}
int sheetcount = workbook.getNumberOfSheets();
if (sheetcount == 0) {
workbook.close();
throw new Exception("Excel文件中没有任何数据");
}
sheetcount = 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>();
for (int f = 0; f < celllength; f++) {
XSSFCell cell = firstRow.getCell(f);
excelFieldNames[f] = cell.getStringCellValue().trim();
for (int g = 0; g < excelFieldNames.length; g++) {
colMap.put(excelFieldNames[g], g);
}
}
colMap.remove(null);
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中缺少必要的字段,或字段名称有误");
}
for (int j = 1; j <= sheet.getLastRowNum(); j++) {
XSSFRow row = sheet.getRow(j);
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();
int length = content.length();
if (length>=2) {
if (content.substring(length-2, length).equals(".0")){
content = content.substring(0,length-2);
}
}
setFieldValueByName(enNormalName, content, entity);
}
resultList.add(entity);
}
}
}
workbook.close();
return resultList;
}