无法直接保存到excel中,但是可以保存到csv文件,同样是excel的格式,方法如下: 譬如要把文件生成在d盘下的test目录下: 1,在d盘根目录下新建test目录 2,sqlplus以system用sysdba登录 3,create or replace directory TMP as ’d:\test’; 4,grant read on directory TMP to user; 5,alter system set utl_file_dir='d:\test' scope=spfile; 然后建立存储过程,表是随便建了一个,最后的文件名按要求需要按照当前日期的前一天生成,所以后边文件名的地方write_file_name处就按照要求来了 create or replace PROCEDURE SP_OUTPUT ( on_flag OUT NUMBER, out_reason OUT VARCHAR2) is v_code number; v_text varchar2(255);
cursor cur_sp_out is select aaa,bbb from aaa; begin open cur_sp_out; loop fetch cur_sp_out into v_aaa,v_bbb; exit when cur_sp_out%notfound; write_file_name := to_char(SYSDATE,'YYYYMMDD')||'.xls'; file_handle := utl_file.fopen('TMP',write_file_name,'a'); write_content := v_aaa||' '||v_bbb; --write file IF utl_file.is_open(file_handle) THEN utl_file.put_line(file_handle,write_content); END IF; --close file utl_file.fclose(file_handle); end loop; close cur_sp_out;
v_code := 0; v_text := '完成'; exception when others then on_flag := SQLCODE; out_reason := SQLERRM; begin IF utl_file.is_open(file_handle) THEN utl_file.fclose(file_handle); end IF; end; rollback; end;