Hi Friends
Thanks to all of you for your generousity in sharing information with me. Finally I got the coding working and would like to post it here to share with other Newbies who may have similar problems.
I am so glad to meet you helpers here. this forum is awesome.
Here are the Working Codes:Code:Option Explicit On Imports System.Data.SqlClient Imports System.Data Imports System.Text Imports Excel = Microsoft.Office.Interop.Excel Imports System.Windows.Forms Public Class FrmGetExcel Dim sqlconn As SqlConnection Dim sqlcmd As SqlCommand Dim DA As SqlDataAdapter Dim DR As SqlDataReader Dim DS As System.Data.DataSet Dim DT As System.Data.DataTable 'common variable Dim connstr As String Dim excelPathName As String = String.Empty Dim bolUpdate As Boolean = False ' false = update not done ------------------------------------------------------------------------- Private Sub btnFolderDialog_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFolderDialog.Click 'prompt user to select Excel name and folder path Dim openFileDialog1 As System.Windows.Forms.OpenFileDialog openFileDialog1 = New System.Windows.Forms.OpenFileDialog With OpenFileDialog1 .Title = "Excel Spreadsheet" .FileName = "" .DefaultExt = ".xls" .AddExtension = True .Filter = "Excel (*.xls)| *.xls|All File(*.xls)|.xls" If .ShowDialog = Windows.Forms.DialogResult.OK Then excelPathName = (CType(.FileName, String)) If (excelPathName.Length) <> 0 Then Me.txtExcelFolderName.Text = excelPathName Else End If End If End With End Sub --------------------------------------------------- Private Sub btnOpenExcel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpenExcel.Click 'test open up excel spreadsheet Dim objExcel As New Excel.Application Dim objBook As Excel.Workbook = objExcel.Workbooks.Open(excelPathName) Dim objSheet As Excel.Worksheet = objBook.Worksheets(1) objExcel.Visible = True 'Dim objExcel As Excel.Application = CType(CreateObject("Excel.Application"), Excel.Application) 'Dim objBook As Excel.Workbook = CType(objExcel.Workbooks.Open(excelPathName), Excel.Workbook) 'objBook.Activate() 'Dim objSheet As Excel.Worksheet = CType(objExcel.Worksheets(1), Excel.Worksheet) 'objExcel.Visible = True 'objSheet.Activate() Dim bolFlag As Boolean = True Dim excelRow As Integer = 7 Dim excelCol As Integer = 1 Dim DGVRow As Integer = 1 Dim strCell1 As String Dim strCell2 As String Dim strCell3 As String Dim strCell4 As String Dim strCell5 As String Dim strCell6 As String Dim strCell7 As String Dim strCell8 As String Dim strCell9 As String Try Do While bolFlag = True If Convert.ToString(objSheet.Cells(excelRow, 1).value) = "" Then bolFlag = False Exit Do End If With DataGridView1 strCell1 = CType(objSheet.Cells(excelRow, 1).value, String) strCell2 = CType(objSheet.Cells(excelRow, 2).value, String) strCell3 = CType(objSheet.Cells(excelRow, 3).value, String) strCell4 = CType(objSheet.Cells(excelRow, 4).value, String) strCell5 = CType(objSheet.Cells(excelRow, 5).value, String) strCell6 = CType(objSheet.Cells(excelRow, 6).value, String) strCell7 = CType(objSheet.Cells(excelRow, 7).value, String) strCell8 = CType(objSheet.Cells(excelRow, 8).value, String) strCell9 = CType(objSheet.Cells(excelRow, 9).value, String) .Rows.Add(New String() {strCell1, strCell2, strCell3, strCell4, strCell5, strCell6, strCell7, strCell8, strCell9}) DGVRow += 1 excelRow += 1 strCell1 = "" strCell2 = "" strCell3 = "" strCell4 = "" strCell5 = "" strCell6 = "" strCell7 = "" strCell8 = "" strCell9 = "" End With Loop Catch ex As Exception MessageBox.Show(ex.Message) Finally objBook.Close() objExcel.Quit() End Try End Sub End clase




Reply With Quote