如果不该成List
程序就会一直有警告
如何修改才是最符合规范的呢?
public List<T> excelToList(String filePath, Class<T> cls) {
//----------把这里的List<T>改成List-这段代码就可以正常执行了------------------------------
List<T> result = new ArrayList<>();
try {
Workbook wb = new XSSFWorkbook(filePath);
int sheetLength = wb.getNumberOfSheets();
for (int i = 0; i < sheetLength; i++) {
Sheet sheet = wb.getSheetAt(i);
int rowLength = sheet.getPhysicalNumberOfRows();
for (int j = 0; j < rowLength; j++) {
T t = cls.newInstance();
ComExcel comExcel = (ComExcel) t;
Row row = sheet.getRow(j);
int cellLength = row.getPhysicalNumberOfCells();
for (int k = 0; k < cellLength; k++) {
Cell cell = row.getCell(k);
String cellValue = cell.getStringCellValue();
Row title = sheet.getRow(0);
Cell titleCell = title.getCell(k);
String titleCellValue = titleCell.getStringCellValue();
Field[] fields = Emp.class.getDeclaredFields();
for (int l = 0; l < fields.length; l++) {
Field field = fields[l];
ExcelField ef = field.getDeclaredAnnotation(ExcelField.class);
String anTitle = ef.title();
if (titleCellValue.equals(anTitle)) {
Field[] declaredFields = Emp.class.getDeclaredFields();
for (Field f : declaredFields) {
ExcelField declaredAnnotation = f.getDeclaredAnnotation(ExcelField.class);
String extitle = declaredAnnotation.title();
if (extitle.equals(titleCellValue)) {
String fName = f.getName();
fName = firstToUpper(fName);
comExcel.getClass().getMethod("set" + fName, new Class[] { String.class })
.invoke(comExcel, new Object[] { cellValue });
}
}
continue;
}
}
}
//--------comExcel无法放入result-无法编译---------------------
result.add(comExcel);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}