Tuesday, November 15, 2016

Post #3 : Programming "How to import file excel into Datagridview in Visual Studio Using Visual Basic .Net"

Hi. Jack

Today we will try to import data from excel file "xls" or "xlsx" to datagridview, this script has been tested using visual studio 2010.

Q. Why do we have to import data from excel to a DataGridView ?

A. Sometimes many companies initially did all the needs of theirs data using excel, when they begin to migrate using application, it would be quite a hassle if you have to enter one by one manualy. Therefore, should we provide import function from excel to DataGridView to Database .

Okay let's start .


1. Prepare an excel file, hahahaha course there should be an excel file that must be prepared.



2. on your project add this "import system.data.oledb"


3.for an example, create a form as shown below


4. 

Private Sub btn_pilih_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_pilih.
 Dim op As New OpenFileDialog
 op.FileName = "" 'kolom filename kosong [file name column is empty]
 op.Filter = "Excel 2000-2003 (*.xls)|*.xls|Excel 2007 (*.xlsx)|*.xlsx" 'gunakan filter biar yang muncul cuma file excel "xls" dan "xlsx" [use a filter so that appears only file "xls" and "xlsx" ]
 op.FilterIndex = 1 'default file extension yang dipilih [The default file extension is selected]
 op.ShowDialog() 'memunculkan dialog pilih fime
 txtFileexcel.Text = op.FileName 'mengisi textbox dengan path dan file name
 End Sub
5. 

6. 

7. 


CONGRATULATIONS !!!

Sample Project : Download here

Next : "Export Data From DataGridView to Excel File"

1 comment:

  1. kalo ada error "external table is not in the expected format."
    connection stringnya diubah jadi.

    "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + txtFileexcel.Text + ";Extended Properties='" & "Excel 8.0;HDR=YES" & "';"

    ReplyDelete