当我第一次使用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里面调用。 下面请follow me 编写我们的组件。 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 '------------------------------------------------------------------------------- 5. 如果实现柱状图? 实际上前面的代码已经实现了柱状图的功能,只是缺省是饼图功能。调用代码改成如下: dim obj set obj = createobject("chinaaspchart.pie") obj.addvalue "男",150 obj.addvalue "女",45 obj.addvalue "不知道",15 obj.chartname = "性别比例图" obj.filename = "d:/123.gif" obj.charttype=54 obj.savechart 6. 在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 上测试通过。
原帖地址:
http://topic.csdn.net/u/20100404/21/fb870a7c-9f3d-405d-adc7-b7c8b7f56600.html (编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|