Oracle applications - Surendranath Subramani: OAF export data from VO in xlsx format

Monday, March 16, 2015

OAF export data from VO in xlsx format

Posted: 16-Mar-2015

Purpose:

In this article we are going to see how to export view object in Microsoft office excel xlsx format

To export with xlsx format few basic things needed are
  1. Jar files (List of jar i've used is shown in below screen shot)

For better understanding I’ll illustrated steps to follow.

Sample Workspace and the jar files can be downloaded at the end of the article

Steps to create data module to be used in the page:


a.       Create table
create table suren_employee (emplno number, empl_name varchar2(100),LAST_UPDATE_DATE          DATE,LAST_UPDATED_BY           NUMBER (15),LAST_UPDATE_LOGIN         NUMBER (15),CREATION_DATE             DATE,CREATED_BY                NUMBER (15));

b.      Insert data to table
insert into suren_employee (emplno,empl_name) values (10, 'SURENDRANATH');
insert into suren_employee (emplno,empl_name) values (20, 'SUBRAMANI');

Steps to design the page:

  1. Create project
  2. Create EO
  3. Create VO
  4. Create application module
  5. Create page
  6. Assign AM to the page
  7. Create a item with style as ‘exportButton’
  8. Set controller on the page layout
  9. Create button in the page layout

After following above steps your project should look like this

















Copy paste below code in the controller:


/* This is a sample code which should be used in the controller
writeXLSXFile is the method which will export data
Call writeXLSXFile method in the PFR (Process Form Request)
*/

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.http.HttpServletResponse;

import oracle.apps.fnd.common.VersionInfo;
import oracle.apps.fnd.framework.OAViewObject;
import oracle.apps.fnd.framework.webui.OAControllerImpl;
import oracle.apps.fnd.framework.webui.OAPageContext;
import oracle.apps.fnd.framework.webui.beans.OAWebBean;

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import suren.oracle.apps.ak.server.SurenEmployeeVORowImpl;
import oracle.jbo.domain.Number ;

  public void processFormRequest(OAPageContext pageContext, OAWebBean webBean)
  {
    super.processFormRequest(pageContext, webBean);
    if(pageContext.getParameter("Download")!=null)
                    {
                      try{
                        System.out.println("Before writeXLSXFile");
                        writeXLSXFile(pageContext, "SurenEmployeeVO1");
                        System.out.println("After writeXLSXFile");
                      }catch(IOException e){
                        e.printStackTrace();
                      }
                        
                    }  
  }

  public  void writeXLSXFile(OAPageContext pageContext,String view_inst_name) throws IOException {
     

    OAViewObject vo = (OAViewObject) pageContext.getRootApplicationModule().findViewObject(view_inst_name);           
    String sheetName = "Export code comb";//name of sheet
    HttpServletResponse response = (HttpServletResponse) pageContext.getRenderingContext().getServletResponse();
    
    // Set excel property

    response.setHeader("Header-Name", "Codecombination");
    response.setHeader("Content-Length", "100000");
    response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
    response.setHeader("Content-Disposition","attachment; filename=" + "SurenEmp" + ".xlsx");
    response.setHeader("Cache-Control","max-age=0");

    XSSFWorkbook wb = new XSSFWorkbook();
    XSSFSheet sheet = wb.createSheet(sheetName);
    
    // Create header
    
    XSSFRow row12 = sheet.createRow(0);

        XSSFCell celh0 = row12.createCell(0);
        celh0.setCellValue("Employee Number");      
        XSSFCell celh1 = row12.createCell(1);
        celh1.setCellValue("Employee Name");        

    int f=1;
        for(SurenEmployeeVORowImpl row = (SurenEmployeeVORowImpl) vo.first(); row != null;row = (SurenEmployeeVORowImpl) vo.next())
    {
    // Create line
        XSSFRow row1 = sheet.createRow(f);

                 for (int i = 0; i < vo.getAttributeCount(); i++)
                 {
                         
                         if (i==0) {
                         XSSFCell cell = row1.createCell(i);
                         Number empno = (Number)row.getEmplno();
                         String empno1=empno.toString();

                           cell.setCellValue(empno1);
                          System.out.println(row.getEmplno());
                         }
                         if (i==1) {
                         XSSFCell cell = row1.createCell(i);
                         cell.setCellValue(row.getEmplName());    // Dept
                         }
                   }
        System.out.println("row2:" + f);
        f=f+1;
    }



    ByteArrayOutputStream outByteStream = new ByteArrayOutputStream();
    wb.write(outByteStream);
    byte[] outArray = outByteStream.toByteArray();
    response.setContentLength(outArray.length);
    ServletOutputStream outStream = response.getOutputStream();
    outStream.write(outArray);
    outStream.flush();
    outStream.close();
  }

You will see errors in the controller saying missing “XSSFCell not found” etc.
To fix this go to your project property and add poi library to the project.


















Download workspace:

12 comments:

  1. Thanks dude. It helped me a lot

    ReplyDelete
  2. Hi Surendra,
    I appreciate your work. I have tried to export 500rows but it gives error like "You have encountered an unexpected error. " Kindly provide your valuable suggestion on this.

    ReplyDelete
    Replies
    1. Please add exception to the code. That may help to figure out the problem.

      Delete
  3. Dear Suri,

    Lot of thanks to ur quick reply.

    I added the exception to code, now am getting java.lang.NullPointerException. But in OAF Page 500 + Records are showing in table format successfully with out any exception, when i press the export button am getting the excel sheet but when am trying to open the excel sheet it shows the Error Page in sheet. I Couldn't post the screen shot. If you want in detail kindly provide your email ID.

    ReplyDelete
  4. Dear Suri,

    Thx I find out the problem, Now am getting the sheet with 1500+ records.

    ReplyDelete
    Replies
    1. Great news.
      Can you provide the solution for the benefit of others?

      Delete
  5. I didn't handle the Null Pointer Exception.When I handled the exception am getting the complete records.

    ReplyDelete
  6. Hi Suri,
    When i deployed into apps server the Excel Sheet not showing the rows, it gives the error "You have encountered an unexpected error. Please contact the System Administrator for assistance." Kindly help on this.

    ReplyDelete
    Replies
    1. The file was not created properly, hence the error. Make sure there are enough data is written.
      Also turn on log.

      Delete
  7. Hi Suri. When you upload the OAF Page to ebs application Server. How you references the poi library?. You modify the s_jto_classpath(Classpath to be used by the Oracle Fulfillment Server) to include references to poi jar?.
    Cheers

    ReplyDelete
    Replies
    1. you need to ftp the library on to the application server and add entry in orion-application.xml file and bounce the application.

      Delete