Saturday 29 June 2013

Export Selected Rows into Excel File

1)Create ViewObject
 eg: EmpVO
Query:Select empno,ename,sal from scott.emp

2)Create Table by Region Using Wizard or manually with multiple Selection Option.
3)Create Transient attribute in EmpVO for Multiple Selection Option.i.e,xxselect type:String.
4)Create button  or Submit Button any where depend on The Requirement .
   Id:ExportBtn
  Prompt:Export
  Event:Export
5)Create a Controller under PageLayoutRN.

Write Below Code

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


  public void processRequest(OAPageContext pageContext, OAWebBean webBean)
  {
    super.processRequest(pageContext, webBean);
       EmpVOImpl vo=(EmpVOImpl )pageContext.getApplicationModule(webBean).findViewObject("EmpVO1");
    vo.executeQuery();
  }

  public void processFormRequest(OAPageContext pageContext, OAWebBean webBean)
  {
    super.processFormRequest(pageContext, webBean);
    if("Export".equals(pageContext.getParameter(EVENT_PARAM)))
    {
    EmpVOImpl vo = (EmpVOImpl)pageContext.getApplicationModule(webBean).findViewObject("EmpVO1");
      EmpVORowImpl row=(EmpVORowImpl)vo.first();
      Row[] row1=vo.getFilteredRows("xxselect","Y");
    try
    {

    HttpServletResponse response = (HttpServletResponse) pageContext.getRenderingContext().getServletResponse();
    response.setContentType("application/txt");
    response.setHeader("Content-Disposition","attachment; filename=" + "Employee Trouble Call Report" + ".csv");
    PrintWriter pw = null;
    pw = response.getWriter();

    //Writing the headers
    pw.write("Emploee Number");
    pw.write(",");
    pw.write("Employee Name");
    pw.write(",");
    pw.write("Salary");  
    pw.write("\n");
    // getting table data
    Row row2=null;
    System.out.println(row1.length);
       for(int i=0;i<row1.length;i++)//Read Selected Rows
       {
       row2=row1[i];
       String dept_name= row2.getAttribute("Empno").toString();
        String emp_name=row2.getAttribute("Ename").toString();
        String Salary=row2.getAttribute("Sal").toString();
         if(dept_name !=null) // for null handling in the code
         {
         pw.write(dept_name);
         }
         else
         {
         pw.write(" ");
         }
//      emp_name = emp_name.replaceAll(",",".");
           pw.write(",");
           if(emp_name!=null)
           {
          pw.write(emp_name);
           }
           else
           {
             pw.write("");
           }
         pw.write(",");//next cell
         if(Salary!=null)
         {
         pw.write(Salary);
         }
         else
         {
           pw.write("");
         }
           pw.write("\n");//next line
       }  
    pw.write(" ");
    pageContext.setDocumentRendered(false); //mandatory
    pw.flush(); //exporting data
    pw.close();

    }
    catch(Exception e)
    {
    e.printStackTrace();
    }
    }
  }
}