想要每个表生成一个图表,遍历各个表的时候失败,只有第一个成功怎么办
Dim a As Integer = CInt(e.Form.Controls("TextBox1").text)
Dim app As New MSExcel.Application()
app.Visible = True
Dim book As MSExcel.Workbook = app.Workbooks.Open("D:\临沂分段.xls") '指定excel文件
For j As Integer = a To 1 Step -1
Dim ws As MSExcel.Worksheet = book.Sheets("临沂第" & j & "段") '指定工作表
'Dim ws As MSExcel.Worksheet = book.Sheets(j)
If ws.ChartObjects.count > 0 Then
ws.ChartObjects.Delete '删除工作表上已经存在的图表
End If
Dim Rg1 As MSExcel.Range = ws.Range("C3:C120")
Dim Rg2 As MSExcel.Range = ws.Range("D1:E120")
Dim Co As MSExcel.ChartObject = ws.ChartObjects.Add(100,30,400,250) '创建图表对象
Dim Cht As MSExcel.Chart = Co.Chart
Dim s As MSExcel.Series
With Cht
.ChartType = MSExcel.XlChartType.xlLine
.SetSourceData(Rg2)
s = Cht.SeriesCollection(1)
s.XValues = Rg1
End With
With Cht.Axes(MSExcel.XlAxisType.xlCategory) '获取X轴
.HasTitle = True
.AxisTitle.Text = "手部距离" '添加标题
.HasMajorGridlines = True '显示主要网格线
'.HasMinorGridlines = True '显示次要网格线
.MinimumScale = 100
.MaximumScale = 500
.MinorUnit = 1
.MajorUnit = 10
.CrossesAt = 400
.ReversePlotOrder = False
End With
With Cht.Axes(MSExcel.XlAxisType.xlValue) '获取Y轴
.HasTitle = True
.AxisTitle.Text = "埋深(m)" '添加标题
.HasMajorGridlines = True'显示只要网格线
.HasMinorGridlines = True'显示次要网格线
End With
Next j
链接:https://pan.baidu.com/s/1hfs1MZt3cIKS6MBGfM4V6Q
提取码:nkxh
[此贴子已经被作者于2024/5/30 17:38:03编辑过]
另外想加一个值为常数的序列要怎么操作,必须先在表中插入列吗?
这个语句就一个图也不能生成是怎么回事呢?Dim ws As MSExcel.Worksheet = book.Sheets(j)
未指定的错误 (异常来自 HRESULT:0x80004005 (E_FAIL))
去掉下面代码
If ws.ChartObjects.count > 0 Then
ws.ChartObjects.Delete '删除工作表上已经存在的图表
End If
必须先在表中插入列
实例已上传链接:https://pan.baidu.com/s/19vIzu9lu7S0PudibN9oxpA
提取码:1333