VB.NET的Excel图表(散点线)自动化 [英] Excel Chart(Scatter lines) automation from VB.NET
本文介绍了VB.NET的Excel图表(散点线)自动化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
任何人都可以帮我找出对以下代码进行哪些更改才能显示图表(仅当我单击工具栏上的切换行/列"按钮时,才显示我想要的图表)无需手动单击该按钮即可自动显示).以下是我的代码:
Hi,
Can anyone please help me in figuring out what change needs to be done to the below code in order to show the chart (the chart I want is displayed only when I click the "Switch Row/Column" button on the toolbar, I want it to be displayed automatically without manually clicking the button). Below is my code:
Private Sub DisplayGraph()
Try
Dim xlApp As Excel.Application
Dim xlWorkBook As Excel.Workbook
Dim xlWorkSheet As Excel.Worksheet
Dim misValue As Object = System.Reflection.Missing.Value
xlApp = New Excel.ApplicationClass
xlWorkBook = xlApp.Workbooks.Add(misValue)
xlWorkSheet = xlWorkBook.Sheets("sheet1")
xlApp.Visible = True
''Adding Data..
xlWorkSheet.Cells(1, 1) = 2
xlWorkSheet.Cells(1, 2) = 1
xlWorkSheet.Cells(2, 1) = 3
xlWorkSheet.Cells(2, 2) = 2
xlWorkSheet.Cells(3, 1) = 2
xlWorkSheet.Cells(3, 2) = 5
xlWorkSheet.Cells(4, 1) = 4
xlWorkSheet.Cells(4, 2) = 7
xlWorkSheet.Cells(5, 1) = 8
xlWorkSheet.Cells(5, 2) = 2
xlWorkSheet.Cells(6, 1) = 5
xlWorkSheet.Cells(6, 2) = 3
Dim oChart As Excel._Chart
oChart = xlWorkBook.Charts.Add(Missing.Value, Missing.Value, Missing.Value, Missing.Value)
oChart.ChartWizard(xlWorkSheet.Range("A1", "B6"), Excel.XlChartType.xlXYScatterLines, Missing.Value, _
Excel.XlRowCol.xlRows, _
Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, _
Missing.Value, Missing.Value)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
推荐答案
应该有一个更直接的解决方案,直接指定正确的格式等,但这可以解决您所描述的问题因为我们正在使用VB.Net,所以我删除了不需要的Missing引用. (我想您是从C#示例转换而来的.)
There should be a more direct solution that just specifies the right format, etc. straight away, but this fixes the problem as you''ve described it, and because we''re using VB.Net, I''ve removed the unneeded Missing references. (I guess you converted this from a C# sample.)
Private Sub DisplayGraph()
Try
Dim xlApp As Excel.Application
Dim xlWorkBook As Excel.Workbook
Dim xlWorkSheet As Excel.Worksheet
xlApp = New Excel.ApplicationClass
xlWorkBook = xlApp.Workbooks.Add()
xlWorkSheet = xlWorkBook.Sheets("sheet1")
xlApp.Visible = True
''Adding Data..
xlWorkSheet.Cells(1, 1) = 2
xlWorkSheet.Cells(1, 2) = 1
xlWorkSheet.Cells(2, 1) = 3
xlWorkSheet.Cells(2, 2) = 2
xlWorkSheet.Cells(3, 1) = 2
xlWorkSheet.Cells(3, 2) = 5
xlWorkSheet.Cells(4, 1) = 4
xlWorkSheet.Cells(4, 2) = 7
xlWorkSheet.Cells(5, 1) = 8
xlWorkSheet.Cells(5, 2) = 2
xlWorkSheet.Cells(6, 1) = 5
xlWorkSheet.Cells(6, 2) = 3
Dim oChart As Excel._Chart
oChart = xlWorkBook.Charts.Add()
oChart.ChartWizard(xlWorkSheet.Range("A1", "B6"), _
Excel.XlChartType.xlXYScatterLines, _
PlotBy:=Excel.XlRowCol.xlRows)
oChart.PlotBy = Excel.XlRowCol.xlColumns
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
这篇关于VB.NET的Excel图表(散点线)自动化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文