ÔÚ´úÂë´°¿ÚÖУ¬½«ÒÔÏ´úÂë Private Sub Button2_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button2.Click
End Sub
替换为: Private Sub Button2_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button2.Click
Dim objSheets As Excel.Sheets
Dim objSheet As Excel._Worksheet
Dim range As Excel.Range
'Get a reference to the first sheet of the workbook.
On Error Goto ExcelNotRunning
objSheets = objBook.Worksheets
objSheet = objSheets(1)
ExcelNotRunning:
If (Not (Err.Number = 0)) Then
MessageBox.Show("Cannot find the Excel workbook. Try clicking Button1 to " + _
"create an Excel workbook with data before running Button2.", _
"Missing Workbook?")
'We cannot automate Excel if we cannot find the data we created,
'so leave the subroutine.
Exit Sub
End If
'Get a range of data.
range = objSheet.Range("A1", "E5")
'Retrieve the data from the range.
Dim saRet(,) As Object
saRet = range.Value
'Determine the dimensions of the array.
Dim iRows As Long
Dim iCols As Long
iRows = saRet.GetUpperBound(0)
iCols = saRet.GetUpperBound(1)
'Build a string that contains the data of the array.
Dim valueString As String
valueString = "Array Data" + vbCrLf
Dim rowCounter As Long
Dim colCounter As Long
For rowCounter = 1 To iRows
For colCounter = 1 To iCols
'Write the next value into the string.
valueString = String.Concat(valueString, _
saRet(rowCounter, colCounter).ToString() + ", ")
Next colCounter
'Write in a new line.
valueString = String.Concat(valueString, vbCrLf)
Next rowCounter
'Report the value of the array.
MessageBox.Show(valueString, "Array Values")
'Clean up a little.
range = Nothing
objSheet = Nothing
objSheets = Nothing
End Sub
|
评论