[英]Using apache poi how to read a specific excel rows and column
嗨,我想在第一行中搜索一個字符串,如果找到了字符串,那么我想移動該列。
for (int i = 0; i < 1; i++) {
Row row = firstSheet.getRow(i);
for (int j = 0; j < row.getPhysicalNumberOfCells(); j++) {
String rtm=row.getCell(j).getStringCellValue();
if(rtm.contains(text)){
System.out.println(row.getCell(j).getStringCellValue()+"|| ");
}
}
}
問題出在您的第一行:
for (int i = 0; i < 1; i++) {
這只會運行一次循環,這就是為什么只獲得第一行!
對於非字符串單元格,例如數字單元格,您的代碼也會失敗。
我建議您花一些時間閱讀關於遍歷行和單元格的Apache POI文檔 。 然后,您可能想要更多類似的東西:
System.out.println("Searching for cells containing '" + text + "'");
DataFormatter formatter = new DataFormatter();
for (Sheet sheet : wb ) {
for (Row row : sheet) {
for (Cell cell : row) {
String value = formatter.formatCellValue(cell);
if (value.contains(text)) {
System.out.println("Found at " + (new CellReference(cell)).formatAsString() + ":");
System.out.println(value);
}
}
}
}
這將處理非字符串單元格,並為您提供如下輸出:
Searching for cells containing 'needle'
Found at A5
This is a needle
Found at B10
A needle can be helpful
然后根據需要進行調整!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.