Skip to main content

Generating the report with APEX_DATA_EXPORT


With the APEX_DATA_EXPORT package, you are able to export data from Oracle Application Express in the following file types: PDF, XLSX, HTML, CSV, XML, and JSON.

Step 1: Create a table and populate it with some sample records.

CREATE TABLE emp
  (
    empno        NUMBER,
    first_name   VARCHAR2(240),
    last_name    VARCHAR2(240),
    mgr          NUMBER,
    deptno       NUMBER,
    sal          NUMBER,
    created_date TIMESTAMP (6),
    comm         NUMBER,
    hiredate     DATE,
    JOB          VARCHAR2(240),
    ename        VARCHAR2(240),
    PRIMARY KEY (empno) USING INDEX ENABLE
  );
/
  
INSERT INTO emp (empno, first_name, last_name, mgr, 
                 deptno, sal, created_date) 
       VALUES
                (1, 'Larry', 'Ellison', ,
                 10, 5000, LOCALTIMESTAMP);
 
INSERT INTO emp (empno, first_name, last_name, mgr, 
                 deptno, sal, created_date) 
       VALUES
                (2, 'Juan', 'Juan', 1,
                 10, 3500, LOCALTIMESTAMP);

INSERT INTO emp (empno, first_name, last_name, mgr, 
                 deptno, sal, created_date) 
       VALUES
                (3, 'Keith', 'Block', 1,
                 10, 3800, LOCALTIMESTAMP);
/

Step 2: Create a new blank page.  

Note: The page number I had was 13. You need to replace the reference to "P13" with your own page number if it is different.

Step 3: Create a new region on the page. In the Property Editor, apply the following changes:

Under Identification:
     For Title - enter Generating the report with APEX_DATA_EXPORT
Under Layout:
     For Position - select Breadcrumb Bar
Under Appearance:
     For Template - Hero
     For Icon - enter fa-download

Step 4: Navigate to that Region and right-click Create Button. In the Property Editor, apply the following changes:

Under Identification:
     For Button Name - enter DOWNLOAD
     For Label - enter Download Report
Under Layout:
     For Region - select Generating the report with APEX_DATA_EXPORT 
     For Position - select Next
Under Appearance:
     For Button Template - select Text with Icon
     Enable Hot button
For Template Option - Set Style Simple
     For Icon - enter fa-download

Step 5: Create a sub region on the parent region (Generating the report with APEX_DATA_EXPORT) . In the Property Editor, apply the following changes:

Under Identification:
     For Title - enter Report Format
Under Layout:
     For Parent Region - select Generating the report with APEX_DATA_EXPORT 
     For Position - select Sub Regions
Under Appearance:
     For Template - Blank with Attributes
 
Step 6: Create a select list on the sub region (Report Format). In the Property Editor, apply the following changes:

Under Identification:
     For Name - enter P13_REPORT_FORMAT
     For Type - Select List
Under Label:
     For Label - enter Report Format
Under Layout:
     For Region - select Report Format
Under Appearance:
     For Template - Hidden
     For Template Options - set Size as Large and Right Margin as Small
Under List of Values:
     For Type - Static Values
     For Static Values - Excel:E; PDF:P; CSV:C; HTML:H; JSON:J; XML:X;
     Display Extra Values - No
     Display Null Values - No
Under Default:
     For Type - Static
    For Static Value - P

Step 8: To view the data from the emp table, create an interactive report region.

Finally, the report generation screen should look like the attached image.


Step 9: To download the report, create a PL/SQL process.

1) Navigate to Rendering Tree and click on Processing Tab. 
2) Right-click on Processing and click Create Process. 
3) In the Property Editor, apply the following changes:

Under Identification:
     For Name - enter Generate Report
Under Source:
     For PL/SQL Code enter the following code:

-------------------------- ********************* --------------------------

DECLARE
    l_context         apex_exec.t_context; 
    l_print_config    apex_data_export.t_print_config;
    l_export          apex_data_export.t_export;
    l_report_format   apex_data_export.t_format;
