Skip to main content

Color Cells in an Oracle APEX Excel Export

Color Cells in an Oracle APEX Excel Export with APEX_DATA_EXPORT.ADD_HIGHLIGHT

Oracle APEX  |  PL/SQL  |  Excel (XLSX) export

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:

  1. Add a flag column to your query. It holds a highlight ID when the color should apply, and NULL when it should not.
  2. Register a highlight that says: when the flag column has this ID, color this display column with these colors.
  3. Pass the highlights to the export call.

Step 1: Define the Highlight

highlight.sql — PL/SQL
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' );  -- red

Colors 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

query.sql — SQL
select empno,
       ename,
       job,
       sal,
       comm,
       case when nvl(comm, 0) = 0 then 1 end as hl_ename
  from emp
 order by empno

nvl(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.

columns.sql — PL/SQL
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.

emp_export.sql — PL/SQL Page Process
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:

email_attachment.sql — PL/SQL
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 as q'~ ... ~'.
  • 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

Popular posts from this blog

APEX - Tip: Fix Floating Label Issue

Oracle APEX's Universal Theme provides a modern and clean user experience through features like floating (above) labels for page items.  These floating labels work seamlessly when users manually enter data, automatically moving the label above the field on focus or input.  However, a common UI issue appears when page item values are set Dynamically the label and the value overlap, resulting in a broken and confusing user interface. once the user focuses the affected item even once, the label immediately corrects itself and displays properly. When an issue is reported, several values are populated based on a single user input, causing the UI to appear misaligned and confusing for the end user. Here, I'll share a few tips to fix this issue. For example, employee details are populated based on the Employee name. In this case, the first True Action is used to set the values, and in the second True Action, paste the following code setTimeout(function () {   $("#P29_EMAIL,#P29_...

Oracle APEX UI Tip: Display Page Title Next to the APEX Logo

In most Oracle APEX applications, every page has a Page Title displayed at the top. While useful, this title occupies vertical space, especially in apps where screen real estate matters (dashboards, reports, dense forms). So the goal is simple: Show the page title near the APEX logo instead of consuming page content space. This keeps the UI clean, professional, and consistent across all pages. Instead of placing the page title inside the page body:         ✅ Fetch the current page title dynamically         ✅ Display it right after the APEX logo         ✅ Do it globally, so it works for every page All of this is achieved using:         ✅ Global Page (Page 0)         ✅ One Dynamic Action         ✅ PL/SQL + JavaScript Simple, effective, and reusable. 1️⃣ Create a Global Page Item On Page 0 (Global Page), create a hidden item:      P0_PAGE_TITLE This item wi...

Interactive Grid Tips (Part-1)

Selection Toolbar Events Validation Styling Advanced Selection 1 Get Selected Row Primary Key Return selected row PK values to a page item. Two approaches depending on APEX version. Selection APEX 24.2+ Before 24.x Copy // Initialization JavaScript Function function (options) { options.defaultGridViewOptions = { selectionStateItem: "P22_SELECTED_IDS" }; return options; } Copy var ig$ = apex.region( "EMP" ).call( "getViews" , "grid" ); var model = ig$.model; var selectedIds = []; ig$.view$.grid( "getSelectedRecords" ).forEach( function (rec) { selectedIds.push(model.getValue(rec, "EMPNO" )); }); $s ( "P22_SELECTED_IDS" , ...