O. Sam
O. Sam

Reputation: 178

Oracle APEX - Export a query into CSV using a button

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 query so we can export the result into a CSV file?

Thanks

Upvotes: 1

Views: 14308

Answers (3)

shabbir jamshedi
shabbir jamshedi

Reputation: 1

The same code I use in my page before header process ,its working fine . The issue is I have 3 process , each process RUN on different Condition e.g if :P12_reg_id=11 then the process-1 ,if :P12_reg_id=12 the process-2 will execute and download the CSv.

i put condtion in process server side condition but only first process is downloading .

any suggestion please

Upvotes: 0

O. Sam
O. Sam

Reputation: 178

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;

Upvotes: 1

Scott
Scott

Reputation: 5035

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.

enter image description here

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.

Upvotes: 8

Related Questions