Exporting a report to Excel is easy in Oracle APEX. Making the Excel file show colored cells based on your data is the part most developers get stuck on. In this post I show the built-in, officially documented way to do it, using the classic EMP table as sample data.
Our rule: if an employee’s commission (COMM) is NULL or 0, the ENAME cell must have a red background in the Excel file.
The Problem
Highlight rules you create on an Interactive Report, and CSS you use on the page, are not carried into an exported XLSX. A plain apex_region.export_data call gives you the data and headings, but no per-cell colors. Many people reach for third-party libraries to solve this, but you do not need one.
The Built-in Solution: ADD_HIGHLIGHT
The APEX_DATA_EXPORT package has a procedure called ADD_HIGHLIGHT. It builds a highlight collection that you pass to apex_data_export.export. A highlight can color a whole row, or one cell in a row. The idea is simple:
- Add a flag column to your query. It holds a highlight ID when the color should apply, and NULL when it should not.
- Register a highlight that says: when the flag column has this ID, color this display column with these colors.
- Pass the highlights to the export call.
Step 1: Define the Highlight
apex_data_export.add_highlight(
p_highlights => l_highlights,
p_id => 1,
p_value_column => 'HL_ENAME', -- flag column in the query
p_display_column => 'ENAME', -- cell that gets colored
p_text_color => '#FFFFFF',
p_background_color => '#FF0000' ); -- redColors are hex codes. If you leave p_display_column empty, the whole row is highlighted instead of one cell.
Step 2: Add the Flag Column to the Query
select empno,
ename,
job,
sal,
comm,
case when nvl(comm, 0) = 0 then 1 end as hl_ename
from emp
order by empnonvl(comm, 0) = 0 is true for both NULL and 0, so both cases get the flag value 1. Everyone with a commission above 0 gets NULL, which means no color.
Step 3: Choose the Exported Columns
The flag column is only a helper, and you do not want it in the Excel file. A column collection lets you export only the columns you list, and you can set the heading text at the same time.
apex_data_export.add_column(l_columns, 'EMPNO', 'Employee No');
apex_data_export.add_column(l_columns, 'ENAME', 'Name');
apex_data_export.add_column(l_columns, 'JOB', 'Job');
apex_data_export.add_column(l_columns, 'SAL', 'Salary');
apex_data_export.add_column(l_columns, 'COMM', 'Commission');The Complete Code
Create a button Download and a page process (type Execute Code, PL/SQL) fired by that button.
DECLARE
l_highlights apex_data_export.t_highlights;
l_columns apex_data_export.t_columns;
l_context apex_exec.t_context;
l_export apex_data_export.t_export;
BEGIN
apex_data_export.add_highlight(
p_highlights => l_highlights,
p_id => 1,
p_value_column => 'HL_ENAME',
p_display_column => 'ENAME',
p_text_color => '#FFFFFF',
p_background_color => '#FF0000' );
apex_data_export.add_column(l_columns, 'EMPNO', 'Employee No');
apex_data_export.add_column(l_columns, 'ENAME', 'Name');
apex_data_export.add_column(l_columns, 'JOB', 'Job');
apex_data_export.add_column(l_columns, 'SAL', 'Salary');
apex_data_export.add_column(l_columns, 'COMM', 'Commission');
l_context := apex_exec.open_query_context(
p_location => apex_exec.c_location_local_db,
p_sql_query => 'select empno, ename, job, sal, comm,
case when nvl(comm, 0) = 0 then 1 end as hl_ename
from emp
order by empno' );
l_export := apex_data_export.export(
p_context => l_context,
p_format => apex_data_export.c_format_xlsx,
p_columns => l_columns,
p_highlights => l_highlights,
p_file_name => 'emp_report' );
apex_exec.close( l_context );
apex_data_export.download( p_export => l_export );
EXCEPTION
WHEN OTHERS THEN
apex_exec.close( l_context );
RAISE;
END;When the user clicks the button, the browser downloads the XLSX.
What You Get
The standard EMP data has 14 rows. Only ALLEN, WARD and MARTIN have a commission above 0. TURNER has a commission of 0 and the rest have NULL. So in the Excel file, the ENAME cell is red for 11 employees and plain for those 3.
Bonus: Send the Colored File by Email
Instead of downloading, you can attach the BLOB to an email. Replace the download call with this:
DECLARE
l_mail_id NUMBER;
BEGIN
-- ... same code as above up to apex_exec.close ...
l_mail_id := apex_mail.send(
p_to => 'someone@example.com',
p_from => 'noreply@example.com',
p_subj => 'EMP Report',
p_body => 'Please find the report attached.' );
apex_mail.add_attachment(
p_mail_id => l_mail_id,
p_attachment => l_export.content_blob,
p_filename => 'emp_report.xlsx',
p_mime_type => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' );
COMMIT;
apex_mail.push_queue;
END;Gotchas
- Quote delimiters. If you build the query with dynamic SQL and use
q'[ ... ]', a regular expression like'[A-Za-z]'inside the text ends the literal early and gives a PLS-00103 error. Use another delimiter such asq'~ ... ~'. - Several highlights in one row. An older Oracle forum thread reported that, in one release, a second highlighted cell in the same row reused the first color. Use a separate flag column and highlight ID for each cell, and test on your APEX version.
- Helper columns. Always pass a column collection, or the flag columns appear in the Excel file.
- Numbers stored as text. If the value you test is a VARCHAR2, convert it safely (for example with
validate_conversion) before comparing.
Conclusion
You do not need external libraries to color cells in an APEX Excel export. Add a flag column to the query, register it with apex_data_export.add_highlight, and pass the highlights to apex_data_export.export. With a few lines of PL/SQL, your exported reports can show exceptions at a glance.
Reference: Oracle APEX API Reference, APEX_DATA_EXPORT package (ADD_HIGHLIGHT and ADD_COLUMN procedures).
Comments
Post a Comment