forked from Madusudanan/Selenium
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreadExcel.java
More file actions
82 lines (68 loc) · 2.3 KB
/
Copy pathreadExcel.java
File metadata and controls
82 lines (68 loc) · 2.3 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
//Imports
//Make sure you have the POI Jar library in your build path
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
public class readExcel
{
public static void readFile(String path) throws IOException {
// Path to the excel file
File datafile = new File(path);
// A Buffered File Input Stream to read the data
InputStream fis = new BufferedInputStream(new FileInputStream(datafile));
// We create a workbook which represents the excel file
HSSFWorkbook book = new HSSFWorkbook(fis);
// Next a sheet which represents the sheet within that excel file
HSSFSheet sheet = book.getSheet("Details");
// No of rows in the sheet
int rowNum = sheet.getLastRowNum() + 1;
// No of columns in the sheet
int colNum = sheet.getRow(0).getLastCellNum();
// A Two dimensional array of Strings which represents the data in the
// sheet
String[][] data = new String[rowNum][colNum];
for (int i = 0; i < rowNum; i++) {
// Get the row
HSSFRow row = sheet.getRow(i);
for (int j = 0; j < colNum; j++) {
// Get the columns or cells for the first row and keep looping
// for the other rows
HSSFCell cell = row.getCell(j);
// Make a call to the method cellToString which actually
// converts the cell contents to String
String value = cellToString(cell);
data[i][j] = value;
// Here is where you write the logic to handle the data.I am
// just printing out the contents here.
System.out.println("The value is " + value);
}
}
}
private static String cellToString(HSSFCell cell) {
Object result;
switch (cell.getCellType()) {
case Cell.CELL_TYPE_NUMERIC:
result = cell.getNumericCellValue();
break;
case Cell.CELL_TYPE_STRING:
result = cell.getStringCellValue();
break;
case Cell.CELL_TYPE_BOOLEAN:
result = cell.getBooleanCellValue();
break;
case Cell.CELL_TYPE_FORMULA:
result = cell.getCellFormula();
break;
default:
throw new RuntimeException("Unknown Cell Type");
}
return result.toString();
}
}