首页 技术 正文
技术 2022年11月9日
0 收藏 424 点赞 2,682 浏览 4610 个字

Ref:http://blog.csdn.net/iamlaosong/article/details/8465177

Excel通过ADO方式连接到Oracle并操作Oracle给那些编程能力不强的人带来很大的福音,结合着Excel的数据处理与图表制作,就能很轻松地处理一些常规工作。

日常工作中需要查询各种数据,而且不断变化,处理这些数据的人不是技术人员,不会连接数据库自己查询,通过下面的办法就可以让技术人员编辑好包含查询语句的excel文件,让管理人员自己输入条件取数了。

我的方法是编辑需要的SQL语句保存在单元格中,并在查询条件需要参数值的地方用问号“?”代替,再在其他单元格中保存查询条件所需的参数值,在“宏”中用参数值替换掉SQL语句中的问号,最后执行查询语句并将结果保存到excel表中。

以下是通过Excel的VBA连接Oracle并读取Oracle相关数据的步骤:

  1、引用ADO相关组件:

打开VBA编辑器,在菜单中点选“工具”–》“引用”。确保“Microsoft ActiviteX Data Objects 2.8 Library”和“Microsoft ActiviteX Data ObjectS Recordset 2.8 Library”被勾选上。

  2、建立读取数据的过程:

  1. Public Sub get_data()
  2. ‘根据工作表中的查询语句读取数据
  3. On Error GoTo ErrMsg:
  4. Dim cnn As Object, rst As Object
  5. Dim name, stat, sqls, field As String
  6. Dim pn(4), pm(4) As String
  7. Dim i, j, kk, pmkk, lineno As Integer
  8. Dim OraOpen As Boolean
  9. Set cnn = CreateObject(“ADODB.Connection”)
  10. Set rst = CreateObject(“ADODB.Recordset”)
  11. sqls = “connect database”
  12. cnn.Open “Provider=msdaora;Data Source=dl580;User Id=sxjkuser;Password=sxjkpasswd;”
  13. OraOpen = True ‘成功执行后,数据库即被打开
  14. If OraOpen Then lineno = [D65536].End(xlUp).Row Else lineno = 0       ‘行数
  15. Application.Calculation = xlManual
  16. For i = 3 To lineno
  17. stat = Trim(Cells(i, 3))
  18. If stat = “Y” Or stat = “y” Then
  19. name = Cells(i, 2)
  20. field = Cells(i, 4)
  21. pn(1) = Cells(i, 5)
  22. pm(1) = Cells(i, 6)
  23. pn(2) = Cells(i, 7)
  24. pm(2) = Cells(i, 8)
  25. pn(3) = Cells(i, 9)
  26. pm(3) = Cells(i, 10)
  27. pn(4) = Cells(i, 11)
  28. pm(4) = Cells(i, 12)
  29. pmkk = Cells(i, 13)
  30. sqls = Cells(i, 15)
  31. ‘MsgBox sqls
  32. For kk = 1 To pmkk  ‘用于参数多次使用,如联合SQL语句中每个子句都需要日期参数
  33. For j = 1 To 4
  34. If pn(j) <> “” Then
  35. sqls = Replace(sqls, “?”, pm(j), 1, 1)
  36. ‘MsgBox sqls
  37. End If
  38. Next j
  39. Next kk
  40. MsgBox sqls
  41. Set rst = cnn.Execute(sqls)
  42. sqls = “clear sheets”
  43. maxrow = Sheets(name).UsedRange.Rows.Count
  44. Sheets(name).Range(“a2:” & field & maxrow).ClearContents
  45. sqls = “CopyFromRecordset”
  46. Sheets(name).Range(“a2”).CopyFromRecordset rst
  47. Cells(i, 3) = “成功”
  48. ‘MsgBox i
  49. End If
  50. Next i
  51. ‘rst.Close
  52. ‘Set rst = Nothing
  53. cnn.Close
  54. Set cnn = Nothing
  55. Application.Calculation = xlAutomatic
  56. ‘Sheets(“分析”).PivotTables(“数据透视表1”).PivotCache.Refresh
  57. Worksheets(“系统参数”).Select
  58. msg = MsgBox(“数据读取完毕!”, vbOKOnly, “iamlaosong”)
  59. Exit Sub
  60. ErrMsg:
  61. OraOpen = False
  62. MsgBox sqls, vbCritical, “操作失败 ,请检查!”
  63. End Sub

3、SQL语句实例

这是一个简单的语句:

  1. SELECT *  FROM zdgc_sn_sj_gfl t
  2. WHERE t.CLCT_DATE = to_date(‘?’, ‘yyyy-mm-dd’)
  3. AND t.JSBZ = ‘1’
  4. ORDER BY t.CITY, t.SSXS

