16 October, 2012

OAF : Upload Excel File to Database

I want to allow user to upload excel file in database from OAF page.

Suppose that excel file contains below columns

Suppose also that I have Entity Object named XxxEmpEO and I created View Object XxxEmpVO based on previous entity object which has below attributes

I added new item in page of type messageFileUpload ["uploadExcelFile" ] and Button ["uploadButton"].
If user click a button, I will upload excel file that is entered in messageFileUpload item to Entity Object and then commit changes to database.
Idea of Implementation
I will read file as byte stream and then loop for every line in file and insert it in view object.
Now I should handle action of upload button, I will write my code in ProcessFromRequest method in Controller Class

   public void processFromRequest(OAPageContext oapagecontext,   
                   OAWebBean oawebbean) {  
     super.processFormRequest(oapagecontext, oawebbean);  
     OAApplicationModule am = oapagecontext.getApplicationModule(oawebbean);  
     OAViewObjectImpl empVO =   
     if (oapagecontext.getParameter("uploadButton") != null) {  
       //Get Data of uploaded Excel file  
       DataObject excelUploadData =   
       //Declare Variable that will be used in reading uploaded file  
       String fileName = null;  
       String contentType = null;  
       Long fileCapacity = null;  
       BlobDomain uploadStream = null;  
       BufferedReader inReader = null;  
       try {  
         fileName =   
             (String)excelUploadData.selectValue(null, "UPLOAD_FILE_NAME");  
         contentType =   
             (String)excelUploadData.selectValue(null, "UPLOAD_FILE_MIME_TYPE");  
         uploadStream =   
             (BlobDomain)excelUploadData.selectValue(null, fileName);  
         inReader =   
             new BufferedReader(new InputStreamReader(uploadStream.getBinaryStream()));  
         fileCapacity = new Long(uploadStream.getLength());  
       } catch (NullPointerException ex) {  
         throw new OAException("Please Select an Excel File to Upload it to Database!!!",   
       try {  
         String wholeLine = "";  
         long counter = 0;  
         String[] seperatedCells;  
         while (((wholeLine = inReader.readLine()) != null)) {  
           //Split the deliminated data and  
           if (wholeLine.trim().length() > 0) {  
             //split whole line to cells  
             seperatedCells = wholeLine.split(",");  
             //Print the current line in Console  
             System.out.println(" line #" + counter + " - " +   
                       seperatedCells[0] + " - " +   
                       seperatedCells[1] + " - " +   
             Row row = empVO.createRow();  
             row.setAttribute("EmpNo", seperatedCells[0]);  
             row.setAttribute("EmpName", seperatedCells[1]);  
             row.setAttribute("Job ", seperatedCells[2]);  
       } catch (IOException e) {  
         throw new OAException(e.getMessage(), OAException.ERROR);  
       throw new OAException("Excel File Uploaded SuccessFully!!!",   


OAF :Formatting DateTime Fields

Use the following code to format DateTime feilds in OAF OAWebBean departureDateBean = webBean.findChildRecursive("DepartureDate"...