public
anonymous / gist:5348116
Created

  • Download Gist
gistfile1.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
'had to add the Excel and Office files from the COM tab in Add Reference, under Project menu item
Imports Excel = Microsoft.Office.Interop.Excel
 
Public Class Form1
 
Private Sub cmdAD_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAD.Click
 
Dim ADdata(,) As Integer
Dim xlApp As Excel.Application
Dim xlWorkBook As Excel.Workbook
Dim xlWorkSheet As Excel.Worksheet
Dim range As Excel.Range
Dim rCnt As Integer
Dim cCnt As Integer
Dim Obj As Object
Dim myfile As String
Dim mymessage As String
 
 
'prompt to select XLS file to load into array
myfile = ""
If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
myfile = OpenFileDialog1.FileName
End If
 
xlApp = New Excel.Application
xlWorkBook = xlApp.Workbooks.Open(myfile)
xlWorkSheet = xlWorkBook.Worksheets("sheet1")
range = xlWorkSheet.UsedRange
 
'reads from left to right, top down
For rCnt = 1 To range.Rows.Count
For cCnt = 1 To range.Columns.Count
Obj = CType(range.Cells(rCnt, cCnt), Excel.Range)
'mymessage = "Row " & rCnt & " Col " & cCnt & " Value " & Obj.value() <---- displays Row #, Col #, and Value
 
ADdata(rCnt, cCnt) = Obj.value
MsgBox(Obj.value)
Next
Next
 
'close file handles, release apps
xlWorkBook.Close()
xlApp.Quit()
 
releaseObject(xlApp)
releaseObject(xlWorkBook)
releaseObject(xlWorkSheet)
 
End Sub
'garbage collection and clean up
Private Sub releaseObject(ByVal obj As Object)
Try
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
obj = Nothing
Catch ex As Exception
obj = Nothing
Finally
GC.Collect()
End Try
End Sub
 
'ends program
Private Sub cmdExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdExit.Click
Close()
End Sub

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.