在VB組件內調用Excel2000實現GIF餅圖
2024-07-21 02:15:42
供稿:網友
 
當我第一次使用excel的時候,就為excel的圖表功能所傾倒,實在強大,并且那些圖也挺漂亮了 
。后來我嘗試著在vb里面調用excel所支持的vba功能,發現功能的確強大,就是十分繁瑣。后來就考慮用vb在excel外面包一層,寫成對象,去掉我們不需要的特性。這樣掉用起來就方便多了,所謂一勞永逸 :p。
在這里,我將像大家介紹一個用vb編寫的餅圖組件,你只需要給它幾個簡單的參數,就可以生成一副gif格式的圖片給你。調用例子如下:
dim obj
set obj = createobject("chinaaspchart.pie")
obj.addvalue "男", 150
obj.addvalue "女", 45
obj.addvalue "不知道", 15
obj.chartname = "性別比例圖"
obj.filename = "d:/123.gif"
obj.savechart
除了在vb里面可以調用,這段代碼同樣也可以在asp里面調用。
下面編寫我們的組件。
1.new project , 請選擇activex dll,在project explorer面板上選擇project1,然后在屬性面板上修改其name為chinaaspchart。同樣把里面的class modules修改為pie
2.保存該project,將project存為chinaaspchart.vbp,將class1.cls存為pie.cls。
3.菜單project,選擇菜單項references,然后請把microsoft active server pages ojbect library、microsoft excel 9.0 object library、com+ services type library選上。
注意:在nt4/win98上沒有com+ service type library這個東東,應該選microsoft transaction server type library
4.編輯pie.cls,代碼如下:
注釋:------------------------------------------------------------------------------- 
dim xl
dim m_chartname
dim m_chartdata()
dim m_charttype
dim m_filename
public errmsg
public founderr
dim icount
type m_value
 label as string
 value as double
end type
dim tvalue as m_value
public property let charttype(charttype)
 m_charttype = charttype
end property
public property get charttype()
 charttype = m_charttype
end property
public property let chartname(chartname)
 m_chartname = chartname
end property
public property get chartname()
 chartname = m_chartname
end property
public property let filename(fname)
m_filename = fname
end property
public property get filename()
 filename = m_filename
end property
public sub addvalue(label, value)
 icount = icount + 1
 redim preserve m_chartdata(icount)
 tvalue.label = label
 tvalue.value = value
 m_chartdata(icount) = tvalue
end sub
public sub savechart()
 on error resume next
 dim isheet
 dim i
 set xl = new excel.application
 xl.application.workbooks.add
 xl.workbooks(1).worksheets("sheet1").activate
 if err.number <> 0 then
 founderr = true
 errmsg = err.description
 err.clear
 else
 xl.workbooks(1).worksheets("sheet1").cells("2,1").value = m_chartname
 for i = 1 to icount
 xl.worksheets("sheet1").cells(1, i + 1).value = m_chartdata(i).label
 xl.worksheets("sheet1").cells(2, i + 1).value = m_chartdata(i).value
 next
 xl.charts.add
 xl.activechart.charttype = m_charttype
 xl.activechart.setsourcedata xl.sheets("sheet1").range("a1:" & chr((icount mod 26) + asc("a")) & "2"), 1
 xl.activechart.location 2, "sheet1"
 with xl.activechart
 .hastitle = true
 .charttitle.characters.text = m_chartname
 end with
 xl.activechart.applydatalabels 2, false, _
 true, false
 with xl.selection.border
 .weight = 2
 .linestyle = 0
 end with
 xl.activechart.plotarea.select
 with xl.selection.border
 .weight = xlhairline
 .linestyle = xlnone
 end with
 xl.selection.interior.colorindex = xlnone
 xl.activewindow.visible = false
 xl.displayalerts = false
 xl.activechart.export m_filename, filtername:="gif"
 xl.workbooks.close
 if err.number <> 0 then
 founderr = true
 errmsg = errmsg
 err.clear
 end if
 end if
 set xl = nothing
end sub
private sub class_initialize()
 icount = 0
 founderr = false
 errmsg = ""
 m_charttype = -4102 注釋:xl3dpie
 注釋:54 注釋:柱狀圖
end sub
注釋:------------------------------------------------------------------------------- 
如果實現柱狀圖?
實際上前面的代碼已經實現了柱狀圖的功能,只是缺省是餅圖功能。調用代碼改成如下:
dim obj
set obj = createobj 本文來源于網頁設計愛好者web開發社區http://www.html.org.cn收集整理,歡迎訪問。|||ect("chinaaspchart.pie")
obj.addvalue "男", 150
obj.addvalue "女", 45
obj.addvalue "不知道", 15
obj.chartname = "性別比例圖"
obj.filename = "d:/123.gif"
obj.charttype=54
obj.savechart
在asp里面調用該組件畫圖并顯示它需要注意的地方。
(1)圖片必須生成在web目錄下。
(2)asp程序運行在多用戶環境下,必須加鎖處理
可以通過application實現。其邏輯如下:
if application("標志")=0 then
 顯示圖片
else
 application.lock
 生成圖片
 顯示圖片
 application("標志")=0
 application.unlock
end if
當然何時需要生成圖片置標志位,就需要您自己根據程序的要求來確定了。
總結:
com里面調用office組件是一個十分有用的技巧,它的優點是開發相對簡單,使用方便,適合企業級低訪問量,高業務要求的應用,缺點是占用系統資源高。
程序在windows 2000 server + office 2000 + vb6.0 上測試通過。
 本文來源于網頁設計愛好者web開發社區http://www.html.org.cn收集整理,歡迎訪問。