我正在尝试使用 VB 在 excel 表中绘制图表。
所以现在我按照 这里给出的说明
1-我在VS2010中开始了一个新的VB项目,叫做Excelgraph。
2- 默认情况下,我得到了 Form1.vb[Design]。
3- 在这个表单上,我通过从工具箱中拖动它来创建一个按钮。
4-我双击它并打开新的 Form1.vb。
5-我删除了该文件中自动生成的所有内容,即 Form1.vb 文件并粘贴了以下代码:
更新代码
这是另一个代码,是最新的,与 Visual Basic 6.0 兼容。
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim oXL As Object ' Excel application
Dim oBook As Object ' Excel workbook
Dim oSheet As Object ' Excel Worksheet
Dim oChart As Object ' Excel Chart
Dim iRow As Integer ' Index variable for the current Row
Dim iCol As Integer ' Index variable for the current Row
Const cNumCols = 10 ' Number of points in each Series
Const cNumRows = 2 ' Number of Series
ReDim aTemp(0 To cNumRows, 0 To cNumCols)
'Start Excel and create a new workbook
oXL = CreateObject("Excel.application")
oBook = oXL.Workbooks.Add
oSheet = oBook.Worksheets.Item(1)
' Insert Random data into Cells for the two Series:
Randomize(Now().ToOADate())
For iRow = 1 To cNumRows
For iCol = 1 To cNumCols
aTemp(iRow, iCol) = Int(Rnd() * 50) + 1
Next iCol
Next iRow
oSheet.Range("A1").Resize(cNumRows, cNumCols).Value = aTemp
'Add a chart object to the first worksheet
oChart = oSheet.ChartObjects.Add(50, 40, 300, 200).Chart
oChart.SetSourceData(Source:=oSheet.Range("A1").Resize(cNumRows, cNumCols))
' Make Excel Visible:
oXL.Visible = True
oXL.UserControl = True
End Sub
End Class
更新
我更新了如上所示的代码。
错误
'aTemp' is not declared. It may be inaccessible due to its protection level.
c:\users\ybf4 \documents\visual studio 2010\Projects\Excelgraph2
\Excelgraph2\Form1.vb
我设法消除了另外两个错误。如何消除此错误?
我在 Visual Studio 2010 上编译上述代码,Office 是 Office 2007。