Question

Please help me guys, its my first time to try importing excel file to datagridview in vb.net. The scenario is that my excel file have so many data and i only need to import the value starting in column B to column I, and start rows must be in column B with the word "START" and ends in the row with the word "END". So far this was my code:

 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    OpenFileDialog1.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.Desktop
    OpenFileDialog1.Filter = "CSV Files (*.csv)|*.csv"

    If OpenFileDialog1.ShowDialog(Me) = System.Windows.Forms.DialogResult.OK Then

        Try
            Dim fi As New FileInfo(OpenFileDialog1.FileName)
            Dim sConnectionStringz As String = "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=Text;Data Source=" & fi.DirectoryName
            Dim objConn As New OleDbConnection(sConnectionStringz)
            objConn.Open()
            'DataGridView1.TabIndex = 1
            Dim objCmdSelect As New OleDbCommand("SELECT * FROM " & fi.Name, objConn)
            Dim objAdapter1 As New OleDbDataAdapter
            objAdapter1.SelectCommand = objCmdSelect
            Dim objDataset1 As New DataSet
            objAdapter1.Fill(objDataset1)
            '--objAdapter1.Update(objDataset1) '--updating
            DataGridView1.DataSource = objDataset1.Tables(0).DefaultView

            objConn.Close()
        Finally


        End Try


    End If
End Sub

And this code imports/shows all the data in datagridview. I already search everywhere but nothing seems to fit. Thanks anyways..

This was the sample file:

CLIENT-RD/OT HC 8   7   8   10  11  12  10
C-RD/OT 6:00    0   0   0   0   0   0   0
C-RD/OT 7:30    0   0   0   0   0   0   0
C-RD/OT 8:00    0   0   0   0   0   0   0
C-RD/OT 9:00    8   7   8   10  11  12  10
C-RD/OT 10:00   0   0   0   0   0   0   0
C-RD/OT 12:00   0   0   0   0   0   0   0
C-RD/OT 1:30    0   0   0   0   0   0   0

ML      0   0   0   0   0   0   0
VL/SIL  2   4   1   1   1   2   2
SL         1    0   1   1   1   0   0

START                   20-Jan  21-Jan  22-Jan  23-Jan  24-Jan  25-Jan  26-Jan
CEBU                     MON    TUE      WED    THU       FRI       SAT   SUN
Team Edna - Esca                            
Adarayan, Dianne          RD    RD  6:00 AM 6:00 AM 6:00 AM 6:00 AM 1:30 PM
Bacalla, Catherine        VL    VL  RD/OT 7:30  7:30 AM 7:30 AM 7:30 AM RD
Dela Cruz, Mary Ann       6:00 AM   7:30 AM RD  RD  7:30 AM 7:30 AM 6:00 AM
Dumana, John Carlos       1:30 PM   1:30 PM 1:30 PM RD  RD  1:30 PM 1:30 PM
Macabodbod, Evelyn        9:00 AM   9:00 AM 9:00 AM 9:00 AM RD  RD/OT 9:00  9:00 AM
Mariano, Kristine         1:30 PM   RD/OT 1:30  1:30 PM 1:30 PM 1:30 PM RD  7:30 AM
Santiago, Gretchen        RD/OT 7:30    6:00 AM 7:30 AM 7:30 AM 1:30 PM 1:30 PM RD
END
Was it helpful?

Solution

try this:

Dim objCmdSelect As New OleDbCommand("SELECT * FROM [Sheet1$B14:I24]", objConn)

lets assume B14 is your starting ROW and I24 is your last column/row record.

UPDATED:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    OpenFileDialog1.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.Desktop
    OpenFileDialog1.Filter = "CSV Files (*.csv)|*.csv"

    If OpenFileDialog1.ShowDialog(Me) = System.Windows.Forms.DialogResult.OK Then

        Try
            Dim fi As New FileInfo(OpenFileDialog1.FileName)
            Dim sConnectionStringz As String = "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=Text;Data Source=" & fi.DirectoryName & ";" & "Extended Properties=""Excel 8.0;HDR=NO;IMEX=1"""
            Dim objConn As New OleDbConnection(sConnectionStringz)
            objConn.Open()

            Dim objAdapter1 As New OleDbDataAdapter("SELECT * FROM [Sheet1$B14:I24]", objConn)

            Dim objDataset1 As New DataSet
            objAdapter1.Fill(objDataset1)

            DataGridView1.DataSource = objDataset1.Tables(0).DefaultView

            objConn.Close()
        Finally


        End Try


    End If
End Sub
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top