使用 Oracle APEX (apex_data_export) 导出 SQL 查询并下载多个 XLSX 文件

问题描述 投票:0回答:1

我想在 Oracle APEX 中创建一些内容,通过选择复选框,我可以执行特定的 SQL 选择并以 xlsx 格式下载它们。但是,我希望能够一次选择多个复选框并同时下载多个文件。这是我的代码,但问题是它每次只下载第一个文件。

这是用

process
编写的,当我们单击按钮时运行。 代码大致是这样的:

DECLARE
     l_context1 apex_exec.t_context;
     l_context2 apex_exec.t_context;
     l_export1  apex_data_export.t_export;
     l_export2  apex_data_export.t_export;
BEGIN

l_context1 := apex_exec.open_query_context(
    p_location    => apex_exec.c_location_local_db,
    p_sql_query   => 'select * from emp' );

l_export1 := apex_data_export.export (
                p_context   => l_context1,
                p_format    => apex_data_export.c_format_xlsx );

apex_data_export.download( p_export => l_export1 );

apex_exec.close( l_context1 );

l_context2 := apex_exec.open_query_context(
    p_location    => apex_exec.c_location_local_db,
    p_sql_query   => 'select * from dept' );

l_export2 := apex_data_export.export (
                p_context   => l_context2,
                p_format    => apex_data_export.c_format_xlsx );

apex_data_export.download( p_export => l_export2 );

apex_exec.close( l_context2 );

EXCEPTION
WHEN others THEN
    IF l_context1 IS NOT NULL THEN
        apex_exec.close( l_context1 );
    END IF;

    IF l_context2 IS NOT NULL THEN
        apex_exec.close( l_context2 );
    END IF;

    raise;
END;

我读到过,

apex_data_export.download
一次只能处理一个下载过程。这是真的?有解决办法吗?

oracle plsql export oracle-apex xlsx
1个回答
0
投票

评论中@koen-lostrie解释的想法可以使用以下代码实现:

DECLARE
     l_context1 apex_exec.t_context;
     l_context2 apex_exec.t_context;
     l_export1  apex_data_export.t_export;
     l_export2  apex_data_export.t_export;
    l_zip_file blob;

    v_vcContentDisposition VARCHAR2 (25)  := 'inline';
    v_file_name VARCHAR2(255) := 'download.zip';
BEGIN

    --- create emp xlsx file then zip it
l_context1 := apex_exec.open_query_context(
    p_location    => apex_exec.c_location_local_db,
    p_sql_query   => 'select * from emp' );

l_export1 := apex_data_export.export (
                p_context   => l_context1,
                p_format    => apex_data_export.c_format_xlsx);

apex_zip.add_file (
            p_zipped_blob => l_zip_file,
            p_file_name   => 'emp.xlsx',
            p_content     => l_export1.content_blob );
    
    --- create dept xlsx file then zip it
l_context2 := apex_exec.open_query_context(
    p_location    => apex_exec.c_location_local_db,
    p_sql_query   => 'select * from dept' );

l_export2 := apex_data_export.export (
                p_context   => l_context2,
                p_format    => apex_data_export.c_format_xlsx );

apex_zip.add_file (
            p_zipped_blob => l_zip_file,
            p_file_name   => 'dept.xlsx',
            p_content     => l_export2.content_blob );

APEX_ZIP.FINISH (p_zipped_blob =>  l_zip_file);

    --- download the zip file
    OWA_UTIL.MIME_HEADER( 'application/zip', FALSE );
    HTP.p('Content-Length: ' || DBMS_LOB.GETLENGTH(l_zip_file));
    HTP.p('Content-Disposition: ' || v_vcContentDisposition ||'; filename="' || v_file_name || '"');
    OWA_UTIL.http_header_close;
    WPG_DOCLOAD.DOWNLOAD_FILE(l_zip_file);
    APEX_APPLICATION.STOP_APEX_ENGINE;

EXCEPTION
WHEN others THEN
    IF l_context1 IS NOT NULL THEN
        apex_exec.close( l_context1 );
    END IF;

    IF l_context2 IS NOT NULL THEN
        apex_exec.close( l_context2 );
    END IF;
    raise;
END;
© www.soinside.com 2019 - 2024. All rights reserved.