oracle使用存储过程将表数据以excel格式导出

打印 上一主题 下一主题

主题 1667|帖子 1667|积分 5001

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
虽然目前pl/sql developer等数据库客户端软件都支持将表数据以excel格式导出,但是假如数据量大,必要等客户端加载表数据等待很久。而且,可能会遇到定时以excel格式导出数据的要求。因此我自己写了一个使用存储过程将表数据以excel格式导出的存储过程。

  • 服务端新建目录
    create directory DIR_EXCEL as 'D:\DIR_EXCEL';
  • 新建存储过程
  1. create or replace procedure pr_export_to_excel(p_table_name      varchar2,
  2.                                                p_where_predicate varchar2 default null) is
  3.   /*
  4.   propose:根据表名和where条件生成excel
  5.   p_where_predicate:where条件语句
  6.   */
  7.   out_file          utl_file.file_type; --定义一个文件类型变量
  8.   str1              varchar2(20000); --定义一个字符串变量,用于存储表1的字段名
  9.   str1_chr          varchar2(30000);
  10.   l_sql             varchar2(20000);
  11.   l_where_predicate varchar2(30000) default 'where ' || p_where_predicate;
  12. begin
  13.   if p_where_predicate is null then
  14.     l_where_predicate := null;
  15.   end if;
  16.   --查询表1的字段名,用制表符分隔,并赋值给str1
  17.   select listagg(column_name, chr(9)) within group(order by column_id)
  18.     into str1
  19.     from user_tab_columns
  20.    where table_name = upper(p_table_name);
  21.   --查询表1的字段名,用制表符分隔,并赋值给str1_chr
  22.   select listagg(case
  23.                    when t.DATA_TYPE = 'DATE' OR t.DATA_TYPE LIKE 'TIMESTAMP%' THEN
  24.                     'to_char(f_cur.' || column_name || ',''YYYYMMDD HH24:MI:SS'')'
  25.                    else
  26.                     'f_cur.' || column_name
  27.                  END,
  28.                  '||chr(9)||') within group(order by column_id)
  29.     into str1_chr
  30.     from user_tab_columns t
  31.    where table_name = upper(p_table_name);
  32.   l_sql := '
  33.   declare
  34.     out_file utl_file.file_type; --定义一个文件类型变量
  35.   BEGIN
  36.     --打开一个文件,指定目录对象、文件名和写入模式
  37.     out_file := utl_file.fopen('' DIR_EXCEL '',
  38.                                ''' || p_table_name ||
  39.            '.xls '',
  40.                                '' W '',
  41.                                32767);
  42.     utl_file.put_line(out_file,
  43.                       ''' || str1 ||
  44.            '''); --写入字段名,换行
  45.     for f_cur in (select *
  46.                     from ' || p_table_name || ' t ' ||
  47.            l_where_predicate || ') loop
  48.       utl_file.put_line(out_file, ' || str1_chr || ');
  49.     end loop;
  50.     utl_file.fclose(out_file);
  51.   exception
  52.     when others then
  53.       utl_file.fclose(out_file); --关闭文件,防止异常关闭
  54.       dbms_output.put_line(SQLERRM);
  55.       dbms_output.put_line(dbms_utility.format_error_backtrace);
  56.       raise; --抛出异常信息
  57.   end;
  58.   ';
  59.   dbms_output.put_line(l_sql);
  60.   --dbms_output.put_line(l_sql);
  61.   execute immediate l_sql;
  62. exception
  63.   when others then
  64.     utl_file.fclose(out_file); --关闭文件,防止异常关闭
  65.     dbms_output.put_line(SQLERRM);
  66.     dbms_output.put_line(dbms_utility.format_error_backtrace);
  67.     raise; --抛出异常信息
  68. end pr_export_to_excel;
复制代码
3.调用存储过程
call pr_export_to_excel('TEST','NAME='''123''');
4.去目录'D:\DIR_EXCEL'取出TEST.xls文件

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

滴水恩情

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表