我在获取特定字符串的行号时遇到了一些麻烦。假设我在第9行有一个单元格内容=“HC”并使用方法findRow(sheet,“HC”)。我应该得到9的返回值,但现在返回值为0.
excelSheetFinder函数的目的是在从findRow()返回行号之后,我可以从我输入的特定列中获取单元格作为参数(enCol和frCol)。
感谢任何帮助!
public static String[] excelSheetFinder(String value, int enCol, int frCol) throws IOException{
String[] shortNames = new String[2];
File excelFile = new File("excel.xlsx");
FileInputStream inputExcelFile = new FileInputStream(excelFile);
XSSFWorkbook wb = new XSSFWorkbook(inputExcelFile);
XSSFSheet sheet = wb.getSheetAt(0);
int shortNameRow = findRow(sheet, value);
System.out.println(shortNameRow);
Row r = sheet.getRow(shortNameRow);
String enCell = r.getCell(enCol).toString();
String frCell = r.getCell(frCol).toString();
shortNames[0] = enCell;
shortNames[1] = frCell;
return shortNames;
}
private static int findRow(XSSFSheet sheet, String value){
//to find row number so we can search through that specific column
int gotRow = 0;
for (Row row: sheet){
for (Cell cell: row){
final DataFormatter df = new DataFormatter();
final XSSFCell cellVal = (XSSFCell) row.getCell(row.getRowNum());
String valueAsString = df.formatCellValue(cellVal);
if (valueAsString.trim() == value){
gotRow = row.getRowNum();
}
}
}
return gotRow;
}
答案 0 :(得分:0)
将==
替换为.equals
进行字符串比较:
if (valueAsString.trim() == value)
==
测试参考相等性
.equals()
测试价值平等