-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExcelReader_Latest.java
87 lines (73 loc) · 2.15 KB
/
ExcelReader_Latest.java
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
83
84
85
86
87
package excelReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ExcelReader_Latest {
File file = null;
FileInputStream fin = null;
XSSFWorkbook workbook = null;
XSSFSheet sheet = null;
public HashMap<String, String> getData(String excelPath, int sheetNumber, String testCaseId) {
HashMap<String, String> dict = new HashMap<String, String>();
file = new File(excelPath);
if (!file.exists()) {
System.out.println("File Doesn't Exists!");
}
try {
fin = new FileInputStream(file);
workbook = new XSSFWorkbook(fin);
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
} catch (IOException e) {
System.out.println(e.getMessage());
} catch (Exception e) {
System.out.println(e.getMessage());
} finally {
if (fin != null) {
try {
fin.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
sheet = workbook.getSheetAt(sheetNumber);
DataFormatter dataFormatter = new DataFormatter();
Row row = null;
for (int rowIndex = 0; rowIndex <= sheet.getLastRowNum(); rowIndex++) {
row = sheet.getRow(rowIndex);
if (row != null) {
Cell first = row.getCell(0);
if (first != null) {
String firstCellValue = dataFormatter.formatCellValue(first);
if (firstCellValue.equals(testCaseId)) {
break;
}
}
}
}
if (row != null) {
for (int colIndex = 0; colIndex <= row.getLastCellNum(); colIndex++) {
if (sheet.getRow(0).getCell(colIndex) == null || row.getCell(colIndex) == null) {
break;
}
String key = dataFormatter.formatCellValue(sheet.getRow(0).getCell(colIndex));
String value = dataFormatter.formatCellValue(row.getCell(colIndex));
dict.put(key, value);
}
}
try {
workbook.close();
} catch (IOException e) {
System.out.println(e.getMessage());
}
return dict;
}
}