How to Import Specific Data from Excel Sheet to DataGridView ?

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I want to import Data from Column A to Column H and from Row 14 to the end.
<img alt="" src="http://social.msdn.microsoft.com/Forums/getfile/136508
I use this code but its not working :

<pre class="prettyprint lang-vb Dim SourceBookPath As String = Nothing
Me.OpenFileDialog1.Filter = "Excel files (*.xls,*.xlsx)|*.xls;*.xlsx"
Me.OpenFileDialog1.ShowDialog()
If Not Me.OpenFileDialog1.FileName Is Nothing Then
SourceBookPath = Me.OpenFileDialog1.FileName
End If
=================================================
Dim MsExcel = CreateObject("Excel.Application")
Dim ExcelBook = MsExcel.Workbooks.Open(SourceBookPath)

Dim con As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & SourceBookPath & ";Extended Properties=Excel 8.0;HDR=Yes;IMEX=1"
Dim str As String = "select * from Sheet1"
Dim ds As New DataSet



Dim I, J As Integer
Dim lastRow As Long = 0


lastRow = MsExcel.Range("A15").Find("*", MsExcel.Range("A15"), XlFindLookIn.xlValues, XlSearchOrder.xlByRows, XlSearchDirection.xlPrevious).row
For I = 8 To lastRow
Dim r As DataRow = ds.Tables("Sheet1").NewRow
For J = 1 To 8
r(J - 1) = MsExcel.Cells(I, J).Value
Next J
ds.Tables("Sheet1").Rows.Add(r)
Next I
DataGridView1.DataSource = ds.Tables("Sheet1")[/code]
<br/>
Any one can help me Please..

View the full article
 
Back
Top