Oracle APEX - Export a query into CSV using a button

前端 未结 2 1441
滥情空心
滥情空心 2021-01-03 15:20

I have a button on an apex page that should allow the user to export data (based on a query not seen by the end user) into a CSV file.

How to link this button to my

相关标签:
2条回答
  • 2021-01-03 15:41

    I did this once by defining a region on the page with condition=never, but you can still invoke the download with the relevant URL, which you can farm from the "download as CSV" link when the region is visible by using the Inspect Element feature of your browser.

    You'll see the request is the region ID with a prefix. So you can use this invoke a download

    /ords/f?p=your_app:your_page:your_session:FLOW_EXCEL_OUTPUT_R571755827567965848_en
    

    or have your button call the same JavaScript

    window.location.href=apex.server.url({p_request: 'FLOW_EXCEL_OUTPUT_R571755827567965848_en'},2);
    

    This will work even if the region condition is Never.

    0 讨论(0)
  • 2021-01-03 15:57

    Thanks to Scott Spendolini, I use his link: https://spendolini.blogspot.fr/2006/04/custom-export-to-csv.html

    I simply create a Report Region with my query. Add a button which will get you to a blank page that I created. On that page, I added a PL/SQL process which will fire "On Load - Before Header" In the source of that process, I use this code:

    begin
    -- Set the MIME type
    owa_util.mime_header( 'application/octet', FALSE );
    -- Set the name of the file
    htp.p('Content-Disposition: attachment; filename="emp.csv"');
    -- Close the HTTP Header
    owa_util.http_header_close;
    -- Loop through all rows in EMP
    for x in (select e.ename, e.empno, d.dname
    from emp e, dept d where e.deptno = d.deptno
      and e.deptno like :P1_DEPTNO)
    loop
     -- Print out a portion of a row,
     -- separated by commas and ended by a CR
     htp.prn(x.ename ||','|| x.empno ||','||
             x.dname || chr(13));
    end loop;
    -- Send an error code so that the
    -- rest of the HTML does not render
    htmldb_application.g_unrecoverable_error := true;
    end;
    
    0 讨论(0)
提交回复
热议问题