欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

vbscript ms owc 封裝代码

程序员文章站 2022-07-04 20:33:42
复制代码 代码如下:''owc.vbs class owc private o '传入物件 public property set set_obj(o_id) set o=...
复制代码 代码如下:

''owc.vbs
class owc
private o
'传入物件
public property set set_obj(o_id)
set o=o_id
end property
'画图矩形图
'chart_bgcolor_图表的背景颜色
'chartcaption_图表的标题
'chartcaption_fontcolor_图表标题颜色
'interior_color_矩形内的填充颜色
'caption_名称
'categories_名称数组
'values_值数组串
public sub bar(chart_bgcolor_,chartcaption_,chartcaption_fontcolor_,interior_color_,caption_,categories_,values_)
o.clear
set cht = o.charts.add
set c = o.constants
cht.type = c.chcharttypecolumnclustered
'设背景色或是填充
o.charts(0).plotarea.interior.setsolid chart_bgcolor_

'加上图表的标题
o.haschartspacetitle = true
set cst=o.chartspacetitle
cst.caption = chartcaption_
cst.font.color = chartcaption_fontcolor_
cst.font.italic = false
cst.font.name = "arial"
cst.font.size = 12
cst.font.underline = c.owcunderlinestylesingle

'添加数据
cht.setdata c.chdimcategories, c.chdataliteral, categories_
cht.seriescollection(0).setdata c.chdimvalues, c.chdataliteral, values_
'直条的背景色进行设定
set sc=o.charts(0).seriescollection(0)
sc.interior.color=interior_color_

'直条上的显示设置
sc.caption=caption_
set dl = cht.seriescollection(0).datalabelscollection.add
dl.hasvalue = true
dl.haspercentage = false
dl.font.size = 9
dl.font.color = "red"
dl.position = c.chlegendpositionright
dl.numberformat = "00.00%"
'左边百分比的属性设置
set cta = cht.axes(c.chaxispositionleft)
cta.font.size = 9
cta.numberformat = "0.0%"
cta.majorunit = 0.1
end sub
'多系列矩形图
'chart_bgcolor_图表的背景颜色
'chartcaption_图表的标题
'chartcaption_fontcolor_图表标题颜色
'color_颜色数组
'caption_名称数组
'categories_名称数组
'values_值数组
public sub serbar(chart_bgcolor_,chartcaption_,chartcaption_fontcolor_,color_,caption_,categories_,values_)
o.clear
o.charts.add
set c = o.constants
'图表的类型
o.charts(0).type=c.chcharttypecolumnclustered
'给绘图区加背景色
o.charts(0).plotarea.interior.setsolid chart_bgcolor_
''加上图表的标题
o.haschartspacetitle = true
o.chartspacetitle.caption = chartcaption_
'标题的属性
o.chartspacetitle.font.color = chartcaption_fontcolor_
o.chartspacetitle.font.italic = false
o.chartspacetitle.font.name = "arial"
o.chartspacetitle.font.size = 12
o.chartspacetitle.font.underline = c.owcunderlinestylesingle
'用循环来新增seriescollection以及里面的内容
for i=0 to ubound(caption_)
valuetemp=""
for j = i*(ubound(categories_)+1) to (i+1)*(ubound(categories_)+1)-1
valuetemp = valuetemp & "," & values_(j)
next
valuearr = split(mid(valuetemp,2),",")
o.charts(0).seriescollection.add
o.charts(0).seriescollection(i).caption = caption_(i)
o.charts(0).seriescollection(i).interior.color = color_(i)
o.charts(0).seriescollection(i).setdata c.chdimcategories, c.chdataliteral, categories_
o.charts(0).seriescollection(i).setdata c.chdimvalues, c.chdataliteral, valuearr
set dl = o.charts(0).seriescollection(i).datalabelscollection.add
dl.hasvalue = true
dl.haspercentage = false
dl.font.size = 9
dl.font.color = "red"
dl.position = c.chlegendpositionright
dl.numberformat = "00.00%"
next
''图例的设定
o.charts(0).haslegend = true
o.charts(0).legend.font.size = 9
o.charts(0).legend.position = c.chlegendpositionbottom
''左边百分比的属性设置
set cta = o.charts(0).axes(c.chaxispositionleft)
cta.font.size = 9
cta.numberformat = "0.00%"
cta.majorunit = 0.1
end sub
'画圆饼图
'chart_bgcolor_绘图区加背景色
'chartcaption_图表的标题
'chartcaption_fontcolor_图表标题颜色
public sub pie(chart_bgcolor_,chartcaption_,chartcaption_fontcolor_,caption_,categories_,values_)
o.clear
set cht = o.charts.add
set c = o.constants
cht.type = c.chcharttypepie3d
'给绘图区加背景色
o.charts(0).plotarea.interior.setsolid chart_bgcolor_
cht.extrudeangle = 90
cht.chartdepth = 169
cht.aspectratio = 120
cht.rotation =180
cht.inclination=70