这是一个复杂的语句:

  1. select aa.zj_code,
  2. aa.zj_mc,
  3. aa.clct_date,
  4. aa.sjzl,
  5. aa.jyqsjzl,
  6. nvl(bb.wgfsl, 0),
  7. nvl(bb.jyqwgfsl, 0)
  8. from (select b.ssxs,
  9. b.zj_code,
  10. b.zj_mc,
  11. a.clct_date,
  12. count(*) sjzl,
  13. sum(case
  14. when to_char(a.clct_time, ‘hh24mi’) <= ‘?’ then
  15. 1
  16. else
  17. 0
  18. end) jyqsjzl
  19. from tb_evt_mail_clct a, sncn_zd_jg b
  20. where a.clct_bureau_org_code = b.zj_code
  21. and a.time_limit_code <> ‘6’
  22. and a.mail_kind_code <> ‘10401’
  23. and a.addi_service_code <> ‘1’
  24. and (a.rcv_area like ‘23%’ or a.rcv_area like ‘24%’)
  25. group by b.ssxs, b.zj_code, b.zj_mc, a.clct_date) aa
  26. left join (select b.ssxs,
  27. b.zj_code,
  28. b.zj_mc,
  29. a.clct_date,
  30. count(*) wgfsl,
  31. sum(decode(jybz, ‘b’, 1, 0)) jyqwgfsl
  32. from sncn_zd_jg b, zdgc_sn_sj_errfc a
  33. where a.zj_code = b.zj_code
  34. and a.jsbz = ‘1’
  35. and a.jybz = ‘b’
  36. group by b.ssxs, b.zj_code, b.zj_mc, a.clct_date) bb on aa.ssxs =
  37. bb.ssxs
  38. and aa.zj_code =
  39. bb.zj_code
  40. and aa.clct_date =
  41. bb.clct_date
  42. where aa.clct_date = to_date(‘?’, ‘yyyy-mm-dd’)
  43. and aa.ssxs = ‘?’
  44. order by aa.zj_code, aa.zj_mc

  4、操作界面

这是一个:

这是另一个:

  5、说明

1)使用者需要安装Oracle客户端并进行本地服务名配置(运行客户端程序Net Configuration Assistant配置,本例配置的服务名是DL580),实际就是配置tnsnames.ora文件。也可以安装简易oracle客户端,并用记事本修改tnsnames.ora文件,本例就是需要在该文件中增加如下内容(本例Oracle数据库服务器地址是10.178.10.197,SID是ORCL):

DL580 =
  (DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = TCP)(HOST = 10.178.10.197)(PORT = 1521))
    )
    (CONNECT_DATA =
      (SERVICE_NAME = orcl)
    )
  )

通过工作表保护使使用者只能修改参数值和状态,其他不能修改,防止破坏相关设置。

2)可以用循环实现参数的多次使用,这在SQL包含多个子查询,参数是日期的情况下很有用,每个子查询多会用到参数中的起止日期。循环中如果某个子查询只用到部分参数,可以用注释语句中加“?”的方式站位。例如:

  1. select b.city,
  2. b.ssxs,
  3. a.clct_bureau_org_code,
  4. b.zj_mc,
  5. a.sender_cust_code,
  6. a.sender_dept_name,
  7. min(a.clct_date),
  8. max(a.clct_date),
  9. count(*) yjzl,
  10. sum(a.actual_total_fee) yjsr
  11. from tb_evt_mail_clct a,
  12. (select * from sncn_zd_jg where jgfl = ‘sd’) b,
  13. (select distinct t.sender_cust_code
  14. from tb_evt_mail_clct t
  15. where t.clct_date < to_date(‘?’, ‘yyyy-mm-dd’)) c
  16. — ?  占位
  17. where a.clct_bureau_org_code = b.zj_code
  18. and a.clct_date between to_date(‘?’, ‘yyyy-mm-dd’) and
  19. to_date(‘?’, ‘yyyy-mm-dd’)
  20. and length(a.sender_cust_code) = 14
  21. and a.sender_cust_code = c.sender_cust_code(+)
  22. and c.sender_cust_code is null
  23. group by b.city,
  24. b.ssxs,
  25. a.clct_bureau_org_code,
  26. b.zj_mc,
  27. a.sender_cust_code,
  28. a.sender_dept_name
  29. order by b.city,
  30. b.ssxs,
  31. a.clct_bureau_org_code,
  32. b.zj_mc,
  33. a.sender_cust_code,
  34. a.sender_dept_name

 

附:简易客户端(版本9i)资源下载地址:http://download.csdn.net/detail/iamlaosong/5035733

       完整的工具包(含Oracle简易客户端资源下载地址:http://download.csdn.NET/detail/iamlaosong/5307186

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,492
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,493
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,295