admin 管理员组文章数量: 887042
2024年1月23日发(作者:php为什么没落了)
VB中调用Excel2003生成Chart图表程序代码
在VB中通过调用Excel的图表制作功能可以生成各种复杂的图表,使编程过程得以简化。举例如下:新建工程,在Form1窗口添加Command1按钮,编写程序在Excel中添加数据并生成饼图。
Private Sub Command1_Click()
Dim x1 As ation '声明数据类型
Set x1 = CreateObject(""ation"")
'创建实例
'添加工作簿
e = True
(""A1"").Value = 1 'A1格赋值
(""B1"").Value = 2 'B1格赋值
(""C1"").Value = 3 'C1格赋值
(""D1"").Value = 4 'D1格赋值
(""A1"", ""D1"").yle = xlContinuous '单元格边框
ntalAlignment = xlVAlignCenter
alAlignment = xlVAlignCenter '上下、左右居中
Set ct = eets(""sheet1"").(10, 40, 220, 120) '插入图形
ype = xl3DPie '图形类型为饼图
rceData Source:=Sheets(""Sheet1"").Range(""A1:D1""), PlotBy:=xlRows
'图形数据来源
With
.HasTitle = True
. = ""饼图"" '图表标题为饼图
End With
ataLabels 2, True '标志旁附图例项标志
Set x1 = Nothing
End Sub
有人可能会觉得程序里对象、属性太多,自己记不住。有一个可以偷懒的方法。打开Excel,点击工具→宏→录制宏,按照步骤添加数据,设置格式,生成饼图,然后停止录制。这时点击工具→宏,再点击编辑,就会出现刚才所做事情的代码,将代码复制到VB中稍做改动就可以了。
aR1C1 = ""1""
Range(""B1"").Select
aR1C1 = ""2""
Range(""C1"").Select
aR1C1 = ""3""
Range(""D1"").Select
aR1C1 = ""4""
Range(""A1:D1"").Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.WrapText = False
.Orientation = 0
.AddIndent = False
.ShrinkToFit = False
.MergeCells = False
End With
s(xlDiagonalDown).LineStyle = xlNone
s(xlDiagonalUp).LineStyle = xlNone
With s(xlEdgeLeft)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With s(xlEdgeTop)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With s(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With s(xlEdgeRight)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With s(xlInsideVertical)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
Range(""A3"").Select
ype = xl3DPie
rceData Source:=Sheets(""Sheet1"").Range(""A1:D1""), PlotBy:= _
xlRows
on Where:=xlLocationAsObject, Name:=""Sheet1""
With ActiveChart
.HasTitle = True
. = ""饼图""
End With
ataLabels Type:=xlDataLabelsShowValue, LegendKey:=True, _
HasLeaderLines:=True
Excel自动生成的代码,自己还需要手工对它进行修改。用同样的方法还可以生成折线图、柱形图等的代码。
版权声明:本文标题:VB中调用Excel2003生成Chart图表程序代码 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.freenas.com.cn/free/1705983979h496673.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论