Ref:http://blog.csdn.net/iamlaosong/article/details/8465177mysql
Excel經過ADO方式鏈接到Oracle並操做Oracle給那些編程能力不強的人帶來很大的福音,結合着Excel的數據處理與圖表製做,就能很輕鬆地處理一些常規工做。sql
平常工做中須要查詢各類數據,並且不斷變化,處理這些數據的人不是技術人員,不會鏈接數據庫本身查詢,經過下面的辦法就可讓技術人員編輯好包含查詢語句的excel文件,讓管理人員本身輸入條件取數了。數據庫
個人方法是編輯須要的SQL語句保存在單元格中,並在查詢條件須要參數值的地方用問號「?」代替,再在其餘單元格中保存查詢條件所需的參數值,在「宏」中用參數值替換掉SQL語句中的問號,最後執行查詢語句並將結果保存到excel表中。編程
如下是經過Excel的VBA鏈接Oracle並讀取Oracle相關數據的步驟:服務器
一、引用ADO相關組件:oracle
打開VBA編輯器,在菜單中點選「工具」--》「引用」。確保「Microsoft ActiviteX Data Objects 2.8 Library」和「Microsoft ActiviteX Data ObjectS Recordset 2.8 Library」被勾選上。編輯器
二、創建讀取數據的過程:ide
- Public Sub get_data()
- '根據工做表中的查詢語句讀取數據
- On Error GoTo ErrMsg:
-
- Dim cnn As Object, rst As Object
- Dim name, stat, sqls, field As String
- Dim pn(4), pm(4) As String
- Dim i, j, kk, pmkk, lineno As Integer
- Dim OraOpen As Boolean
-
- Set cnn = CreateObject("ADODB.Connection")
- Set rst = CreateObject("ADODB.Recordset")
- sqls = "connect database"
- cnn.Open "Provider=msdaora;Data Source=dl580;User Id=sxjkuser;Password=sxjkpasswd;"
- OraOpen = True '成功執行後,數據庫即被打開
-
- If OraOpen Then lineno = [D65536].End(xlUp).Row Else lineno = 0 '行數
-
- Application.Calculation = xlManual
- For i = 3 To lineno
- stat = Trim(Cells(i, 3))
-
- If stat = "Y" Or stat = "y" Then
- name = Cells(i, 2)
- field = Cells(i, 4)
- pn(1) = Cells(i, 5)
- pm(1) = Cells(i, 6)
- pn(2) = Cells(i, 7)
- pm(2) = Cells(i, 8)
- pn(3) = Cells(i, 9)
- pm(3) = Cells(i, 10)
- pn(4) = Cells(i, 11)
- pm(4) = Cells(i, 12)
- pmkk = Cells(i, 13)
- sqls = Cells(i, 15)
- 'MsgBox sqls
-
- For kk = 1 To pmkk '用於參數屢次使用,如聯合SQL語句中每一個子句都須要日期參數
- For j = 1 To 4
- If pn(j) <> "" Then
- sqls = Replace(sqls, "?", pm(j), 1, 1)
- 'MsgBox sqls
- End If
- Next j
- Next kk
- MsgBox sqls
- Set rst = cnn.Execute(sqls)
- sqls = "clear sheets"
- maxrow = Sheets(name).UsedRange.Rows.Count
- Sheets(name).Range("a2:" & field & maxrow).ClearContents
- sqls = "CopyFromRecordset"
- Sheets(name).Range("a2").CopyFromRecordset rst
- Cells(i, 3) = "成功"
- 'MsgBox i
- End If
- Next i
-
- 'rst.Close
- 'Set rst = Nothing
- cnn.Close
- Set cnn = Nothing
-
- Application.Calculation = xlAutomatic
- 'Sheets("分析").PivotTables("數據透視表1").PivotCache.Refresh
- Worksheets("系統參數").Select
- msg = MsgBox("數據讀取完畢!", vbOKOnly, "iamlaosong")
- Exit Sub
- ErrMsg:
- OraOpen = False
- MsgBox sqls, vbCritical, "操做失敗 ,請檢查!"
-
- End Sub
三、SQL語句實例工具
這是一個簡單的語句:.net
- SELECT * FROM zdgc_sn_sj_gfl t
- WHERE t.CLCT_DATE = to_date('?', 'yyyy-mm-dd')
- AND t.JSBZ = '1'
- ORDER BY t.CITY, t.SSXS
這是一個複雜的語句:
- select aa.zj_code,
- aa.zj_mc,
- aa.clct_date,
- aa.sjzl,
- aa.jyqsjzl,
- nvl(bb.wgfsl, 0),
- nvl(bb.jyqwgfsl, 0)
- from (select b.ssxs,
- b.zj_code,
- b.zj_mc,
- a.clct_date,
- count(*) sjzl,
- sum(case
- when to_char(a.clct_time, 'hh24mi') <= '?' then
- 1
- else
- 0
- end) jyqsjzl
- from tb_evt_mail_clct a, sncn_zd_jg b
- where a.clct_bureau_org_code = b.zj_code
- and a.time_limit_code <> '6'
- and a.mail_kind_code <> '10401'
- and a.addi_service_code <> '1'
- and (a.rcv_area like '23%' or a.rcv_area like '24%')
- group by b.ssxs, b.zj_code, b.zj_mc, a.clct_date) aa
- left join (select b.ssxs,
- b.zj_code,
- b.zj_mc,
- a.clct_date,
- count(*) wgfsl,
- sum(decode(jybz, 'b', 1, 0)) jyqwgfsl
- from sncn_zd_jg b, zdgc_sn_sj_errfc a
- where a.zj_code = b.zj_code
- and a.jsbz = '1'
- and a.jybz = 'b'
- group by b.ssxs, b.zj_code, b.zj_mc, a.clct_date) bb on aa.ssxs =
- bb.ssxs
- and aa.zj_code =
- bb.zj_code
- and aa.clct_date =
- bb.clct_date
- where aa.clct_date = to_date('?', 'yyyy-mm-dd')
- and aa.ssxs = '?'
- order by aa.zj_code, aa.zj_mc
四、操做界面
這是一個:
這是另外一個:
五、說明
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包含多個子查詢,參數是日期的狀況下頗有用,每一個子查詢多會用到參數中的起止日期。循環中若是某個子查詢只用到部分參數,能夠用註釋語句中加「?」的方式站位。例如:
- select b.city,
- b.ssxs,
- a.clct_bureau_org_code,
- b.zj_mc,
- a.sender_cust_code,
- a.sender_dept_name,
- min(a.clct_date),
- max(a.clct_date),
- count(*) yjzl,
- sum(a.actual_total_fee) yjsr
- from tb_evt_mail_clct a,
- (select * from sncn_zd_jg where jgfl = 'sd') b,
- (select distinct t.sender_cust_code
- from tb_evt_mail_clct t
- where t.clct_date < to_date('?', 'yyyy-mm-dd')) c
- -- ? 佔位
- where a.clct_bureau_org_code = b.zj_code
- and a.clct_date between to_date('?', 'yyyy-mm-dd') and
- to_date('?', 'yyyy-mm-dd')
- and length(a.sender_cust_code) = 14
- and a.sender_cust_code = c.sender_cust_code(+)
- and c.sender_cust_code is null
- group by b.city,
- b.ssxs,
- a.clct_bureau_org_code,
- b.zj_mc,
- a.sender_cust_code,
- a.sender_dept_name
- order by b.city,
- b.ssxs,
- a.clct_bureau_org_code,
- b.zj_mc,
- a.sender_cust_code,
- a.sender_dept_name
附:簡易客戶端(版本9i)資源下載地址:http://download.csdn.net/detail/iamlaosong/5035733
完整的工具包(含Oracle簡易客戶端)資源下載地址:http://download.csdn.NET/detail/iamlaosong/5307186