You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
83 lines
2.3 KiB
83 lines
2.3 KiB
package com.utils;
|
|
|
|
import java.util.Random;
|
|
import java.util.ArrayList;
|
|
import org.springframework.stereotype.Component;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
import org.apache.poi.ss.usermodel.Cell;
|
|
import java.text.DecimalFormat;
|
|
import java.util.Objects;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
@Component
|
|
public class CommonUtil {
|
|
/**
|
|
* 获取随机字符串
|
|
*
|
|
* @param num
|
|
* @return
|
|
*/
|
|
public static String getRandomString(Integer num) {
|
|
String base = "abcdefghijklmnopqrstuvwxyz0123456789";
|
|
Random random = new Random();
|
|
StringBuffer sb = new StringBuffer();
|
|
for (int i = 0; i < num; i++) {
|
|
int number = random.nextInt(base.length());
|
|
sb.append(base.charAt(number));
|
|
}
|
|
return sb.toString();
|
|
}
|
|
|
|
/**
|
|
* 获取随机验证码
|
|
*
|
|
* @param num
|
|
* @return
|
|
*/
|
|
public static String getRandomNumber(Integer num) {
|
|
String base = "0123456789";
|
|
Random random = new Random();
|
|
StringBuffer sb = new StringBuffer();
|
|
for (int i = 0; i < num; i++) {
|
|
int number = random.nextInt(base.length());
|
|
sb.append(base.charAt(number));
|
|
}
|
|
return sb.toString();
|
|
}
|
|
|
|
public static String getCellValue(Cell cell) {
|
|
String resultValue = "";
|
|
// 判空
|
|
if (Objects.isNull(cell)) {
|
|
return resultValue;
|
|
}
|
|
|
|
// 拿到单元格类型
|
|
int cellType = cell.getCellType();
|
|
switch (cellType) {
|
|
// 字符串类型
|
|
case Cell.CELL_TYPE_STRING:
|
|
resultValue = StringUtils.isEmpty(cell.getStringCellValue()) ? "" : cell.getStringCellValue().trim();
|
|
break;
|
|
// 布尔类型
|
|
case Cell.CELL_TYPE_BOOLEAN:
|
|
resultValue = String.valueOf(cell.getBooleanCellValue());
|
|
break;
|
|
// 数值类型
|
|
case Cell.CELL_TYPE_NUMERIC:
|
|
resultValue = new DecimalFormat("#.######").format(cell.getNumericCellValue());
|
|
break;
|
|
// 取空串
|
|
default:
|
|
break;
|
|
}
|
|
return resultValue;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|