欢迎来到天天文库
浏览记录
ID:43486048
大小:29.01 KB
页数:4页
时间:2019-10-08
《VB中调用Excel生成图表》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、VB中调用Excel生成图表添加时间:2004-9-3 查看:298次 编写数据库程序的最后步骤一般都是通过查询检索生成各种报表、图形等,在VB中通过调用Excel的图表制作功能可以生成各种复杂的图表,使编程过程得以简化。举例如下:新建工程,在Form1窗口添加Command1按钮,编写程序在Excel中添加数据并生成饼图。 PrivateSubCommand1_Click() Dimx1AsExcel.Application'声明数据类型 Setx1=CreateObject(""Excel.Application"")
2、 '创建实例 x1.Workbooks.Add '添加工作簿 x1.Visible=True x1.Range(""A1"").Value=1'A1格赋值 x1.Range(""B1"").Value=2'B1格赋值 x1.Range(""C1"").Value=3'C1格赋值 x1.Range(""D1"").Value=4'D1格赋值 x1.Range(""A1"",""D1"").Borders.LineStyle=xlContinuous'单元格边框 x1.ActiveSheet.Rows.Hori
3、zontalAlignment=xlVAlignCenter x1.ActiveSheet.Rows.VerticalAlignment=xlVAlignCenter'上下、左右居中 Setct=x1.Worksheets(""sheet1"").ChartObjects.Add(10,40,220,120)'插入图形 ct.Chart.ChartType=xl3DPie'图形类型为饼图 ct.Chart.SetSourceDataSource:=Sheets(""Sheet1"").Range(""A1:D1""),PlotB
4、y:=xlRows'图形数据来源 Withct.Chart .HasTitle=True .ChartTitle.Characters.Text=""饼图""'图表标题为饼图 EndWith ct.Chart.ApplyDataLabels2,True'标志旁附图例项标志 Setx1=Nothing EndSub 有人可能会觉得程序里对象、属性太多,自己记不住。有一个可以偷懒的方法。打开Excel,点击工具→宏→录制宏,按照步骤添加数据,设置格式,生成饼图,然后停止录制。这时点击工具→宏,再点击编辑,就会出现刚才
5、所做事情的代码,将代码复制到VB中稍做改动就可以了。 ActiveCell.FormulaR1C1=""1"" Range(""B1"").Select ActiveCell.FormulaR1C1=""2"" Range(""C1"").Select ActiveCell.FormulaR1C1=""3"" Range(""D1"").Select ActiveCell.FormulaR1C1=""4"" Range(""A1:D1"").Select WithSelection .Horizontal
6、Alignment=xlCenter .VerticalAlignment=xlCenter .WrapText=False .Orientation=0 .AddIndent=False .ShrinkToFit=False .MergeCells=False EndWith Selection.Borders(xlDiagonalDown).LineStyle=xlNone Selection.Borders(xlDiagonalUp).LineStyle=xlNone WithSelection.B
7、orders(xlEdgeLeft) .LineStyle=xlContinuous .Weight=xlThin .ColorIndex=xlAutomatic EndWith WithSelection.Borders(xlEdgeTop) .LineStyle=xlContinuous .Weight=xlThin .ColorIndex=xlAutomatic EndWith WithSelection.Borders(xlEdgeBottom) .LineStyle=xlContinuous
8、 .Weight=xlThin .ColorIndex=xlAutomatic EndWith WithSelection.Borde
此文档下载收益归作者所有