In Application Express 3.1 we made it really easy to store files in tables with a BLOB column and to build reports on those tables with a download link that allows downloading the files to your client. So this feature takes care of storing and downloading the CSV files. No I only needed to write some logic to actually generate the CSV file and insert it into a table.
The following function does that. You supply a query, then the query is parsed and executed. And then the column headings are derived from the describe table and written to the CSV file, followed by the result set. And in the end, the file is returned back as a BLOB. This BLOB can then be written to a table, or otherwise processed.
To showcase this function, I create an APEX application that has a report page for downloading the CSV files. And a form page that lets you create CSV files based on the demo tables that ship with our APEX sample application. You can try this out here:
http://apex.oracle.com/pls/otn/f?p=36083:1:
logon as demo/demo123
If you’d like to try this on your local APEX instance, review the implementation, and use this for your own application, you can download the packaged application here: CSV and BLOB Demo Application.
This file contains the application along with the supporting objects (get_report function and other required database objects). It does not include the demo tables, so you’ll have to have the APEX default sample application installed in your workspace. If you don’t have this application, click on Create Application -> Create Demonstration Application.
Some other interesting usages for this code could be to generate XML instead of CSV. You would only have to replace the delimiters, etc with XML tags using the column names. This XML file could then be used together with our new PL/SQL Print API, i.e. you could send it off to BI Publisher or Apache FOP, along with a XSL-FO stylesheet or RTF laytout, and produce PDF, Word or Excel files, and either download them to the client or store them in BLOB columns the same way as above. More on this in my next posting.


4 comments:
Hello marc,
Very interesting post. I ran your demo application, and I still need to learn and understand your code better, but it seems to me that with a combination of your code, and the APEX dictionary (to retrieve a report region source, or the IR sql-query) it will be possible, with minor changes to your code, to easily produce a tab delimited files. It seems to be an issue lately.
Regards,
Arie.
Hi Marc,
it seems like that you still need to mask a single double quote to comply with the usual CSV-standards:
http://en.wikipedia.org/wiki/Comma-separated_values
Something like :
l_col_val := replace(l_col_val, '"', '""');
Regards,
~Dietmar.
Hi Dietmar,
Yes, that would be important. Thank you for you comment,
Marc
Hello Marc,
I am very new to apex and Salesforce platform. My concern is using APEX, I don't want to use Dataloader or any other application. I want to develop my own application with the same GUI like this application, will use Lead, Account objects/table to genrate a CSV file which store on client desktop. Is it possible?
Thanks in advanve.
papexuser
Post a Comment