Home » ADF » ADF Tuts: Export selected table rows to Excel
ADF Tuts: Export selected table rows to Excel

ADF Tuts: Export selected table rows to Excel

You create an application using the application creation wizard.

Step 1:- Create an Entity Object using your Schema.

Step 2:- Create a Boolean transient attribute in EO.

Step 3:- Create a view object using that EO.

Step 4:- When you create a view object, a data control will appears in appModuleDataControl select that VO and        Drag it on your page as a read only table.

Step 5:- Drag a command button in your page.

pic1

Step6:- Select ‘File Download Action Listener’ from component pallet and drop on command button.

pic2

Step 7:-After dropping go to the file download action listener property.

There are some properties of this component.

contantType:-Here you can give the type of your file. Like for Excel ‘xls’ and for PDF ‘pdf’

FileName-Here you can give the file name.

Method- Here you can describe the method.

pic3

Now go to your manage bean and create a method and add it your File download action listener’s Method property.

Code is given below..

public void downloadExcel(FacesContext facesContext, OutputStream outputStream) {
try{
BindingContext bindingctx=BindingContext.getCurrent();
BindingContainer binding=bindingctx.getCurrentBindingsEntry();
DCBindingContainer bindingsImpl = (DCBindingContainer) binding;
DCIteratorBinding dciter = bindingsImpl.findIteratorBinding("EmployeesView1Iterator");
ViewObject vo_rep=dciter.getViewObject();
OutputStreamWriter w = new OutputStreamWriter(outputStream);
int childCnt=t1.getChildCount();
w.append(" Employee Detail");
w.append("");
w.append("");
w.append("");
RichColumn col=null;
List c=new ArrayList();
c=t1.getChildren();
for(int i=0;i<c.size();i++)
{
if(i==0)
continue;
col = (RichColumn)c.get(i);
w.append("");
w.append(col.getHeaderText());
w.append("");
}
w.append("");
Row r=null;
for(int i=0;i<vo_rep.getRowCount();i++)
{
boolean flag=false;
if (i == 0)
r=vo_rep.first();
else
r=vo_rep.next();
if(r.getAttribute("Mark")!=null)
{
flag=(Boolean)r.getAttribute("Mark");
}
if(flag==true)
{
w.append("");
if(r.getAttribute("EmployeeId") != null)
w.append("" + r.getAttribute("EmployeeId") + "");
else
w.append("");
if(r.getAttribute("FirstName") != null)
w.append("" + r.getAttribute("FirstName") + "");
else
w.append("");
if(r.getAttribute("LastName") != null)
w.append("" + r.getAttribute("LastName") + "");
else
w.append("");
if(r.getAttribute("Email") != null)
w.append("" + r.getAttribute("Email") + "");
else
w.append("");
if(r.getAttribute("PhoneNumber") != null)
w.append("" + r.getAttribute("PhoneNumber") + "");
else
w.append("");
if(r.getAttribute("Salary") != null)
{
w.append("" + r.getAttribute("Salary") + "");
}
}
}
w.close();
}
catch(Exception e) {
System.out.println("excelExportMethod raised exception:" + e.getMessage());
e.printStackTrace();
}
}

Step 8:- Finally run your Project your Excel will be download.

pic4

On the click of Export to Excel button you can download excel sheet of selected record.

pic5

About rafat

3 comments

  1. Dear,

    int childCnt=t1.getChildCount();

    what is t1 here?

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>