o.haschartspacetitle = true
o.chartspacetitle.caption = chartcaption_
o.chartspacetitle.font.color = chartcaption_fontcolor_
o.chartspacetitle.font.name = "arial"
o.chartspacetitle.font.size = 12
o.chartspacetitle.font.underline = c.owcunderlinestylesingle

cht.haslegend = true
cht.legend.font.size = 9
cht.legend.position = c.chlegendpositionbottom

cht.setdata c.chdimcategories, c.chdataliteral, categories_
cht.seriescollection(0).setdata c.chdimvalues, c.chdataliteral, values_
set sc=o.charts(0).seriescollection(0)
sc.caption=caption_
set dl = cht.seriescollection(0).datalabelscollection.add
dl.separator = ":"
dl.hasvalue = false
dl.hasseriesname = false
dl.hascategoryname=true
dl.haspercentage = true
dl.font.size = 9
dl.font.color = "red"
dl.numberformat = "00.00%"
end sub
'拆线图
'chart_bgcolor_绘图区加背景色
'chartcaption_图表的标题
'chartcaption_fontcolor_图表标题颜色
public sub line(chart_bgcolor_,chartcaption_,chartcaption_fontcolor_,caption_,categories_,values_)
o.clear
set cht = o.charts.add
set c = o.constants
cht.type = c.chcharttypelinemarkers
'给绘图区加背景色
o.charts(0).plotarea.interior.setsolid chart_bgcolor_
o.haschartspacetitle = true
o.chartspacetitle.caption = chartcaption_
o.chartspacetitle.font.color = chartcaption_fontcolor_
o.chartspacetitle.font.name = "arial"
o.chartspacetitle.font.size = 12
o.chartspacetitle.font.underline = c.owcunderlinestylesingle

cht.setdata c.chdimcategories, c.chdataliteral, categories_
cht.seriescollection(0).setdata c.chdimvalues, c.chdataliteral, values_

set sc=o.charts(0).seriescollection(0)
sc.caption=caption_
set dl = cht.seriescollection(0).datalabelscollection.add
dl.hasvalue = true
dl.haspercentage = false
dl.font.size = 9
dl.font.color = "red"

set categoryaxis = cht.axes(c.chaxispositionbottom)
categoryaxis.font.size = 9

set categoryaxis = cht.axes(c.chaxispositionleft)
categoryaxis.font.size = 9
end sub
'多系列拆线图
'chart_bgcolor_图表的背景颜色
'chartcaption_图表的标题
'chartcaption_fontcolor_图表标题颜色
'color_颜色数组
'caption_名称数组
'categories_名称数组
'values_值数组
public sub serline(chart_bgcolor_,chartcaption_,chartcaption_fontcolor_,color_,seriesnames_,categories_,values_)
o.clear
set cht = o.charts.add
set c = o.constants
'设置图表类型
cht.type = c.chcharttypelinemarkers
'给绘图区加背景色
o.charts(0).plotarea.interior.color=chart_bgcolor_
'加上标题
o.haschartspacetitle = true
o.chartspacetitle.caption = chartcaption_
o.chartspacetitle.font.color = chartcaption_fontcolor_
o.chartspacetitle.font.name = "arial"
o.chartspacetitle.font.size = 12
''添加数据
cht.setdata c.chdimseriesnames, c.chdataliteral, seriesnames_
cht.setdata c.chdimcategories, c.chdataliteral, categories_

set categoryaxis = cht.axes(c.chaxispositionbottom)
categoryaxis.font.size = 9

set categoryaxis = cht.axes(c.chaxispositionleft)
categoryaxis.font.size = 9

for i = 0 to ubound(seriesnames_)
valuetemp = ""
for j = i*(ubound(categories_)+1) to (i+1)*(ubound(categories_)+1)-1
valuetemp = valuetemp & "," & values_(j)
next
valuearr = split(mid(valuetemp,2),",")
cht.seriescollection(i).setdata c.chdimvalues, c.chdataliteral, valuearr
cht.seriescollection(i).line.color = color_(i)
cht.seriescollection(i).line.weight = c.owclineweightthin
cht.seriescollection(i).marker.style = c.chmarkerstylediamond
cht.seriescollection(i).interior.color = color_(i)
set dl = cht.seriescollection(i).datalabelscollection.add
dl.hasvalue = true
dl.haspercentage = false
dl.font.size = 9
dl.font.color="red"
next
end sub
'清除图型
public sub clear()
o.clear
end sub
end class

使用测试代码:
复制代码 代码如下:

<html>
<head>
<title>vbscript owc class test</title>
<script language="vbscript" src="owc.vbs"></script>
<script language="vbscript">
sub window_onload()
set t = new owc

