将多个 SQL 查询的组合数据导出到 Oracle APEX 中的单个 CSV 报告中

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

我尝试通过使用 CLOB 组合两个 SQL 查询来以 CSV 格式从屏幕导出数据,但无法使用 apex_data_export.export API 导出它,因为它不直接接受 CLOB (l_combined_clob) 作为其 p_context 参数。还有其他办法吗?

代码

DECLARE
  l_combined_clob CLOB;
  l_page_items_clob CLOB;
  l_report_clob CLOB;
  l_page_items_export apex_data_export.t_export;
  l_report_export apex_data_export.t_export;
  l_page_items_context apex_exec.t_context;
  l_report_context apex_exec.t_context;
  l_combined_blob BLOB;
BEGIN

  l_page_items_context := apex_exec.open_query_context(
    p_location => apex_exec.c_location_local_db,
    p_sql_query => '
      SELECT
        region AS REGION_NAME,
        item_name AS FIELD_NAME,
        APEX_UTIL.GET_SESSION_STATE(item_name) AS FIELD_VALUE
      FROM
        apex_application_page_items
      WHERE
        page_id = :APP_PAGE_ID
        AND application_id = :APP_ID
      ORDER BY region'
  );

  l_page_items_export := apex_data_export.export(
    p_context => l_page_items_context,
    p_format  => apex_data_export.c_format_csv,
    p_as_clob => true
  );
  l_page_items_clob := l_page_items_export.content_clob;

  apex_exec.close(l_page_items_context);


  l_report_context := apex_exec.open_query_context(
    p_location => apex_exec.c_location_local_db,
    p_sql_query => 'SELECT * FROM emp'
  );

  l_report_export := apex_data_export.export(
    p_context => l_report_context,
    p_format  => apex_data_export.c_format_csv,
    p_as_clob => true
  );
  l_report_clob := l_report_export.content_clob;

  apex_exec.close(l_report_context);


  l_combined_clob := 'Page Items Section:' || CHR(10) || l_page_items_clob || CHR(10) ||
                     'Report Section:' || CHR(10) || l_report_clob;
l_combined_export := apex_data_export.export(
    p_context => l_combined_clob,
    p_format  => apex_data_export.c_format_csv,
    p_filename => 'combined_export.csv'
    -- p_as_clob => true
  );

  apex_data_export.download(
      p_export => l_combined_export
      );


END;
plsql oracle-apex
1个回答
0
投票

我双重支持使用APEX_DATA_EXPORT,格式不再是CSV而是简单的文本文件。 这可以通过一些 pl/sql 来完成。以下是在渲染之前或提交之后(不是在动态操作中)放入页面进程的示例。请注意,您必须通过连接列来生成自己的逗号分隔数据。

owa_util.mime_header('text/plain',   FALSE);
htp.p('Content-Type: application/octet-stream');
htp.p('Content-Disposition: attachment;filename="My Text File.txt"');
owa_util.http_header_close;

htp.p('Emp Records');

FOR emp_rec IN (SELECT ename ||','||job as c FROM emp) LOOP
  htp.p(emp_rec.c);
END LOOP;

htp.p('Dept Records');

FOR dept_rec IN (SELECT dname ||','||loc as c FROM dept) LOOP
  htp.p(dept_rec.c);
END LOOP;

apex_application.stop_apex_engine;
© www.soinside.com 2019 - 2024. All rights reserved.