[英]how to iterate first columns then rows to read excel sheet in java
i want to read an excel sheet so i want to read my 1st column data and iterate all rows then 2nd column so how to do it. 我想阅读一个Excel工作表,所以我想阅读我的第一列数据,然后依次遍历所有行和第二列,以便进行操作。
i have tried using row iteration first then followed by coloumn 我已经尝试过先使用行迭代,然后再使用coloumn
Iterator < Row > rowIterator = sheetName.iterator();
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
Iterator < Cell > cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
}
}
If i understand you question properly then you are trying to iterate over column first then row . 如果我理解您的问题正确,那么您将尝试首先遍历列,然后遍历row。 This is what you might be trying to do.
这就是您可能想做的。 Hope it helps :
希望能帮助到你 :
Sheet sheet = workbook.getSheetAt(0);
for(int i=0; i<sheet.getRow(sheet.getTopRow()).getLastCellNum(); i++){
Iterator<Row> rowIterator=sheet.rowIterator();
while(rowIterator.hasNext()){
System.out.println(rowIterator.next().getCell(i)); //do what you want
}
}
If I understand the question, you want to read the data from excel column-wise. 如果我理解这个问题,则想从excel逐列读取数据。 ie read the entire Column1 and then move to Column2 and so on.
即读取整个Column1,然后移至Column2,依此类推。
If that is your requirement, Please find for the code snippet in java. 如果这是您的要求,请在Java中找到代码片段。
// Getting List of Column Headers in the sheet and saving the same to a list
int masterSheetColumnIndex = sheet.getColumns();
List<String> ExpectedColumns = new ArrayList<String>();
for (int x = 0; x < masterSheetColumnIndex; x++) {
Cell celll = sheet.getCell(x, 0);
String d = celll.getContents();
System.out.println("d : "+d);
ExpectedColumns.add(d);
}
columnDataValues = new LinkedHashMap<String, List<String>>();
List<String> column1 = new ArrayList<String>();
// read values from excel sheet for each column
for (int j = 0; j < masterSheetColumnIndex; j++) {
column1 = new ArrayList<String>();
for (int i = 1; i < sheet.getRows()-7; i++) {
Cell cell = sheet.getCell(j, i);
column1.add(cell.getContents());
System.out.println("cell.getContents() : "+cell.getContents());
}
columnDataValues.put(ExpectedColumns.get(j), column1);
}
Please revert in case of any queries in above code. 如果以上代码中有任何查询,请还原。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.