如果文件没有图表,调用删除就会出错
Dim App As New MSExcel.Application
Dim Wb As MSExcel.WorkBook = App.WorkBooks.Open("D:\Report.xls")
Dim Ws As MSExcel.WorkSheet = Wb.WorkSheets(1)'指定工作表
Dim Rg As MSExcel.Range = Ws.Range("A2:B18") '图表数据源
try
ws.ChartObjects.Delete'删除工作表上已经存在的图表
catch
End try
Dim Co As MSExcel.ChartObject = Ws.ChartObjects.Add(100,30,400,250) '创建图表对象
Dim Cht As MSExcel.Chart = Co.Chart
Cht.ChartType = MSExcel.XlChartType.xlLineMarkers'图表类型
Cht.SetSourceData(Rg,MSExcel.XlRowCol.xlColumns) '指定数据源和绘图方式
Cht.HasTitle = True
Cht.ChartTitle.Text = "测试图表标题"
With Cht.ChartTitle.Font '设置标题字体
.Size = 16
.ColorIndex = 3
.Name = "隶书"
End With
App.Visible = True