-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathExcelReader.java
More file actions
39 lines (34 loc) · 1021 Bytes
/
ExcelReader.java
File metadata and controls
39 lines (34 loc) · 1021 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import java.io.FileInputStream;
import java.util.Iterator;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
public class ExcelReader {
public static Object[][] readExcel() throws Exception{
Object array[][] = new Object[4][3];
FileInputStream fi = new FileInputStream("D:\\Java2-5WS\\Data.xls");
HSSFWorkbook workBook = new HSSFWorkbook(fi);
HSSFSheet sheet = workBook.getSheetAt(0);
Iterator<Row> rows = sheet.rowIterator();
int row = 0;
boolean isFirstTimeLoop = false;
while(rows.hasNext()){
Row currentRow = rows.next();
if(!isFirstTimeLoop){
isFirstTimeLoop = true;
continue;
}
Iterator<Cell> cells = currentRow.cellIterator();
int col = 0;
while(cells.hasNext()){
Cell currentCell = cells.next();
int value = (int)currentCell.getNumericCellValue();
array[row][col]=value;
col++;
}
row++;
}
return array;
}
}