Oracle通过Sqlplus结合Shell脚本方式生成Excel文件-成都创新互联网站建设

关于创新互联

多方位宣传企业产品与服务 突出企业形象

公司简介 公司的服务 荣誉资质 新闻动态 联系我们

Oracle通过Sqlplus结合Shell脚本方式生成Excel文件

一、问题描述
       今天接到一个任务,是要求每天通过SQL脚本生成excel文件,并且自动发送到相关人员邮箱。这个需求我还真是没有做过,之前只做过通过SQL脚本生成为HTML网页文件。于是乎,我又开始的学习过程,如今的Internet时代,解决问题就是easy,很快就找到了eygle的博客(使用SQL*PLUS,构建完美excel或html输出http://www.eygle.com/archives/2005/04/eoasqlplusieaae.html)。下面是学习过程中的实验部分。

二、实验
1.建立测试表

  1. SAM@dzwj> create table test1(id int,namevarchar2(10),locvarchar2(30),hire_datedate,emailvarchar2(20),departmentvarchar2(20));
  2. Table created.

  3. SAM@dzwj> insert into test1values (1,'sam1','beijing',sysdate,'sam1@oracle.com','it');
  4. 1row created.
  5. SAM@dzwj> insert into test1values (2,'sam2','beijing',sysdate,'sam2@oracle.com','it');
  6. 1row created.
  7. SAM@dzwj> insert into test1values (3,'sam3','beijing',sysdate,'sam3@oracle.com','it');
  8. 1row created.
  9. SAM@dzwj> insert into test1values (4,'sam4','beijing',sysdate,'sam4@oracle.com','it');
  10. 1row created.
  11. SAM@dzwj> insert into test1values (5,'sam5','beijing',sysdate,'sam5@oracle.com','it');
  12. 1row created.

  13. SAM@dzwj> SAM@dzwj> commit;
  14. Commit complete.

  15. SAM@dzwj> select * from test1;

  16.         ID NAME LOC HIRE_DATE EMAIL DEPARTMENT
  17. ---------- ---------- ------------------------------ ------------------ -------------------- --------------------
  18.          1 sam1 beijing 28-NOV-17 sam1@oracle.com it
  19.          2 sam2 beijing 28-NOV-17 sam2@oracle.com it
  20.          3 sam3 beijing 28-NOV-17 sam3@oracle.com it
  21.          4 sam4 beijing 28-NOV-17 sam4@oracle.com it
  22.          5 sam5 beijing 28-NOV-17 sam5@oracle.com it


2.编辑 main.sql

  1. [oracle@testdb~]$ cat main.sql
  2. set linesize 200pagesize 10000
  3. set termoff verifyoff feedbackoff
  4. set markup htmlon entmapon spool on preformat off
  5. alter session set NLS_DATE_FORMAT='YYYY-MM-DD HH24:MI:SS';
  6. spool /home/oracle/test1.xls
  7. @/home/oracle/get_tables.sql
  8. spool off
  9. exit

3.编辑 get_tables.sql

  1. [oracle@testdb~]$ cat get_tables.sql
  2. select * from test1;

4.编辑执行文件 collect.sh

  1. [oracle@testdb~]$ cat collect.sh
  2. #!/bin/bash
  3. . /home/oracle/.bash_profile
  4. DATE=`date +%Y%m%d`
  5. sqlplus sam/oracle@dzwj@/home/oracle/main
  6. mv/home/oracle/test1.xls/home/oracle/test1_${DATE}.xls

5.给collect.sh 执行权限

  1. [oracle@testdb~]$ chmod u+x collect.sh

6.执行

  1. [oracle@testdb~]$ ./collect.sh

  2. SQL*Plus: Release 11.2.0.4.0 Productionon Wed Nov 29 11:00:19 2017

  3. Copyright(c) 1982, 2013, Oracle. All rights reserved.


  4. Connectedto:
  5. OracleDatabase 11g Enterprise EditionRelease 11.2.0.4.0- 64bit Production
  6. With the Partitioning, OLAP, Data Miningand Real Application Testing options

  7. Disconnectedfrom OracleDatabase 11g Enterprise EditionRelease 11.2.0.4.0- 64bit Production
  8. With the Partitioning, OLAP, Data Miningand Real Application Testing options

7.验证
将文件传回到本地机器打开,得到想要的excel文件
Oracle通过Sqlplus结合Shell脚本方式生成Excel文件

三、总结
       生活在Internet时代真是件幸福的事,此次任务算是告一段落,但是当中还是碰到一些小problems,比如一开始没有加时间NLS_DATE_FORMAT变量的修改,导出的时间类型数据时没有时间,只有年月日。总而言之,多学习,多实践,没错的。向eygle大神致谢。  Where there is a will, there is a way.





分享名称:Oracle通过Sqlplus结合Shell脚本方式生成Excel文件
文章分享:http://kswsj.cn/article/peggho.html

其他资讯