Created
April 9, 2013 18:28
-
-
Save anonymous/5348116 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment