I am trying to create Object from Excel. The excel file looks like this
I am trying to follow this idea from this post How to convert my xlsx sheet to java object using Apache POI. The very 1st answer looks good. However I am little lost on the part on how to create the object.
public class Tr {
String empName;
String empID;
String empDept;
//Constructor
public Tr(String empName, String empID, String empDept) {
this.empName = empName;
this.empID = empID;
this.empDept = empDept;
}
//The following part will read Excel and return cell data
public static ArrayList<String> name = new ArrayList<String>();
public static ArrayList<String> deptId = new ArrayList<String>();
public static ArrayList<String> dName = new ArrayList<String>();
public ArrayList<String> getCellData(int cellNo) throws IOException {
FileInputStream file = new FileInputStream("C:\\MyTemp\\People.xlsx");
HSSFWorkbook book = new HSSFWorkbook(file);
HSSFSheet sheet = book.getSheet("Sheet1");
Iterator<Row> it = sheet.iterator();
ArrayList<String> cellData = new ArrayList<String>();
while (it.hasNext()) {
cellData.add(it.next().getCell(cellNo).getStringCellValue());
}
return cellData;
}
//Assigning cell data to variables and converting to string
public void assignEmployee() throws IOException {
empName = getCellData(0).toString();
empID = getCellData(1).toString();
empDept = getCellData(2).toString();
}
public static void main(String[] args) {
}
}
Your help or ideas will be appreciated. Thanks.