-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReadExcel.java
More file actions
56 lines (51 loc) · 1.91 KB
/
ReadExcel.java
File metadata and controls
56 lines (51 loc) · 1.91 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
package com.demoqa.dataUtile;
import java.io.File;
import java.io.IOException;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ReadExcel {
public static String[][] getExcelData() {
// String fileLocation = "./test-data/Login-data.xlsx"; //file location, (./
// default project folder)
String fileLocation = new File("./test-data/Login-data.xlsx").getAbsolutePath();
XSSFWorkbook wbook = null;
try {
wbook = new XSSFWorkbook(fileLocation);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} // workbook access or loading the file
XSSFSheet sheet = wbook.getSheetAt(0); // access sheet
int lastRowNum = sheet.getLastRowNum();
int numberOfRows = sheet.getPhysicalNumberOfRows();
System.out.println("No.of Rows with header: " + numberOfRows);
System.out.println("No.of Rows: " + lastRowNum);
short lastCellNum = sheet.getRow(0).getLastCellNum(); // to get column number
System.out.println("No.of cells: " + lastCellNum);
String[][] data = new String[lastRowNum][lastCellNum];
for (int i = 1; i <= lastRowNum; i++) {
XSSFRow row = sheet.getRow(i); // row
for (int j = 0; j < lastCellNum; j++) {
XSSFCell cell = row.getCell(j); // column
DataFormatter dft = new DataFormatter();
String value = dft.formatCellValue(cell); // class to read numeric and string
// System.out.println(value);
data[i - 1][j] = value;
/*
* String value = cell.getStringCellValue(); // value System.out.println(value);
* // printing the value
*/
}
}
try {
wbook.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return data;
}
}