2012-05-02 179 views
0

有沒有辦法做到這一點?我在Excel中有6000行,需要幾個月的時間才能將它導入到sql數據庫中。如何從excel文件導出數據並導入到sql數據庫?

例如有:列A列B在Excel,我想將其導入到列A列B到SQL表。

謝謝!

+0

Sugestion,保存Excel爲CSV然後使用任何編程語言解析它變成可以插入 - >運行腳本! –

+0

我知道微軟Office Access有一個功能可以從excel工作表導入數據庫。你可能想看看那 – Manuel

+1

哪個數據庫? MySQL,SQL Server,DB2?無論如何,首先導出所需的CSV格式數據(在Excel中,保存爲.csv文件),然後將該CSV文件導入到數據庫中。 (不同的數據庫有不同的做法。) –

回答

0

這是一種通過VBA的方法,用於書籍數據庫。只要您可以建立與數據庫的連接,就不會太困難。顯然,你必須稍微調整一下,以便它能夠與你的數據庫和你的數據一起工作。特別是,你將不得不調整SQL字符串來連接你的數據庫(改變「test.yourdatabase」)

Sub addBook(title As String, author As String, link As String, foundBy As String) 
    ' for this code to work, you need a reference to the MS Activex 
    ' data access objects 
    ' Tools|References... microsoft activex data objects 2.8 (or newer) 

    Dim cn As New Connection 
    Dim cs As String 
    Dim un as String 
    Dim pw as String 
    Dim sql As String 


    cs = "DRIVER=SQL Server;SERVER=websql.org,5901;DATABASE=websql" 'connection string 

    un = username 'this is the username for the database 

    pw = password 'this is the password for the database account 

    cn.Open cs, Range("un").Value, Range("pw").Value 


    sql = "insert into test.yourdatabase(searchtitle, searchAuthor, link, foundBy, foundTime) values(" & _ 
    "'<TITLE>', " & _ 
    "'<AUTHOR>', " & _ 
    "'<LINK>', " & _ 
    "'<FOUNDBY>', " & _ 
    "getdate());" 

    'replace statements are for correcting any ' that occure in the titles or names 

    sql = Replace(sql, "<TITLE>", Replace(title, "'", "''")) 
    sql = Replace(sql, "<AUTHOR>", Replace(author, "'", "''")) 
    sql = Replace(sql, "<LINK>", Replace(link, "'", "''")) 
    sql = Replace(sql, "<FOUNDBY>", Replace(foundBy, "'", "''")) 

    'Debug.Print sql 

cn.Execute sql 

    cn.Close 
End Sub 
相關問題