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