BEGIN
    l_context := apex_exec.open_query_context(
        p_location    => apex_exec.c_location_local_db,
        p_sql_query   => 'select * from emp' );

    l_print_config := apex_data_export.get_print_config(
        p_orientation     => apex_data_export.c_orientation_portrait,
        p_border_width    => 2 );
    
    IF :P13_REPORT_FORMAT = 'P' THEN
        l_report_format := apex_data_export.c_format_pdf;
    elsIF :P13_REPORT_FORMAT = 'E' THEN
        l_report_format := apex_data_export.c_format_xlsx;
    elsIF :P13_REPORT_FORMAT = 'C' THEN
        l_report_format := apex_data_export.c_format_csv;
    elsIF :P13_REPORT_FORMAT = 'J' THEN
        l_report_format := apex_data_export.c_format_json;
    elsIF :P13_REPORT_FORMAT = 'X' THEN
        l_report_format := apex_data_export.c_format_xml
    elsIF :P13_REPORT_FORMAT = 'H' THEN
        l_report_format := apex_data_export.c_format_html;                               
    END IF;
    
    l_export := apex_data_export.export (
        p_context         => l_context,
        p_print_config    => l_print_config,
        p_format          => l_report_format);

    apex_exec.close( l_context );

    apex_data_export.download( p_export => l_export );

EXCEPTION
    when others THEN
        apex_exec.close( l_context );
        raise;
END;

-------------------------- ********************* --------------------------

Under Server-side condition:
     For When Button pressed - select DOWNLOAD

Step 10: Navigate to Rendering Tree and click on Page (No. 13). In the Property Editor, apply the following changes:

Under Advanced
     For Reload on Submit - select Always.

Note: The default behavior for Reload on Submit is Only for success. This will result in the below error when downloading the report.


Step 11: Click Save and Run Page.

Output: Now you can programmatically generate the report by selecting the Report Format and clicking on the Generate Report button.

The demo is here.

References:

Comments

  1. Do you need financing to renovate your house or to carry out your various projects? Fast and reliable in the short and long term at a rate of 3%. For more details: contact us directly by financialserviceoffer876@gmail.com WhatsApp +918929509036

    ReplyDelete
  2. Thanks but this is available "out of the box" now"

    ReplyDelete
    Replies
    1. yes it is, however sometimes the users want to Export something else than is displayed on screen. Then this becomes very handy

      Delete
  3. Realmente muito bom e útil.

    ReplyDelete

Post a Comment

Popular posts from this blog

Printing Page Numbers in RTF Template [Oracle BI Publisher]

Here I am giving an example to print the page numbers dynamically in the RTF (Rich Text Format) template. Step 1:  Go to page footer and copy and paste the below script. Page |  <?fo:page-number?>  of  <?fo:page-number-citation:xdofo:lastpage-joinseq?> <fo:page-number> :   This is the object, which is used to represent the current page-number. <?fo:page-number-citation:xdofo:lastpage-joinseq?> :  This is the syntax, which is used to represent the total number of pages. Step 2:  Load the XML and preview the result. Output: That's it. References: fo:page-number Printing Page Number Code in Oracle XMLP RTF Template

Save Selected Interactive Grid Records into a Collection - Oracle APEX

Here I am giving an example to save selected interactive grid records into a oracle apex collection. Step 1: Create a new blank page. Note: Mine was page 20. You will need to update reference to " P20 " with your page number if it's different. Step 2: Create a new interactive grid report region to the page using below query. Set Static Id "EmpDetails" to the region. SELECT  *     FROM   ( SELECT  emp . empno ,                emp . ename ,                emp . JOB ,                dept . dname department ,                dept . loc  LOCATION ,                mgr . ename  manager ,                emp . hiredate ,                 nvl ( emp . sal , 0 )  salary ,                 nvl ( emp . comm , 0 )  commission            FROM  eba_demo_chart_emp emp ,                eba_demo_chart_dept dept ,                eba_demo_chart_emp mgr           WHERE  emp . deptno = dept . deptno             AND  emp . mgr      = mgr . empno  ( + )           ORDER   BY  emp . ename