|
|
@@ -6,9 +6,11 @@ import com.railway.common.annotation.Excel.Type;
|
|
|
import com.railway.common.annotation.Excels;
|
|
|
import com.railway.common.constant.Constants;
|
|
|
import com.railway.common.core.text.Convert;
|
|
|
+import com.railway.common.enums.DelFlagEnum;
|
|
|
import com.railway.common.exception.UtilException;
|
|
|
import com.railway.common.utils.DictUtils;
|
|
|
import com.railway.common.utils.LocalDateUtil;
|
|
|
+import com.railway.common.utils.SecurityUtils;
|
|
|
import com.railway.common.utils.StringUtils;
|
|
|
import com.railway.common.utils.file.FileTypeUtils;
|
|
|
import com.railway.common.utils.file.FileUploadUtils;
|
|
|
@@ -321,7 +323,7 @@ public class ExcelUtil<T> {
|
|
|
public List<T> importExcel(String sheetName, InputStream is, int titleNum, int dataStartNum)
|
|
|
throws Exception {
|
|
|
this.wb = WorkbookFactory.create(is);
|
|
|
- return importExcel(sheetName, wb, titleNum, dataStartNum);
|
|
|
+ return importExcel(null, wb, sheetName, titleNum, dataStartNum);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -331,8 +333,8 @@ public class ExcelUtil<T> {
|
|
|
* @param sheetName 表格索引名
|
|
|
* @return 转换后集合
|
|
|
*/
|
|
|
- public List<T> importExcel(String sheetName, Workbook wb, int titleNum) throws Exception {
|
|
|
- return importExcel(sheetName, wb, titleNum, titleNum + 1);
|
|
|
+ public List<T> importExcel(Long fileId, Workbook wb, String sheetName, int titleNum) throws Exception {
|
|
|
+ return importExcel(fileId, wb, sheetName, titleNum, titleNum + 1);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -342,8 +344,8 @@ public class ExcelUtil<T> {
|
|
|
* @param sheetName 表格索引名
|
|
|
* @return 转换后集合
|
|
|
*/
|
|
|
- public List<T> importExcel(String sheetName, Workbook wb) throws Exception {
|
|
|
- return importExcel(sheetName, wb, 0, 1);
|
|
|
+ public List<T> importExcel(Long fileId, Workbook wb, String sheetName) throws Exception {
|
|
|
+ return importExcel(fileId, wb, sheetName, 0, 1);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -354,7 +356,7 @@ public class ExcelUtil<T> {
|
|
|
* @param wb Workbook
|
|
|
* @return 转换后集合
|
|
|
*/
|
|
|
- public List<T> importExcel(String sheetName, Workbook wb, int titleNum, int dataStartNum)
|
|
|
+ public List<T> importExcel(Long fileId, Workbook wb, String sheetName, int titleNum, int dataStartNum)
|
|
|
throws Exception {
|
|
|
this.type = Type.IMPORT;
|
|
|
List<T> list = new ArrayList<>();
|
|
|
@@ -375,133 +377,156 @@ public class ExcelUtil<T> {
|
|
|
}
|
|
|
// 获取最后一个非空行的行下标,比如总行数为n,则返回的为n-1
|
|
|
int rows = sheet.getLastRowNum();
|
|
|
+ if (rows == 0) {
|
|
|
+ return list;
|
|
|
+ }
|
|
|
|
|
|
- if (rows > 0) {
|
|
|
- // 定义一个map用于存放excel列的序号和field.
|
|
|
- Map<String, Integer> cellMap = new HashMap<>();
|
|
|
- Map<Integer, String> cellMapTmp = new HashMap<>();
|
|
|
- // 获取表头
|
|
|
- for (int hRow = titleNum; hRow < dataStartNum; hRow++) {
|
|
|
- Row heard = sheet.getRow(hRow);
|
|
|
- for (int i = 0; i < heard.getPhysicalNumberOfCells(); i++) {
|
|
|
- Cell cell = heard.getCell(i);
|
|
|
- if (StringUtils.isNotNull(cell)) {
|
|
|
- String value = this.getCellValue(heard, i).toString().replaceAll("\n", "");
|
|
|
- if (cellMapTmp.containsKey(i)) {
|
|
|
- if(StringUtils.isEmpty(value)) {
|
|
|
- value = cellMapTmp.get(i);
|
|
|
- } else {
|
|
|
- value = cellMapTmp.get(i).replaceAll("`", "") + value;
|
|
|
- }
|
|
|
- }
|
|
|
- if (StringUtils.isEmpty(value) && i > 0) {
|
|
|
- value = cellMapTmp.get(i - 1) + "`";
|
|
|
- }
|
|
|
- cellMapTmp.put(i, value);
|
|
|
- }
|
|
|
- }
|
|
|
+ Map<Integer, Object[]> fieldsMap = getFieldsMap(sheet, titleNum, dataStartNum);
|
|
|
+ for (int i = dataStartNum; i <= rows; i++) {
|
|
|
+ // 从第2行开始取数据,默认第一行是表头.
|
|
|
+ Row row = sheet.getRow(i);
|
|
|
+ // 判断当前行是否是空行
|
|
|
+ if (isRowEmpty(row)) {
|
|
|
+ continue;
|
|
|
}
|
|
|
- for (Map.Entry<Integer, String> entry : cellMapTmp.entrySet()) {
|
|
|
- if(cellMap.containsKey(entry.getValue())){
|
|
|
- continue;
|
|
|
+ T entity = null;
|
|
|
+ for (Map.Entry<Integer, Object[]> entry : fieldsMap.entrySet()) {
|
|
|
+ // 如果不存在实例则新建.
|
|
|
+ entity = (entity == null ? clazz.newInstance() : entity);
|
|
|
+ // 从map中得到对应列的field.
|
|
|
+ Field field = (Field) entry.getValue()[0];
|
|
|
+ Excel attr = (Excel) entry.getValue()[1];
|
|
|
+ Object val = convert(entry, field, row);
|
|
|
+ String propertyName = field.getName();
|
|
|
+ if (StringUtils.isNotEmpty(attr.targetAttr())) {
|
|
|
+ propertyName = field.getName() + "." + attr.targetAttr();
|
|
|
+ } else if (StringUtils.isNotEmpty(attr.readConverterExp())) {
|
|
|
+ val = reverseByExp(Convert.toStr(val), attr.readConverterExp(), attr.separator());
|
|
|
+ } else if (StringUtils.isNotEmpty(attr.dictType())) {
|
|
|
+ val = reverseDictByExp(Convert.toStr(val), attr.dictType(), attr.separator());
|
|
|
+ } else if (!attr.handler().equals(ExcelHandlerAdapter.class)) {
|
|
|
+ val = dataFormatHandlerAdapter(val, attr);
|
|
|
+ } else if (ColumnType.IMAGE == attr.cellType() && StringUtils.isNotEmpty(pictures)) {
|
|
|
+ PictureData image = pictures.get(row.getRowNum() + "_" + entry.getKey());
|
|
|
+ if (image == null) {
|
|
|
+ val = "";
|
|
|
+ } else {
|
|
|
+ byte[] data = image.getData();
|
|
|
+ val = FileUtils.writeImportBytes(data);
|
|
|
+ }
|
|
|
}
|
|
|
- cellMap.put(entry.getValue(), entry.getKey());
|
|
|
+ ReflectUtils.invokeSetter(entity, propertyName, val);
|
|
|
}
|
|
|
- // 有数据时才处理 得到类的所有field.
|
|
|
- List<Object[]> fields = this.getFields();
|
|
|
- Map<Integer, Object[]> fieldsMap = new HashMap<>();
|
|
|
- for (Object[] objects : fields) {
|
|
|
- Excel attr = (Excel) objects[1];
|
|
|
- Integer column = cellMap.get(attr.name());
|
|
|
- if (column != null) {
|
|
|
- fieldsMap.put(column, objects);
|
|
|
+ if (null != entity) {
|
|
|
+ if(StringUtils.isNotEmpty(fileId)) {
|
|
|
+ setBaseValue(entity, fileId);
|
|
|
}
|
|
|
+ list.add(entity);
|
|
|
}
|
|
|
- for (int i = dataStartNum; i <= rows; i++) {
|
|
|
- // 从第2行开始取数据,默认第一行是表头.
|
|
|
- Row row = sheet.getRow(i);
|
|
|
- // 判断当前行是否是空行
|
|
|
- if (isRowEmpty(row)) {
|
|
|
- continue;
|
|
|
- }
|
|
|
- T entity = null;
|
|
|
- for (Map.Entry<Integer, Object[]> entry : fieldsMap.entrySet()) {
|
|
|
- Object val = this.getCellValue(row, entry.getKey());
|
|
|
-
|
|
|
- // 如果不存在实例则新建.
|
|
|
- entity = (entity == null ? clazz.newInstance() : entity);
|
|
|
- // 从map中得到对应列的field.
|
|
|
- Field field = (Field) entry.getValue()[0];
|
|
|
- Excel attr = (Excel) entry.getValue()[1];
|
|
|
- // 取得类型,并根据对象类型设置值.
|
|
|
- Class<?> fieldType = field.getType();
|
|
|
- if (String.class == fieldType) {
|
|
|
- String s = Convert.toStr(val);
|
|
|
- if (StringUtils.endsWith(s, ".0")) {
|
|
|
- val = StringUtils.substringBefore(s, ".0");
|
|
|
+ }
|
|
|
+ return list;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void setBaseValue(T entity, Long fileId){
|
|
|
+ ReflectUtils.invokeSetter(entity, "delFlag", DelFlagEnum.NOT_DELETE.getCode());
|
|
|
+ ReflectUtils.invokeSetter(entity, "fileId", fileId);
|
|
|
+ ReflectUtils.invokeSetter(entity, "createTime", LocalDateTime.now());
|
|
|
+ ReflectUtils.invokeSetter(entity, "createBy", SecurityUtils.getUsername());
|
|
|
+ }
|
|
|
+
|
|
|
+ private Map<Integer, Object[]> getFieldsMap(Sheet sheet, int titleNum, int dataStartNum) {
|
|
|
+ // 定义一个map用于存放excel列的序号和field.
|
|
|
+ Map<Integer, String> cellMapTmp = new HashMap<>();
|
|
|
+ // 获取表头
|
|
|
+ for (int hRow = titleNum; hRow < dataStartNum; hRow++) {
|
|
|
+ Row heard = sheet.getRow(hRow);
|
|
|
+ for (int i = 0; i < heard.getPhysicalNumberOfCells(); i++) {
|
|
|
+ Cell cell = heard.getCell(i);
|
|
|
+ if (StringUtils.isNotNull(cell)) {
|
|
|
+ String value = this.getCellValue(heard, i).toString().replaceAll("\n", "");
|
|
|
+ if (cellMapTmp.containsKey(i)) {
|
|
|
+ if (StringUtils.isEmpty(value)) {
|
|
|
+ value = cellMapTmp.get(i);
|
|
|
} else {
|
|
|
- String dateFormat = field.getAnnotation(Excel.class).dateFormat();
|
|
|
- if (StringUtils.isNotEmpty(dateFormat)) {
|
|
|
- val = parseDateToStr(dateFormat, val);
|
|
|
- } else {
|
|
|
- val = Convert.toStr(val);
|
|
|
- }
|
|
|
- }
|
|
|
- } else if ((Integer.TYPE == fieldType || Integer.class == fieldType)
|
|
|
- && StringUtils.isNumeric(Convert.toStr(val))) {
|
|
|
- val = Convert.toInt(val);
|
|
|
- } else if ((Long.TYPE == fieldType || Long.class == fieldType) && StringUtils.isNumeric(
|
|
|
- Convert.toStr(val))) {
|
|
|
- val = Convert.toLong(val);
|
|
|
- } else if (Double.TYPE == fieldType || Double.class == fieldType) {
|
|
|
- val = Convert.toDouble(val);
|
|
|
- } else if (Float.TYPE == fieldType || Float.class == fieldType) {
|
|
|
- val = Convert.toFloat(val);
|
|
|
- } else if (BigDecimal.class == fieldType) {
|
|
|
- val = Convert.toBigDecimal(val);
|
|
|
- } else if (Date.class == fieldType || LocalDate.class == fieldType) {
|
|
|
- if (val instanceof String) {
|
|
|
- String dateFormat = field.getAnnotation(Excel.class).dateFormat();
|
|
|
- if (StringUtils.isNotEmpty(dateFormat) && dateFormat.equals("MMdd")) {
|
|
|
- val = String.valueOf(LocalDate.now().getYear()) + val;
|
|
|
- }
|
|
|
- val = LocalDateUtil.parseDate(val);
|
|
|
- } else if (val instanceof Double) {
|
|
|
- val = DateUtil.getJavaDate((Double) val);
|
|
|
+ value = cellMapTmp.get(i).replaceAll("`", "") + value;
|
|
|
}
|
|
|
- if (null == val) {
|
|
|
- val = getCellDateValue(row, entry.getKey());
|
|
|
- }
|
|
|
- if (LocalDate.class == fieldType) {
|
|
|
- val = LocalDateUtil.parseLocalDate(val);
|
|
|
- }
|
|
|
- } else if (Boolean.TYPE == fieldType || Boolean.class == fieldType) {
|
|
|
- val = Convert.toBool(val, false);
|
|
|
}
|
|
|
- String propertyName = field.getName();
|
|
|
- if (StringUtils.isNotEmpty(attr.targetAttr())) {
|
|
|
- propertyName = field.getName() + "." + attr.targetAttr();
|
|
|
- } else if (StringUtils.isNotEmpty(attr.readConverterExp())) {
|
|
|
- val = reverseByExp(Convert.toStr(val), attr.readConverterExp(), attr.separator());
|
|
|
- } else if (StringUtils.isNotEmpty(attr.dictType())) {
|
|
|
- val = reverseDictByExp(Convert.toStr(val), attr.dictType(), attr.separator());
|
|
|
- } else if (!attr.handler().equals(ExcelHandlerAdapter.class)) {
|
|
|
- val = dataFormatHandlerAdapter(val, attr);
|
|
|
- } else if (ColumnType.IMAGE == attr.cellType() && StringUtils.isNotEmpty(pictures)) {
|
|
|
- PictureData image = pictures.get(row.getRowNum() + "_" + entry.getKey());
|
|
|
- if (image == null) {
|
|
|
- val = "";
|
|
|
- } else {
|
|
|
- byte[] data = image.getData();
|
|
|
- val = FileUtils.writeImportBytes(data);
|
|
|
- }
|
|
|
+ if (StringUtils.isEmpty(value) && i > 0) {
|
|
|
+ value = cellMapTmp.get(i - 1) + "`";
|
|
|
}
|
|
|
- ReflectUtils.invokeSetter(entity, propertyName, val);
|
|
|
+ cellMapTmp.put(i, value);
|
|
|
}
|
|
|
- list.add(entity);
|
|
|
}
|
|
|
}
|
|
|
- return list;
|
|
|
+ Map<String, Integer> cellMap = new HashMap<>();
|
|
|
+ for (Map.Entry<Integer, String> entry : cellMapTmp.entrySet()) {
|
|
|
+ if (cellMap.containsKey(entry.getValue())) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ cellMap.put(entry.getValue(), entry.getKey());
|
|
|
+ }
|
|
|
+ // 有数据时才处理 得到类的所有field.
|
|
|
+ List<Object[]> fields = this.getFields();
|
|
|
+ Map<Integer, Object[]> fieldsMap = new HashMap<>();
|
|
|
+ for (Object[] objects : fields) {
|
|
|
+ Excel attr = (Excel) objects[1];
|
|
|
+ Integer column = cellMap.get(attr.name());
|
|
|
+ if (column != null) {
|
|
|
+ fieldsMap.put(column, objects);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return fieldsMap;
|
|
|
+ }
|
|
|
+
|
|
|
+ private Object convert(Map.Entry<Integer, Object[]> entry, Field field, Row row) {
|
|
|
+ Object val = this.getCellValue(row, entry.getKey());
|
|
|
+ // 取得类型,并根据对象类型设置值.
|
|
|
+ Class<?> fieldType = field.getType();
|
|
|
+ if (String.class == fieldType) {
|
|
|
+ String s = Convert.toStr(val);
|
|
|
+ if (StringUtils.endsWith(s, ".0")) {
|
|
|
+ val = StringUtils.substringBefore(s, ".0");
|
|
|
+ } else {
|
|
|
+ String dateFormat = field.getAnnotation(Excel.class).dateFormat();
|
|
|
+ if (StringUtils.isNotEmpty(dateFormat)) {
|
|
|
+ val = parseDateToStr(dateFormat, val);
|
|
|
+ } else {
|
|
|
+ val = Convert.toStr(val);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else if ((Integer.TYPE == fieldType || Integer.class == fieldType)
|
|
|
+ && StringUtils.isNumeric(Convert.toStr(val))) {
|
|
|
+ val = Convert.toInt(val);
|
|
|
+ } else if ((Long.TYPE == fieldType || Long.class == fieldType) && StringUtils.isNumeric(
|
|
|
+ Convert.toStr(val))) {
|
|
|
+ val = Convert.toLong(val);
|
|
|
+ } else if (Double.TYPE == fieldType || Double.class == fieldType) {
|
|
|
+ val = Convert.toDouble(val);
|
|
|
+ } else if (Float.TYPE == fieldType || Float.class == fieldType) {
|
|
|
+ val = Convert.toFloat(val);
|
|
|
+ } else if (BigDecimal.class == fieldType) {
|
|
|
+ val = Convert.toBigDecimal(val);
|
|
|
+ } else if (Date.class == fieldType || LocalDate.class == fieldType) {
|
|
|
+ if (val instanceof String) {
|
|
|
+ String dateFormat = field.getAnnotation(Excel.class).dateFormat();
|
|
|
+ if (StringUtils.isNotEmpty(dateFormat) && dateFormat.equals("MMdd")) {
|
|
|
+ val = String.valueOf(LocalDate.now().getYear()) + val;
|
|
|
+ }
|
|
|
+ val = LocalDateUtil.parseDate(val);
|
|
|
+ } else if (val instanceof Double) {
|
|
|
+ val = DateUtil.getJavaDate((Double) val);
|
|
|
+ }
|
|
|
+ if (null == val) {
|
|
|
+ val = getCellDateValue(row, entry.getKey());
|
|
|
+ }
|
|
|
+ if (LocalDate.class == fieldType) {
|
|
|
+ val = LocalDateUtil.parseLocalDate(val);
|
|
|
+ }
|
|
|
+ } else if (Boolean.TYPE == fieldType || Boolean.class == fieldType) {
|
|
|
+ val = Convert.toBool(val, false);
|
|
|
+ }
|
|
|
+
|
|
|
+ return val;
|
|
|
}
|
|
|
|
|
|
/**
|