In the current JDeveloper version 11.1.1.9.0 and 12.1.3 the af:exportCollectionActionListener
got enhanced by options to filter the data to export.
The option this blog talks about is the one marked, the FilterMethod. The ducumentation for 12.12 Exporting Data from Table, Tree, or Tree Table does not reveal too much about how to use this FilterMethod.
The sample we build in this blog entry shows how the FilterMethod can be used to filter the data to be exported to excel.
In older version of JDev you hadto use a trick to filter the data which was downloaded from a table see Validate Data before Export via af:exportCollectionActionListener or af:fileDownloadActionListener. The new property of the
af:exportCollectionActionListener
allows to filter the data without using the trick.The sample just load the employees table from the HR DB schema and shows it in a table on the screen. In the toolbar we add a button which has the
af:exportCollectionActionListener
attached.Below is the page code of the toolbar holdign the export button:
<f:facet name="toolbar"> <af:toolbar id="t2"> <af:button text="Export to Excel" id="b1"> <af:exportCollectionActionListener type="excelHTML" exportedId="t1" filename="emp.xsl" title="Export" filterMethod="#{ExportToExcelBean.exportCollectionFilter}"/> </af:button> </af:toolbar>
The filterMethod of the af:exportCollectionActionListener
points to a bean method exportCollectionFilter
in a request scoped bean ExportToExcelBean
. The method gets called for each cell of the table which gets exported.
/** * This method gets called for each cell which is to be exported. * It can be used to filter data to be exported. In this case salary values > 6000 are not exported * @param uIComponent component of the cess which gets to be exported * @param exportContext context of the exported data (holds e.g. file name, character set...) * @param formatHandler format to be exported * @return true if cell value is exported, false if not */ public Boolean exportCollectionFilter(UIComponent uIComponent, ExportContext exportContext, FormatHandler formatHandler) { if (exportContext.isFirstInRow()) { count++; _logger.info("Start a new Row " + count); } _logger.info("Export Collection UIComponent: " + uIComponent.getId()); if (uIComponent instanceof RichOutputText) { RichOutputText rot = (RichOutputText) uIComponent; Object val = rot.getValue(); String headerText = ""; UIComponent component = rot.getParent(); if (component instanceof RichColumn) { RichColumn col = (RichColumn) component; headerText = col.getHeaderText(); } StringBuilder sb = new StringBuilder(); sb.append("Name: "); sb.append(headerText); sb.append(" Value: "); sb.append(val); _logger.info(sb.toString()); // check if the salary is greater than 6000 if ("Salary".equals(headerText)) { if (((BigDecimal) val).intValue() > 6000) { // if yes return false so that the value isn't exported _logger.info("Skip Vals > 6000"); return false; } } } return true; }
The method gets the uiComponent which represents the current cell to be exported, the ExportContext and the FormatHandler for the export. The ExportContext hold information about the filename, title, the used character set and status information about the row and cells currently exported. The status can be used to find out is a new row just starts to be exported ro is a cell is part of a span of cells. In the sample we use this information to print a log message for each row exported.
The FormatHandler is used to generate the document to be exported and the data in it. I did not find a way to use my own handler and there is no documentation about how to use another handler, so we leaf this as is for the moment.
In the sample method we like to filter the employee data in a way, that salaries greater than 6000 are not exported to the resulting file. As the method is called for each cell, the first thing to find out is which cell currently used. In lines 15-29 we use the current UIComponent to find out which column we are in. In lines 31-37 we check the salary column. In case the salary value is greater than 6000 we return false as this will trigger that the cell value is not exported. If the salary is below or equal to 6000 we return true and the cell value is exported.
Below we see the result we get if we export the table without the filterMethod set:
and the result with the filter method set:
You can download the sample application which was build using JDeveloper 12.1.3 and the HR DB schema from GitHub.