lundi 6 avril 2015

Unmarsahll Excel file using Reflection

I am doing some automation tests and we are using Excel files for Data Driven testing. I would like to create a utility to unmarshall an Excel file into a Java object, similar to how JAXB works. What I would like is a custom Java annotation , like @ExcelColumn(name = "name"), then based on the mapping I would give that excel file path to a method to call the setters based on corresponding column names.


For example:


Excel File:


firstName lastName


James Bond


John Doe


Java Object:



public class Person {
@ExcelColumn(name = "firstName")
private String firstName;
@ExcelColumn(name = "lastName")
private String lastName;


//getters and setters

}


Parse Excel:



List<Person> people = ExcelUnmarshaller.unmarshall("C:\people.xls", Person.class);
//people.get(0).getFirstName would equal "James"


Does anyone know of a utility like this wrapped around Apache Poi and/or does anyone know exactly how I could do this? I would have to know how I can call the setter on a property and then how I could do non string types, like if it is an integer, and then how I could go about doing nested objects. (I think I could use dot notation in excel column, to denote child object, example: person.address.streetName)


Thanks for any help






Aucun commentaire:

Enregistrer un commentaire