簡體   English   中英

如何從Excel文件中導出數據並導入到SQL數據庫中?

[英]How to export data from excel file and import into to sql database?

有沒有辦法做到這一點? 我在excel中有6000行,將它導入sql數據庫需要花費數月的時間。

例如,有:excel中的A列B列 ,我想將其導入到sql表中的A B列中。

謝謝!

這是我用於圖書數據庫的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

如果您使用的是MySQL ,它提供了一種將數據導出到數據庫的直接方法: https : //dev.mysql.com/doc/mysql-for-excel/en/mysql-for-excel-export.html

如果您使用的是SQLite / PostGres ,Devart會提供商業產品來執行以下操作:

https://www.devart.com/excel-addins/postgresql.html

https://www.devart.com/excel-addins/sqlite.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM