Exporting to Excel from any ADF Table

No matter how fancy the application you develop, no matter how sophisticated the user interfaces: end users will always clamor for Excel. All data eventually will end up in some Excel Spreadsheet it seems. And grudgingly or not, we will of course oblige them. Depending on how difficult it actually is to achieve such Excel export functionality.

Well, frankly, using the Apache POI library and the ADF Model, it turns out to be very simple. In fact, it turns out to be very simple to create a generic Table To Excel Export facility, that we can apply to any Table (binding) in our application in a matter of mere minutes.
Exporting to Excel from any ADF Table
Note: the foundation for this solution was laid in Frank Nimphius’ Weblog article ADF Faces: Exporting table data to Excel from ADF Faces/ADF (http://www.orablogs.com/fnimphius/archives/001881.html ) which itself was based on work done by Jakub Pawlowski. We will see a refined version of their work, one that is not dependent on ADF BC as Business Service, that is generic for all Table Bindings and that allows the user control over which columns should be in the Excel Spreadsheet as well as which rows.....

We can add a generic dialog that pops up when the Export to Excel button has been pressed in the Table Header and that allows the user to:

  • Specify the name of the Excel Spreadsheet
  • Specify which rows to export (the ones displayed in the table, all rows or a selected range)
  • Indicate which columns to include in the spreadsheet, including ones not shown in the table on screen, and in which order

Adding that capability is pretty straightforward, using a commandbutton with two setactionlistener elements, a generic page that acts as popup dialog along with its generic navigation case and the generic ExcelExporter bean and its configuration in the faces-config.xml.

Create this managed bean configuration for the ExcelExportBean

  <managed-bean>
    <managed-bean-name>ExcelExportBean</managed-bean-name>
    <managed-bean-class>nl.amis.jsf.ExcelExporter</managed-bean-class>
    <managed-bean-scope>session</managed-bean-scope>
  </managed-bean>
 

The class ExcelExporter can be downloaded in the zipfile in the end of the article.

Also add this global navigation case to the faces-config.xml:

  <navigation-rule>
    <from-view-id>*</from-view-id>
    <navigation-case>
      <from-outcome>dialog:ExcelPopup</from-outcome>
      <to-view-id>/ExcelPopup.jspx</to-view-id>
    </navigation-case>
  </navigation-rule>
 

It indicates that the action outcome dialog:ExcelPopup, returned from any command button, should navigate to the generic ExcelPopupjspx page.

 Exporting to Excel from any ADF Table

The commandButton required to add Excel Export to any af:table element looks like this:

<af:commandButton text="Export to Excel" useWindow="true"
                  action="#{ExcelExportBean.exportThisTableToExcel}">
   <af:setActionListener
           from="#{'BooksView'}"
           to="#{ExcelExportBean.tableBindingName}"/>
   <af:setActionListener
           from="#{'Title,Isbn, PublisherName,AuthorsDisplayNames'}"
           to="#{ExcelExportBean.defaultAttributeNames}"/>
</af:commandButton>

Add the useWindow attribute to the commandButton Export to Excel and set it to true – we need this to open the dialog/popup in a new window. We make use of two setActionListener elements to inject two configuration details into the ExcelExport bean. This first is the name of the table binding. This corresponds to the id attribute of the table element in the PageDefinition file. The second one – the defaultAttributeNames – specifies through a comma separated list of attribute names which columns should be included in the Excel sheet. Note: the Attribute Names in this list should correspond with the Value property of the AttrNames\Item elements in the table binding.

Exporting to Excel from any ADF Table

Running the page and pressing the Export to Excel button gives us the popup dialog that shows a shuttle with all attributes available for export:

Exporting to Excel from any ADF Table

Pressing the Export button will now invoke the doExport method on the ExcelExportBean

<f:facet name="footer">
  <af:commandButton text="Export..."
                    action="#{ExcelExportBean.doExport}"/>
</f:facet>

The bean knows which attributes to export in which order from which table binding, it has been told which rows to export – all, the ones shown in the page or a selected range – and it will export the Excel document.

 

Note: the ExcelExport class is completely independent of ADF BC as implementation technology for the business service. It only speaks ADF Model lingo: tableBinding, iterator, rowsetiterator etc. It can be used to produce Excel Exports from data coming from any Data Control.

The next steps

Moving forward I would like to be able to have the user specify Excel Template files upfront into which the data is pasted in the correct location. I will also try to implement uploading excel documents and extract data (new rows and changes) from them.

 

Resources

You can download the most recent version (which is not all that recent by the way) of POI from: http://www.apache.org/dyn/closer.cgi/jakarta/poi/.

ExcelExport class and Popup Dialog page:  excelexporter.zip
 

Download the database scripts to create the ALS database: als_database.zip

Download the JDeveloper 10.1.3.2 project (without the jar files): exportexcel.zip

 

22 Comments

  1. Lucas Jellema December 29, 2011
  2. Eva December 23, 2011
  3. Pete Town August 30, 2011
  4. Ahmed May 21, 2009
  5. ashita January 28, 2009
  6. Raja November 17, 2008
  7. Kevin Bongiovanni July 29, 2008
  8. Ram June 19, 2008
  9. Vanko1 March 14, 2008
  10. Klaas March 7, 2008
  11. Rao August 4, 2007
  12. Lucas Jellema August 4, 2007
  13. Rao August 2, 2007
  14. Rao August 2, 2007
  15. Lucas Jellema June 17, 2007
  16. Nota June 15, 2007
  17. Hari Krishna May 24, 2007
  18. Juan Jesus May 14, 2007
  19. Lucas Jellema May 7, 2007
  20. Michael Trompertz May 5, 2007
  21. zsttr April 19, 2007
  22. Jakub Pawłowski March 28, 2007