المكتبة التقنية ←

تنزيل ملف Excel مخصص من Oracle APEX

English version

A dashboard and its Excel download often serve different readers. The dashboard might show totals by month, while a reviewer needs one row per order. A dedicated export query gives that workbook its own columns, ordering, and rules without adding a second report region to the page.

Build the workbook from a query

In a development APEX application, create a button that submits the page. Add a PL/SQL page process and restrict it to that button. Keep this download request separate from processes that save or change data. The example uses two synthetic orders from DUAL, so it needs no application tables.

declare
    l_query apex_exec.t_context;
    l_file  apex_data_export.t_export;
begin
    l_query := apex_exec.open_query_context(
        p_location => apex_exec.c_location_local_db,
        p_sql_query => q'[
            select 1001 as order_id, date '2026-01-15' as order_date,
                   125.50 as order_total
            from dual
            union all
            select 1002, date '2026-01-16', 78.00
            from dual
            order by order_id
        ]'
    );
    begin
        l_file := apex_data_export.export(
            p_context   => l_query,
            p_format    => apex_data_export.c_format_xlsx,
            p_file_name => 'sample-orders'
        );
    exception
        when others then
            apex_exec.close(l_query);
            raise;
    end;
    apex_exec.close(l_query);
    apex_data_export.download(p_export => l_file);
end;

The query context supplies typed rows. APEX_DATA_EXPORT.EXPORT builds the XLSX payload, and DOWNLOAD sends it as the response. Close the query context before calling DOWNLOAD; by default, the download stops the APEX engine. The inner exception handler releases the context if building the workbook fails, without intercepting normal download termination. See Oracle's references for EXPORT, DOWNLOAD, and closing the query context.

Decide what users can export

  • Scope: A standalone query does not inherit an Interactive Report's runtime filters. If the file follows page filters, pass their validated values as binds. If users expect the current report view, consider its native download instead.
  • Access: Apply authorization to the page process and enforce row access in the query. Select only columns approved for the workbook. A hidden UI column is not an export permission.
  • Response: Use a request that expects a file. A callback whose JavaScript expects JSON needs a different download flow.

Test what the reviewer receives

Start with the two sample rows. Check the filename, column order, and that the totals are numeric. With your authorized development data, check an empty result, a restrictive filter, and two users with different scopes. Verify the workbook contents against the intended export rules, including any row limit, before enabling the button for users.

Original LinkedIn post

النسخة العربية

شاشة الـDashboard وملف Excel ممكن يخدموا ناس مختلفة. الشاشة قد تعرض إجمالي الطلبات بالشهر، بينما المراجع محتاج صفًا لكل طلب. استعلام مخصص للتصدير يخلّيك تحدد أعمدة الملف وترتيبها وقواعدها من غير إضافة Report Region ثانية للصفحة.

جهّز الملف من استعلام

داخل تطبيق APEX للتطوير، اعمل زر يرسل الصفحة، وأضف Page Process بلغة PL/SQL يشتغل عند الضغط على الزر ده فقط. افصل طلب التنزيل عن العمليات اللي بتحفظ أو تعدّل البيانات. المثال يستخدم طلبين افتراضيين من DUAL، فمش محتاج جداول تطبيق.

declare
    l_query apex_exec.t_context;
    l_file  apex_data_export.t_export;
begin
    l_query := apex_exec.open_query_context(
        p_location => apex_exec.c_location_local_db,
        p_sql_query => q'[
            select 1001 as order_id, date '2026-01-15' as order_date,
                   125.50 as order_total
            from dual
            union all
            select 1002, date '2026-01-16', 78.00
            from dual
            order by order_id
        ]'
    );
    begin
        l_file := apex_data_export.export(
            p_context   => l_query,
            p_format    => apex_data_export.c_format_xlsx,
            p_file_name => 'sample-orders'
        );
    exception
        when others then
            apex_exec.close(l_query);
            raise;
    end;
    apex_exec.close(l_query);
    apex_data_export.download(p_export => l_file);
end;

الـQuery Context يوفّر الصفوف بأنواع بياناتها. الدالة APEX_DATA_EXPORT.EXPORT تجهّز محتوى XLSX، والإجراء DOWNLOAD يرسله في الاستجابة. اقفل الـContext قبل استدعاء DOWNLOAD، لأن التنزيل بيوقف محرك APEX بالإعداد الافتراضي. معالجة الخطأ الداخلية تحرّر الـContext لو تجهيز الملف فشل، من غير ما تتدخل في الإنهاء الطبيعي للتنزيل. راجع مراجع Oracle عن EXPORT وDOWNLOAD وإغلاق الـQuery Context.

حدّد ما يقدر المستخدم يصدّره

  • النطاق: الاستعلام المستقل مش بياخد تلقائيًا فلاتر الـInteractive Report وقت التشغيل. لو الملف يتبع فلاتر الصفحة، مرّر القيم بعد التحقق منها باستخدام Bind Variables. ولو المطلوب نفس عرض التقرير الحالي، فكّر في التنزيل المدمج في التقرير.
  • الصلاحيات: طبّق صلاحية على الـPage Process، وطبّق قواعد الوصول للصفوف داخل الاستعلام. اختار الأعمدة المسموح بإرسالها في الملف فقط؛ إخفاء عمود في الواجهة لا يمنع تصديره.
  • الاستجابة: استخدم طلبًا يتوقّع ملفًا. لو عندك Callback والـJavaScript مستني JSON، هتحتاج مسار تنزيل مختلف.

اختبر الملف اللي هيوصل للمراجع

ابدأ بالصفّين الافتراضيين، وراجع اسم الملف وترتيب الأعمدة وإن الإجماليات أرقام. في بيئة تطوير مصرح لك باستخدامها، اختبر نتيجة فارغة، وفلتر يضيّق النتائج، ومستخدمين بصلاحيات مختلفة. قارن محتويات الملف بقواعد التصدير المطلوبة، بما فيها أي حد لعدد الصفوف، قبل إتاحة الزر للمستخدمين.

البوست الأصلي على LinkedIn