ADF : How to Export data from excel(.csv) to ADF table ?
public void fileUploaded(ValueChangeEvent valueChangeEvent) {
// Add event code here...
UploadedFile file = (UploadedFile)valueChangeEvent.getNewValue();
try {
AdfFacesContext.getCurrentInstance().addPartialTarget(staffTable);
parseFile(file.getInputStream());
} catch (IOException e) {
// TODO add more
}
}
public void parseFile(java.io.InputStream file) {
BufferedReader reader = new BufferedReader(new InputStreamReader(file));
String strLine = "";
StringTokenizer st = null;
int lineNumber = 0, tokenNumber = 0;
Row rw = null;
CollectionModel _tableModel = (CollectionModel)staffTable.getValue();
//the ADF object that implements the CollectionModel is JUCtrlHierBinding. It
//is wrapped by the CollectionModel API
JUCtrlHierBinding _adfTableBinding =
(JUCtrlHierBinding)_tableModel.getWrappedData();
//Acess the ADF iterator binding that is used with ADF table binding
DCIteratorBinding it =
_adfTableBinding.getDCIteratorBinding();
//read comma separated file line by line
try
{
while ((strLine = reader.readLine()) != null)
{
lineNumber++;
// create a new row skip the header (header has linenumber 1)
if (lineNumber>1) {
rw = it.getNavigatableRowIterator().createRow();
rw.setNewRowState(Row.STATUS_INITIALIZED);
it.getNavigatableRowIterator().insertRow(rw);
}
//break comma separated line using ","
st = new StringTokenizer(strLine, ",");
while (st.hasMoreTokens())
{
//display csv values
tokenNumber++;
String theToken = st.nextToken();
System.out.println("Line # " + lineNumber + ", Token # " +
tokenNumber +
", Token : " + theToken);
if (lineNumber>1){
switch (tokenNumber) {
case 1: rw.setAttribute("Id", theToken);
case 2: rw.setAttribute("FirstName", theToken);
case 3: rw.setAttribute("LastName", theToken);
case 4: rw.setAttribute("EmailAddress", theToken);
}
}
}
//reset token number
tokenNumber = 0;
}
}
catch (IOException e) {
// TODO add more
FacesContext fctx = FacesContext.getCurrentInstance();
fctx.addMessage(staffTable.getClientId(fctx), new FacesMessage(FacesMessage.SEVERITY_ERROR,
"Content Error in Uploaded file", e.getMessage()));
}
catch (Exception e) {
FacesContext fctx = FacesContext.getCurrentInstance();
fctx.addMessage( null, new FacesMessage(FacesMessage.SEVERITY_ERROR,
"Data Error in Uploaded file", e.getMessage()));
}
}