Oracle存储过程将执行结果导出生成Excel文件

---directories新建存储路径
create or replace directory DIR_EXCEL as '/opt/leasing';
 
create or replace procedure PRC_TASK_EXCEL is
 
out_file utl_file.file_type; --定义一个文件类型
 
L_FILENAME varchar2(200);
 
str1 varchar2(200);
 
str2 varchar2(200);
 
begin
 
--定义Excel文件名称
 
select to_char(sysdate, 'yyyymmddhh24miss') || '.xls'
 
into L_FILENAME
 
from dual;
 
--定义Excel文件抬头
 
select '姓名' into str1 from dual;
 
select '电话' into str2 from dual;
 
--导出Excel文件
 
out_file := utl_file.fopen('DIR_EXCEL', L_FILENAME, 'W');
 
--写入Excel抬头内容
 
utl_file.put(out_file, convert(str1, 'ZHS16GBK'));----根据Oracle数据库的字符编码进行转换
 
utl_file.put(out_file, convert(str2, 'ZHS16GBK'));
 
utl_file.put_line(out_file, '');
 
--循环写入Excel明细
 
for o in (select c.bakhxm || chr(9) xm, c.bakhzh || chr(9) zh
 
from bat001 c) loop
 
utl_file.put(out_file, convert(o.xm, 'ZHS16GBK'));
 
utl_file.put(out_file, convert(o.zh, 'ZHS16GBK'));
 
utl_file.put_line(out_file, '');
 
end loop;
 
utl_file.fflush(out_file);
 
utl_file.fclose(out_file); --关闭文件流
 
--处理异常
 
exception
 
when others then
 
rollback;
 
utl_file.fclose(out_file); --关闭文件流,防止异常关闭
 
end PRC_TASK_EXCEL;

 

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值