[英]Java - Excel read multiple row data
I need to read excel file which more than 300 rows. 我需要读取超过300行的excel文件。 I need to extract value from particular cell only.
我只需要从特定细胞中提取价值。
Code 码
Workbook workbook = Workbook.getWorkbook(new File(excelFile));
Sheet sheet = workbook.getSheet(0);
Cell emp_name = sheet.getCell(1,2);
Cell emp_dpt = sheet.getCell(2,2);
Cell emp_pdpt = sheet.getCell(3,2);
Cell emp_no = sheet.getCell(4,2);
Cell emp_desn = sheet.getCell(5,2);
Cell emp_dj = sheet.getCell(6,2);
Cell emp_lvl = sheet.getCell(7,2);
Cell emp_eval = sheet.getCell(8,2);
String name = emp_name.getContents();
String dpartment = emp_dpt.getContents();
String pre_department = emp_pdpt.getContents();
String employee_no = emp_no.getContents();
String designation = emp_desn.getContents();
String datejoined = emp_dj.getContents();
String evalution = emp_eval.getContents();
System.out.println(name);
System.out.println(dpartment);
System.out.println(pre_department);
System.out.println(employee_no);
System.out.println(designation);
System.out.println(datejoined);
System.out.println(evalution);
Above code helps me to fetch data from the excel but only one value extracted. 上面的代码帮助我从excel中获取数据,但只提取了一个值。 How do I fetch all the data from excel.
如何从excel中获取所有数据。
you can use iterator
that allows you to read each and every cell 您可以使用允许您读取每个单元格的
iterator
see example here 看这里的例子
Iterator<Row> rowIterator = sheet.iterator();
while(rowIterator.hasNext()) {
Row row = rowIterator.next();
//For each row, iterate through each columns
Iterator<Cell> cellIterator = row.cellIterator();
while(cellIterator.hasNext()) {
Cell cell = cellIterator.next();
switch(cell.getCellType()) {
case Cell.CELL_TYPE_BOOLEAN:
System.out.print(cell.getBooleanCellValue() + "\t\t");
break;
case Cell.CELL_TYPE_NUMERIC:
System.out.print(cell.getNumericCellValue() + "\t\t");
break;
case Cell.CELL_TYPE_STRING:
System.out.print(cell.getStringCellValue() + "\t\t");
break;
}
}
Do it in a loop 在循环中做它
When you say , sheet.getCell(1,2), you read the cell with column 1 and row 2. 当你说,sheet.getCell(1,2)时,你读取了第1列和第2行的单元格。
Support if you want to read , column 1 and row 3, then do this sheet.getCell(1,3); 支持,如果你想阅读,第1列和第3行,然后执行此sheet.getCell(1,3);
sheet.getRows() ->Returns the number of rows in this sheet sheet.getRows() - >返回此工作表中的行数
pseudo code: 伪代码:
for (int rowNum = 5; rowNum < sheet.getRows(); rowNum++) {
int column = 4;
sheet.getCell(column ++, rowNum).getContents(); //read 4th column and 5th row into a variable or object as per your logic;
sheet.getCell(column ++, rowNum).getContents(); //read 5th column and 5th row;
......
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.