[英]Read Excel sheet with Apache POI without header using for loop
I want to read Excel file without a header using for loop and want to use that data to pass to another method.我想使用 for 循环读取没有 header 的 Excel 文件,并希望使用该数据传递给另一个方法。 I'm unable to do so.
我不能这样做。 Without header works fine.
没有 header 工作正常。
@DataProvider(name = "inputData")
public static Object[][] getExcelData() throws IOException {
inputStream = new FileInputStream(Constants.TEST_DATA);
workbook = new XSSFWorkbook(inputStream);
sheet = workbook.getSheet("CRMData");
int rowCount = sheet.getLastRowNum() + 1;
int columnCount = sheet.getRow(0).getLastCellNum();
Object[][] data = new Object[rowCount][columnCount];
for (int i = 0; i < rowCount; i++) {
row = sheet.getRow(i);
for (int j = 0; j < columnCount; j++) {
cell = row.getCell(j);
data[i][j] = cell.getStringCellValue();
}
}
workbook.close();
return data;
}
I expect this method to fetch only test data, not the header, and pass that data to my test case for execution.我希望此方法仅获取测试数据,而不是 header,并将该数据传递给我的测试用例以供执行。
You mean as in: 您的意思是:
for (int i = 1; i < rowCount; i++) {
row = sheet.getRow(i);
for (int j = 0; j < columnCount; j++) {
cell = row.getCell(j);
data[i - 1][j] = cell.getStringCellValue();
}
}
That is, start from the second row? 也就是说,从第二行开始?
If you want to skip header means top row of your excel file initialize value of i for the for loop with 1 which is responsible to traverse within rows.如果你想跳过 header 意味着你的 excel 文件的第一行初始化 i 的 for 循环值 1 负责在行内遍历。 Top row used to have index value 0. So your for loop code for row and columns should be like this.
顶行曾经有索引值 0。所以你的行和列的 for 循环代码应该是这样的。
Object[][] data = new Object[rowCount][columnCount];
for (int i = 1; i < rowCount; i++) {
XSSFRow row = sheet.getRow(i);
for (int j = 0; j < columnCount; j++) {
XSSFCell cell = row.getCell(j);
data[i][j] = cell.getStringCellValue();
System.out.println(data[i][j]);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.