简体   繁体   English

如何将Excel文件导入MySQL

[英]How to import excel file to mysql

This is the code i got it only import excel to datagridview. 这是我只将excel导入到datagridview的代码。 how do i import excel to mysql database using vb.net? 如何使用vb.net将Excel导入mysql数据库?

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    OpenFileDialog1.ShowDialog()
End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    Try

        Dim MyConnection As System.Data.OleDb.OleDbConnection
        Dim dataSet As System.Data.DataSet
        Dim MyCommand As System.Data.OleDb.OleDbDataAdapter

        MyConnection = New System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + TextBox1.Text + ";Extended Properties=Excel 12.0;")
        MyCommand = New System.Data.OleDb.OleDbDataAdapter("select * from [Sheet1$]", MyConnection)

        dataSet = New System.Data.DataSet
        MyCommand.Fill(dataSet)
        DataGridView1.DataSource = dataSet.Tables(0)

        MyConnection.Close()
    Catch ex As Exception
        MsgBox(ex.Message.ToString)
    End Try
End Sub

Private Sub OpenFileDialog1_FileOk(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
    TextBox1.Text = OpenFileDialog1.FileName
End Sub

No matter you use VB or other language. 无论您使用VB还是其他语言。

  • Read you file, and loop over 阅读您的文件,然后遍历
  • You just need to transform the excel column_title to to have a string like (column1, column2, column3, ...) 您只需要将excel column_title转换为具有类似(column1, column2, column3, ...)的字符串

  • Then every excel row to concatenated values_variable : 然后每个excel行连接到values_variable:

 (value1, value2, value3, ...), (value1, value2, value3, ...), (value1, value2, value3, ...) 
  • So, you just need to create an INSERT query at the end of the loop: 因此,您只需要在循环结束时创建一个INSERT查询:

INSERT INTO table_name (column1, column2, column3, ...) VALUES (value1, value2, value3, ...), (value1, value2, value3, ...), (value1, value2, value3, ...), ... ; INSERT INTO table_name(第1栏,第2栏,第3栏,...),(第1栏,第2栏,第3栏,...),(第1栏,第2栏,第3栏,...),(第1栏,第2栏,第3栏,...), ,...;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM