Wednesday 18 September 2013

Export Data from Excel file to DataBase tables

Step 1: Create FileUpload Item  in Page
            Id:FileUploadItem
            Prompt:FileUpload
            DataType:Blob
Step 2:Create  a Submit Button
           Id:Go
          Prompt:Go
Step 3:Create a submit Button
           Id:Update
           Prompt:Update
           Event:update
Step 4:Write Following Code in controller i.e, attached to a page.

 import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import oracle.apps.fnd.common.VersionInfo;
import oracle.apps.fnd.framework.OAApplicationModule;
import oracle.apps.fnd.framework.OAException;
import oracle.apps.fnd.framework.server.OAViewObjectImpl;
import oracle.apps.fnd.framework.webui.OAControllerImpl;
import oracle.apps.fnd.framework.webui.OAPageContext;
import oracle.apps.fnd.framework.webui.beans.OAWebBean;

import oracle.cabo.ui.data.DataObject;

import oracle.jbo.Row;
import oracle.jbo.domain.BlobDomain;

/**
 * Controller for ...
 */
public class FileUploadCO extends OAControllerImpl
{
  public static final String RCS_ID="$Header$";
  public static final boolean RCS_ID_RECORDED =
        VersionInfo.recordClassVersion(RCS_ID, "%packagename%");

  /**
   * Layout and page setup logic for a region.
   * @param pageContext the current OA page context
   * @param webBean the web bean corresponding to the region
   */
  public void processRequest(OAPageContext pageContext, OAWebBean webBean)
  {
    super.processRequest(pageContext, webBean);
  }

  /**
   * Procedure to handle form submissions for form elements in
   * a region.
   * @param pageContext the current OA page context
   * @param webBean the web bean corresponding to the region
   */
  public void processFormRequest(OAPageContext pageContext, OAWebBean webBean)
  {
    super.processFormRequest(pageContext, webBean);
    OAApplicationModule am=(OAApplicationModule)pageContext.getApplicationModule(webBean);
    OAViewObjectImpl vo = (OAViewObjectImpl) am.findViewObject("DemoVO1");
    if (pageContext.getParameter("Go")!=null)
    {
    DataObject fileUploadData =(DataObject)pageContext.getNamedDataObject("FileUploadItem");
    String fileName = null;
    String contentType = null;
    Long fileSize = null;
    BlobDomain uploadedByteStream = null;
    BufferedReader in = null;
     
    try
    {
    fileName = (String)fileUploadData.selectValue(null, "UPLOAD_FILE_NAME");
    contentType =(String)fileUploadData.selectValue(null, "UPLOAD_FILE_MIME_TYPE");
    uploadedByteStream = (BlobDomain)fileUploadData.selectValue(null, fileName);
    in = new BufferedReader(new InputStreamReader(uploadedByteStream.getBinaryStream()));   
     fileSize = new Long(uploadedByteStream.getLength()); 
    System.out.println("fileSize"+fileSize);
    }
    catch(NullPointerException ex)
    {
    throw new OAException("Please Select a File to Upload", OAException.ERROR);
    }
    try{     
    //Open the CSV file for reading  
    String lineReader="";

    long t =0;
     
     
    String[] linetext;
     
     
    while (((lineReader = in.readLine()) !=null) ){
     
     
    //Split the deliminated data and
    if (lineReader.trim().length()>0)
    {
    System.out.println("lineReader"+lineReader.length());
    linetext = lineReader.split(",");
    t++;  
    if(t>1)
{
    if (!vo.isPreparedForExecution()) {
    vo.setMaxFetchSize(0);
    vo.executeQuery();
    }
    Row row = vo.createRow();
    System.out.println("Column1->"+linetext[0]);
    row.setAttribute("VendorName", linetext[0]);
    row.setAttribute("Segment1",linetext[1]);
    row.setAttribute("VendorId",linetext[2]);
    
    vo.last();
    vo.next();
    vo.insertRow(row);
}
    }
     }
    }
     
     
    catch (IOException e)
    {
     e.printStackTrace();
    }
    }
    if ("update".equals(pageContext.getParameter(EVENT_PARAM))) {
    am.getOADBTransaction().commit();
    throw new OAException("Uploaded SuccessFully",OAException.CONFIRMATION);
     
     
    }
    }
    }