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..

  1. public void downloadExcel(FacesContext facesContext, OutputStream outputStream) {  
  2. try{  
  3. BindingContext bindingctx=BindingContext.getCurrent();  
  4. BindingContainer binding=bindingctx.getCurrentBindingsEntry();  
  5. DCBindingContainer bindingsImpl = (DCBindingContainer) binding;  
  6. DCIteratorBinding dciter = bindingsImpl.findIteratorBinding(“EmployeesView1Iterator”);  
  7. ViewObject vo_rep=dciter.getViewObject();  
  8. OutputStreamWriter w = new OutputStreamWriter(outputStream);  
  9. int childCnt=t1.getChildCount();  
  10. w.append(” Employee Detail”);  
  11. w.append(“”);  
  12. w.append(“”);  
  13. w.append(“”);  
  14. RichColumn col=null;  
  15. List c=new ArrayList();  
  16. c=t1.getChildren();  
  17. for(int i=0;i<c.size();i++)  
  18. {  
  19. if(i==0)  
  20. continue;  
  21. col = (RichColumn)c.get(i);  
  22. w.append(“”);  
  23. w.append(col.getHeaderText());  
  24. w.append(“”);  
  25. }  
  26. w.append(“”);  
  27. Row r=null;  
  28. for(int i=0;i<vo_rep.getRowCount();i++)  
  29. {  
  30. boolean flag=false;  
  31. if (i == 0)  
  32. r=vo_rep.first();  
  33. else  
  34. r=vo_rep.next();  
  35. if(r.getAttribute(“Mark”)!=null)  
  36. {  
  37. flag=(Boolean)r.getAttribute(“Mark”);  
  38. }  
  39. if(flag==true)  
  40. {  
  41. w.append(“”);  
  42. if(r.getAttribute(“EmployeeId”) != null)  
  43. w.append(“” + r.getAttribute(“EmployeeId”) + “”);  
  44. else  
  45. w.append(“”);  
  46. if(r.getAttribute(“FirstName”) != null)  
  47. w.append(“” + r.getAttribute(“FirstName”) + “”);  
  48. else  
  49. w.append(“”);  
  50. if(r.getAttribute(“LastName”) != null)  
  51. w.append(“” + r.getAttribute(“LastName”) + “”);  
  52. else  
  53. w.append(“”);  
  54. if(r.getAttribute(“Email”) != null)  
  55. w.append(“” + r.getAttribute(“Email”) + “”);  
  56. else  
  57. w.append(“”);  
  58. if(r.getAttribute(“PhoneNumber”) != null)  
  59. w.append(“” + r.getAttribute(“PhoneNumber”) + “”);  
  60. else  
  61. w.append(“”);  
  62. if(r.getAttribute(“Salary”) != null)  
  63. {  
  64. w.append(“” + r.getAttribute(“Salary”) + “”);  
  65. }  
  66. }  
  67. }  
  68. w.close();  
  69. }  
  70. catch(Exception e) {  
  71. System.out.println(“excelExportMethod raised exception:” + e.getMessage());  
  72. e.printStackTrace();  
  73. }  
  74. }  

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

You may also like:

  • pic5
  • pic1
  • pic2
  • pic3
  • pic4

About Rafat Sidiqqui

Leave a Reply