Private Sub Command2_Click()
Dim pType As Integer '图表类型1---柱形图,2---饼图
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Add
Set xlSheet = xlBook.Worksheets(1)
xlSheet.Cells(1, 1).Value = "安徽"
xlSheet.Cells(1, 2).Value = 1
xlSheet.Cells(2, 1).Value = "天津"
xlSheet.Cells(2, 2).Value = 2
xlSheet.Cells(3, 1).Value = "四川"
xlSheet.Cells(3, 2).Value = 3
Charts.Add '添加图表
pType = 2 '图表类型1---柱形图,2---饼图
Select Case pType
Case 1 '1---柱形图
ActiveChart.ChartType = xlColumnClustered
ActiveChart.SetSourceData Source:=Sheets("Sheet1").Range("A1:B3"), PlotBy:= _
xlColumns
ActiveChart.Location Where:=xlLocationAsObject, Name:="Sheet1"
With ActiveChart
.HasTitle = False
.Axes(xlCategory, xlPrimary).HasTitle = False
.Axes(xlValue, xlPrimary).HasTitle = False
End With
Case 2 '2---饼图
ActiveChart.ChartType = xlPie
ActiveChart.SetSourceData Source:=Sheets("Sheet1").Range("A1:B3"), PlotBy:= _
xlColumns
ActiveChart.Location Where:=xlLocationAsObject, Name:="Sheet1"
With ActiveChart
.HasTitle = True
.ChartTitle.Characters.Text = "分布情况统计图"
End With
End Select
'显示表格
Dim ExclFileName As String
ExclFileName = App.Path & "\统计图" & ".xls"
If Dir(ExclFileName) <> "" Then
Kill ExclFileName
End If
xlApp.Application.Visible = True
'交还控制给Excel
' xlSheet.PrintPreview
' xlApp.Application.Quit
' xlApp.Quit
' End With
End Sub