优化这段代码 public static LinkedHashMap<String, String> excelToShopIdList(InputStream inputStream) { LinkedHashMap<String, String> res = new LinkedHashMap<>(); try { Workbook sheets = WorkbookFactory.create(inputStream); inputStream.close(); Sheet sheetAt = sheets.getSheetAt(0); // 获取设备编码 res.put("deviceCode", sheetAt.getSheetName()); int lastRowNum = sheetAt.getLastRowNum() + 1; // 标识 从该行开始解析 String[] ar = new String[]{"name", "base", "decimal", "description", "disable", "mult"}; int isTrueNum = 0; for (int i = 0; i < lastRowNum; i++) { Row row = sheetAt.getRow(i); // 空行直接跳过 if (ObjectUtils.isEmpty(row)) { isTrueNum = 0; continue; } // isTrueNum < 6 说明 和标识行没有完全匹配 if (isTrueNum < 6) { isTrueNum = 0; //和 标识行进行匹配 for (int j = 0; j < ar.length; j++) { if (ObjectUtils.isNotEmpty(row.getCell(j) ) && row.getCell(j).getCellType().equals(CellType.STRING) && ar[j].equals(row.getCell(j).getStringCellValue())) { isTrueNum++; continue; } } } else { Cell nameCell = row.getCell(0); Cell desCell = row.getCell(3); Cell typeCell = row.getCell(13); if (ObjectUtils.isEmpty(nameCell)) { continue; } if (CellType.STRING.equals(nameCell.getCellType()) && StringUtils.isEmpty(nameCell.getStringCellValue())) { continue; } // 结束标识行 if ("var_name".equals(nameCell.getStringCellValue())) { break; } res.put(nameCell.getStringCellValue(), desCell.getStringCellValue()); res.put(nameCell.getStringCellValue()+"_type#", typeCell.getStringCellValue()); } } } catch (IOException e) { e.printStackTrace(); } return res; }
时间: 2024-02-05 21:02:44 浏览: 21
这段代码可以进行如下优化:
1. 使用try-with-resources语句,自动关闭输入流。
2. 可以将Sheet对象的获取和解析逻辑单独抽出来作为一个方法,提高代码的可读性和可维护性。
3. 使用常量来代替魔法值,提高代码的可读性。
4. 使用StringUtils.isNotBlank()代替ObjectUtils.isNotEmpty()和StringUtils.isNotEmpty(),代码更简洁。
改进后的代码如下:
```
private static final String[] COLUMN_NAMES = {"name", "base", "decimal", "description", "disable", "mult"};
private static final String VAR_NAME_COLUMN_NAME = "var_name";
private static final int COLUMN_NAME_INDEX = 0;
private static final int DESCRIPTION_INDEX = 3;
private static final int TYPE_INDEX = 13;
public static LinkedHashMap<String, String> excelToShopIdList(InputStream inputStream) {
LinkedHashMap<String, String> res = new LinkedHashMap<>();
try (Workbook workbook = WorkbookFactory.create(inputStream)) {
Sheet sheet = workbook.getSheetAt(0);
String deviceCode = sheet.getSheetName();
res.put("deviceCode", deviceCode);
int lastRowIndex = sheet.getLastRowNum() + 1;
int matchedColumnCount = 0;
for (int i = 0; i < lastRowIndex; i++) {
Row row = sheet.getRow(i);
if (row == null) {
matchedColumnCount = 0;
continue;
}
if (matchedColumnCount < COLUMN_NAMES.length) {
matchedColumnCount = matchColumnNames(row);
} else {
Cell nameCell = row.getCell(COLUMN_NAME_INDEX);
if (nameCell == null || StringUtils.isBlank(nameCell.getStringCellValue())) {
continue;
}
if (VAR_NAME_COLUMN_NAME.equals(nameCell.getStringCellValue())) {
break;
}
Cell descriptionCell = row.getCell(DESCRIPTION_INDEX);
Cell typeCell = row.getCell(TYPE_INDEX);
res.put(nameCell.getStringCellValue(), descriptionCell.getStringCellValue());
res.put(nameCell.getStringCellValue() + "_type#", typeCell.getStringCellValue());
}
}
} catch (IOException e) {
e.printStackTrace();
}
return res;
}
private static int matchColumnNames(Row row) {
int matchedColumnCount = 0;
for (int j = 0; j < COLUMN_NAMES.length; j++) {
Cell cell = row.getCell(j);
if (cell != null && cell.getCellType() == CellType.STRING && COLUMN_NAMES[j].equals(cell.getStringCellValue())) {
matchedColumnCount++;
}
}
return matchedColumnCount;
}
```