#
Junjie
2024-08-19 18147c6baeb59f3427eba5904614e924a6a72785
zy-asrs-wms/src/main/java/com/zy/asrs/wms/utils/ExcelUtil.java
@@ -2,21 +2,22 @@
import com.zy.asrs.framework.common.Cools;
import com.zy.asrs.wms.asrs.entity.MatField;
import com.zy.asrs.wms.asrs.entity.template.OrderTemplate;
import io.swagger.annotations.ApiModelProperty;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
 * Created by vincent on 2/17/2024
@@ -52,7 +53,7 @@
            }
            String memo = "未知";
            if (field.isAnnotationPresent(ApiModelProperty.class)) {
                memo = field.getAnnotation(ApiModelProperty.class).value();
                memo = field.getAnnotation(ApiModelProperty.class).value() + "[" + field.getName() + "]";
            }
            header.createCell(headerIdx).setCellValue(memo);
            headerIdx++;
@@ -61,7 +62,7 @@
        //动态字段
        if(dynamicFields != null){
            for (MatField field : dynamicFields) {
                header.createCell(headerIdx).setCellValue(field.getDescribe());
                header.createCell(headerIdx).setCellValue(field.getDescribe() + "[" + field.getName() + "]");
                headerIdx++;
            }
        }
@@ -125,6 +126,91 @@
        return workbook;
    }
    public static <T> List<T> parseExcelFile(MultipartFile file, Class<T> clz) {
        List<T> dataList = new ArrayList<>();
        try (Workbook workbook = new HSSFWorkbook(file.getInputStream())) {
            ArrayList<HashMap<String, Object>> list = new ArrayList<>();
            ArrayList<String> fields = new ArrayList<>();
            Sheet sheet = workbook.getSheetAt(0);
            for (Row row : sheet) {
                HashMap<String, Object> map = new HashMap<>();
                if (!fields.isEmpty()) {
                    for (String field : fields) {
                        map.put(field, null);
                    }
                    list.add(map);
                }
                for (Cell cell : row) {
                    if (cell.getRowIndex() == 0) {
                        Pattern pattern = Pattern.compile("\\[(.*?)\\]");
                        Matcher matcher = pattern.matcher(cell.getStringCellValue());
                        while (matcher.find()) {
                            fields.add(matcher.group(1));
                        }
                    }else {
                        String value = "";
                        if(cell.getCellType() == CellType.NUMERIC.getCode()) {
                            // 先将数字转换为字符串
                            value = String.valueOf(cell.getNumericCellValue());
                            // 处理字符串value
                        } else if(cell.getCellType() == CellType.STRING.getCode()) {
                            // 直接获取字符串值
                            value = cell.getStringCellValue();
                            // 处理字符串value
                        }
                        map.put(fields.get(cell.getColumnIndex()), value);
                    }
                }
            }
            for (HashMap<String, Object> map : list) {
                T t = ExcelUtil.parseData(clz, map);
                dataList.add(t);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return dataList;
    }
    public static <T> T parseData(Class<T> clz, Map<String, Object> map) {
        try {
            T newInstance = clz.newInstance();
            Field[] fields = Cools.getAllFields(clz);
            for (Field field : fields) {
                field.setAccessible(true);
                if (!map.containsKey(field.getName())) {
                    if (Modifier.isTransient(field.getModifiers()) && field.getName().equals("dynamicFields")) {
                        Method method = clz.getMethod("syncFieldMap", Map.class);
                        method.invoke(newInstance, map);
                    }
                    continue;
                }
                if (map.get(field.getName()) == null) {
                    continue;
                }
                // 判断字段类型并赋值
                if (field.getType() == String.class) {
                    field.set(newInstance, String.valueOf(map.get(field.getName())));
                } else if (field.getType() == Integer.class) {
                    field.set(newInstance, Integer.parseInt(map.get(field.getName()).toString()));
                } else if (field.getType() == Float.class) {
                    field.set(newInstance, Float.parseFloat(map.get(field.getName()).toString()));
                } else if (field.getType() == Double.class) {
                    field.set(newInstance, Double.parseDouble(map.get(field.getName()).toString()));
                } else if (field.getType() == Boolean.class) {
                    field.set(newInstance, Boolean.parseBoolean(map.get(field.getName()).toString()));
                }
            }
            return newInstance;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}