我需要创建一个循环,每次使用不同的rowno值。以下是代码的一部分:
int rowno = 2;
File file = new File("path");
Workbook workbook = Workbook.getWorkbook(file);
String value = workbook.getSheet(0).getCell(1, rowno).getContents();
下一行编号为11,20,29等等......
答案 0 :(得分:4)
使用您需要检查的行声明数组,列表或集合:
int[] rowsToCheck = { 11, 20, 29 };
使用值迭代遍历数组:
for (int i = 0; i < rowsToCheck.length; i++) {
String value = workbook.getSheet(0).getCell(1, rowsToCheck[i]).getContents();
System.out.println(value);
}
更新:
如果数字是连续的11 + 9 = 20 + 9 = 29 + 9 ......建议Tarik,只需将其设为循环增量为9。
int numberOfRows = // what you neeed
for (int i = 0; i < numberOfRows; i+=9) {
String value = workbook.getSheet(0).getCell(1, i).getContents();
System.out.println(value);
}
答案 1 :(得分:2)
也许我错过了一些东西,但下面的代码应该这样做:
int rowno;
int lastrow=100; // Correct as needed
File file = new File("path");
Workbook workbook = Workbook.getWorkbook(file);
for (rowno=2; rowno<lastrow; rowno+=9) {
String value = workbook.getSheet(0).getCell(1, rowno).getContents();
}
答案 2 :(得分:1)
您必须获取excel文件中每行的行数。获得工作表后,您知道最大行数,然后迭代并增加9。
Sheet currentSheet = workbook.getSheetAt(int index);
for (int rowno = 2; rowno < currentSheet.getPhysicalNumberOfRows(); rowno = rowno + 9)
{
String value = currentSheet.getCell(1, rowno).getContents();
}
答案 3 :(得分:0)
for (int i = 2; i < maxValue; i = i + 9) {
}