categories =array("a2-1","a2-2","a2-3","a3-1","a3-2","a3-3"_
,"b2-1","b2-2","b2-3","b3-1","b3-2","b3-3"_
,"c2-1","c2-2","c3-1","c3-2","c3-3")
values = array(0.813,0.689,0.800,0.833,0.681,0.864,0.743_
,0.894,0.822,0.874,0.746,0.908,0.850_
,0.728,0.731,0.734,0.624)
set t.set_obj=document.getelementbyid("cs1")
t.bar "#ffffff","报表标题","blue","#cccc00","组别",categories,values

color=array("#ccffff","#99ccff","#00ccff","#ff0000","#660066","#ff8080")
caption=array("第1周","第2周","第3周","第4周")
categories=array("a栋","b栋")
values=array(0.6951,0.5798,0.7075,0.512,0.7907,0.7493,0.8691,0.7716)
set t.set_obj=document.getelementbyid("cs2")
t.serbar "#ffffff","2008年10月坑头三厂各周各楼层达成率比较图","red",color,caption,categories,values

categories=array("台北","上海")
values = array(238760,912560.62)
set t.set_obj=document.getelementbyid("cs3")
t.pie "#ffffff","销售金额所佔比例(城市分类)","blue","城市",categories,values

categories =array("2008/11/1","2008/11/2","2008/11/3","2008/11/4","2008/11/5","2008/11/6","2008/11/7","2008/11/8","2008/11/9","2008/11/10","2008/11/11","2008/11/12","2008/11/13","2008/11/14","2008/11/15","2008/11/16","2008/11/17","2008/11/18","2008/11/19","2008/11/20","2008/11/21","2008/11/22","2008/11/23","2008/11/24","2008/11/25","2008/11/26","2008/11/27","2008/11/28","2008/11/29","2008/11/30")
values = array(67239.89,60400.26,7202.89,18453.12,28889.29,102136.25,78826.39,146291.6,93743.29,44204.44,45349.28,50730.6,37900.21,84359.87,169333.62,114594.96,75334.77,51360.32,55853.05,37912.63,45068.22,63930.98,72993.71,18699.5,24714.5,34792.91,39414.58,92171.2,74433.06,3998894.41)
set t.set_obj=document.getelementbyid("cs4")
t.line "#ffffff","销售金额","blue","日期",categories,values

color=array("#ccffff","#99ccff","#00ccff","#ff0000")
seriesnames = array("a产品","b产品","c产品","d产品")
categories = array("2005/3/24","2005/3/25","2005/3/26","2005/3/27","2005/3/28")
values = array(50,100,20,80,89,40,60,20,90,70,20,50,55,25,60,80,20,75,58,100)
set t.set_obj=document.getelementbyid("cs5")
t.serline "#ffffff","达成率月报表","red",color,seriesnames,categories,values

set t=nothing
end sub
</script>
</head>
<body>
<div id="c1"><object id="cs1" name="cs1" classid="clsid:0002e55d-0000-0000-c000-000000000046" codebase="owc11.dll" style="width:100%;height:350"></object></div>
<div id="c2"><object id="cs2" name="cs2" classid="clsid:0002e55d-0000-0000-c000-000000000046" codebase="owc11.dll" style="width:100%;height:350"></object></div>
<div id="c3"><object id="cs3" name="cs3" classid="clsid:0002e55d-0000-0000-c000-000000000046" codebase="owc11.dll" style="width:100%;height:350"></object></div>
<div id="c4"><object id="cs4" name="cs4" classid="clsid:0002e55d-0000-0000-c000-000000000046" codebase="owc11.dll" style="width:100%;height:350"></object></div>
<div id="c5"><object id="cs5" name="cs5" classid="clsid:0002e55d-0000-0000-c000-000000000046" codebase="owc11.dll" style="width:100%;height:350"></object></div>
</body>
</html>

owc是什么?
office web components是一组的com控件,设计的目的是为众多的控件容器提供交互的电子表格建模,数据报表和数据可视化功能。owc库包含四个主要的组件:电子表格组件,图表组件,透视表组件和数据源组件。
  “office web components”名称中的单词”office”表示这些控件就是由那些开发微软excel和access的程序员中的一部分人开发的,并且这些控件被设计成在外观,使用感受和行为表现上就像是他们的微软office兄弟的精简版一样。这些控件确实不包含excel和access中的所有的功能——换句话说,你也不会为了在浏览器中查看一个报表而动态的下载完整的excel和access,这些控件也确实包含了许多的常用功能,尤其是那些在与已有内容进行交互时所必需的功能。此外,这些控件还能够读写excel 2000的html格式的文件,允许用户点击一个按钮,将当前的数据加载到excel中,以便作更加强大的分析。