zuhalterr
July 14th, 2009, 11:39 AM
I have a problem with the function below. If anybody knows of a simpler way to accomplish the same results, then please advice.
The problem that I have with this function is that sometimes, not always, it keeps the Excel process running and when this happens I can run the function again until I kill the Excel process manually.
Another problem with this function is that it is slow if the Excel sheet contains a lot of data.
And also I can't get this function to read all the sheets in the Excel workbook and put the contents of each sheet in a separate data table in my dataset.
Thanks in advance for all your help. :confused::confused:
Public Function StreamExcelData() As Boolean
Dim Connection As System.Data.OleDb.OleDbConnection
Dim Command As System.Data.OleDb.OleDbDataAdapter
Dim OpenFileDB As New OpenFileDialog
Dim i As Integer
Dim Sheet As String
Dim myExcel, myWorkBook As Object
With OpenFileDB
.Filter = "All files | *.* | Excel files | *.xl*"
.FilterIndex = 2
.InitialDirectory = "C:\My Documents"
.AddExtension = True
.DefaultExt = "xls"
.CheckFileExists = True
.CheckPathExists = True
.Title = "Open Input Source File"
End With
If (OpenFileDB.ShowDialog() = DialogResult.OK) Then
gblSaveNewFile = OpenFileDB.FileName
gblFileName = Left(gblSaveNewFile, InStr(gblSaveNewFile, ".") - 1)
gblFileName = gblFileName.Substring(InStrRev(gblFileName, "\"))
Try
Connection = New System.Data.OleDb.OleDbConnection( _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & gblSaveNewFile & ";" & _
"Extended Properties=""Excel 8.0;HDR=YES;IMEX=1""")
myExcel = CreateObject("Excel.Application")
'Open the selected Excel document
myWorkBook = myExcel.WorkBooks.Open(gblSaveNewFile)
'Select the first sheet from the document
Sheet = myWorkBook.Worksheets(1).Name
'Import the data from Excel's first sheet
Command = New System.Data.OleDb.OleDbDataAdapter("SELECT * FROM [" & Sheet & "$]", Connection)
Command.TableMappings.Add("MyDataSample", "MyDataSample")
MyDs = New DataSet()
Command.Fill(MyDs)
myWorkBook.Close()
myExcel.Quit()
myExcel = Nothing
myWorkBook = Nothing
Connection.Close()
GC.Collect()
'Rename the column names by replacing the spaces with _ where applicable
For i = 0 To MyDs.Tables(0).Columns.Count - 1
MyDs.Tables(0).Columns(i).ColumnName = Trim(MyDs.Tables(0).Columns(i).ColumnName).Replace(" ", "_")
Next
MyTable = New DataTable("MY_DATA_TABLE")
MyTable = MyDs.Tables(0).Copy
Return True
Catch ex As System.Exception
System.Windows.Forms.MessageBox.Show(ex.Message)
myExcel = Nothing
myWorkBook = Nothing
Return False
End Try
myWorkBook.Close()
myExcel.Quit()
myExcel = Nothing
myWorkBook = Nothing
Connection.Close()
GC.Collect()
Else
Return False
End If
End Function
The problem that I have with this function is that sometimes, not always, it keeps the Excel process running and when this happens I can run the function again until I kill the Excel process manually.
Another problem with this function is that it is slow if the Excel sheet contains a lot of data.
And also I can't get this function to read all the sheets in the Excel workbook and put the contents of each sheet in a separate data table in my dataset.
Thanks in advance for all your help. :confused::confused:
Public Function StreamExcelData() As Boolean
Dim Connection As System.Data.OleDb.OleDbConnection
Dim Command As System.Data.OleDb.OleDbDataAdapter
Dim OpenFileDB As New OpenFileDialog
Dim i As Integer
Dim Sheet As String
Dim myExcel, myWorkBook As Object
With OpenFileDB
.Filter = "All files | *.* | Excel files | *.xl*"
.FilterIndex = 2
.InitialDirectory = "C:\My Documents"
.AddExtension = True
.DefaultExt = "xls"
.CheckFileExists = True
.CheckPathExists = True
.Title = "Open Input Source File"
End With
If (OpenFileDB.ShowDialog() = DialogResult.OK) Then
gblSaveNewFile = OpenFileDB.FileName
gblFileName = Left(gblSaveNewFile, InStr(gblSaveNewFile, ".") - 1)
gblFileName = gblFileName.Substring(InStrRev(gblFileName, "\"))
Try
Connection = New System.Data.OleDb.OleDbConnection( _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & gblSaveNewFile & ";" & _
"Extended Properties=""Excel 8.0;HDR=YES;IMEX=1""")
myExcel = CreateObject("Excel.Application")
'Open the selected Excel document
myWorkBook = myExcel.WorkBooks.Open(gblSaveNewFile)
'Select the first sheet from the document
Sheet = myWorkBook.Worksheets(1).Name
'Import the data from Excel's first sheet
Command = New System.Data.OleDb.OleDbDataAdapter("SELECT * FROM [" & Sheet & "$]", Connection)
Command.TableMappings.Add("MyDataSample", "MyDataSample")
MyDs = New DataSet()
Command.Fill(MyDs)
myWorkBook.Close()
myExcel.Quit()
myExcel = Nothing
myWorkBook = Nothing
Connection.Close()
GC.Collect()
'Rename the column names by replacing the spaces with _ where applicable
For i = 0 To MyDs.Tables(0).Columns.Count - 1
MyDs.Tables(0).Columns(i).ColumnName = Trim(MyDs.Tables(0).Columns(i).ColumnName).Replace(" ", "_")
Next
MyTable = New DataTable("MY_DATA_TABLE")
MyTable = MyDs.Tables(0).Copy
Return True
Catch ex As System.Exception
System.Windows.Forms.MessageBox.Show(ex.Message)
myExcel = Nothing
myWorkBook = Nothing
Return False
End Try
myWorkBook.Close()
myExcel.Quit()
myExcel = Nothing
myWorkBook = Nothing
Connection.Close()
GC.Collect()
Else
Return False
End If
End Function