🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
/** * 复制sour里属性不为空的值到obje为空的属性 * * @param obje * 目标实体类 * @param sour * 源实体类 * @param isCover * 是否保留obje类里不为null的属性值(true为保留源值,属性为null则赋值) * @return obje */ public static Object Copy(Object obje, Object sour, boolean isCover) { Field[] fields = sour.getClass().getDeclaredFields(); for (int i = 0, j = fields.length; i < j; i++) { String propertyName = fields[i].getName(); Object propertyValue = getProperty(sour, propertyName); if (isCover) { if (getProperty(obje, propertyName) == null && propertyValue != null) { Object setProperty = setProperty(obje, propertyName, propertyValue); } } else { Object setProperty = setProperty(obje, propertyName, propertyValue); } } return obje; } /** * 给bean赋值 * * @param bean * @param propertyName * @param value * @return */ private static Object setProperty(Object bean, String propertyName, Object value) { Class clazz = bean.getClass(); try { Field field = clazz.getDeclaredField(propertyName); Method method = clazz.getDeclaredMethod( getSetterName(field.getName()), new Class[] { field.getType() }); return method.invoke(bean, new Object[] { value }); } catch (Exception e) { } return null; } /** * 得到值 * * @param bean * @param propertyName * @return */ private static Object getProperty(Object bean, String propertyName) { Class clazz = bean.getClass(); try { Field field = clazz.getDeclaredField(propertyName); Method method = clazz.getDeclaredMethod( getGetterName(field.getName()), new Class[] {}); return method.invoke(bean, new Object[] {}); } catch (Exception e) { } return null; } /** * 得到setter方法 * * @param propertyName * 变量名 * @return */ private static String getSetterName(String propertyName) { // String method = "set" + propertyName.substring(0, 1).toUpperCase() + // propertyName.substring(1); String method; if (propertyName.length() > 1 && Character.isUpperCase(propertyName.charAt(1))) { method = "set" + propertyName; } else { method = "set" + propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1); } return method; } /** * 根据变量名得到get方法 * * @param propertyName * @return */ private static String getGetterName(String propertyName) { String method; if (propertyName.length() > 1 && Character.isUpperCase(propertyName.charAt(1))) { method = "get" + propertyName; } else { method = "get" + propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1); } return method; } public static Object getCellFormatValue(Cell cell) { Object cellValue = null; if (cell != null) { // 判断cell类型 switch (cell.getCellType()) { case NUMERIC: { cellValue = String.valueOf(cell.getNumericCellValue()); break; } case FORMULA: { // 判断cell是否为日期格式 if (DateUtil.isCellDateFormatted(cell)) { // 转换为日期格式YYYY-mm-dd // cellValue = cell.getDateCellValue(); cellValue = new SimpleDateFormat("yyyy-MM-dd").format(cell .getDateCellValue()); } else { // 数字 cellValue = String.valueOf(cell.getNumericCellValue()); } break; } case STRING: { cellValue = cell.getRichStringCellValue().getString(); break; } default: cellValue = ""; } } else { cellValue = ""; } return cellValue; } /** * 获得取值位置单元格。 * * @param sh * @param point * @param labColIndex * @param labRowIndex * @param currentrow * @return */ private static Cell getCell(Sheet sh, String point, int labColIndex, int labRowIndex, int currentrow) { int colIndex = -1; int rowIndex = -1; if (point.equalsIgnoreCase("N")) { colIndex = labColIndex; rowIndex = currentrow; } if (point.equalsIgnoreCase("U")) { colIndex = labColIndex; rowIndex = labRowIndex - 2; } if (point.equalsIgnoreCase("D")) { colIndex = labColIndex; rowIndex = labRowIndex; } if (point.equalsIgnoreCase("L")) { colIndex = labColIndex - 1; rowIndex = labRowIndex - 1; } if (point.equalsIgnoreCase("R")) { colIndex = labColIndex + 1; rowIndex = labRowIndex - 1; } if (point.equalsIgnoreCase("C")) { colIndex = labColIndex; rowIndex = labRowIndex - 1; } Row row = sh.getRow(rowIndex); if (row != null) { return row.getCell(colIndex); } return null; } public static Object getMergedRegionValue(Sheet sheet, int row, int column) { int sheetMergeCount = sheet.getNumMergedRegions(); for (int i = 0; i < sheetMergeCount; i++) { CellRangeAddress ca = sheet.getMergedRegion(i); int firstColumn = ca.getFirstColumn(); int lastColumn = ca.getLastColumn(); int firstRow = ca.getFirstRow(); int lastRow = ca.getLastRow(); if (row >= firstRow && row <= lastRow) { if (column >= firstColumn && column <= lastColumn) { Row fRow = sheet.getRow(firstRow); Cell fCell = fRow.getCell(firstColumn); return CellUtil.getCellValue(fCell, true); } } } return null; }