前言:
經過不斷的嘗試,總算成功寫出可以把SQLite的工作表,完整複製到Excel的工作表上!!
以下為程式碼:
---------------
Sub 從SQLite取出檔案到Excel工作表()
Dim conn As Object, rst As Object
Set conn = CreateObject("ADODB.Connection")
Set rst = CreateObject("ADODB.Recordset")
'開啟sqlite指定資料庫,路徑與檔案名稱要對
conn.Open "DRIVER=SQLite3 ODBC Driver;Database=你自己放的資料庫路徑;"
'SQL查詢語法
strSQL = "SELECT * FROM data "
' OPEN RECORDSET
rst.Open strSQL, conn, 1, 1
' 將資料庫的標題取出,放入工作表第一列
For f = 0 To rst.Fields.Count - 1
Sheets("data").Cells(1, f + 1).Value = rst.Fields(f).Name
Next
' OUTPUT TO WORKSHEET
Worksheets("data").Range("A2").CopyFromRecordset rst
' FREE RESOURCES
Set rst = Nothing: Set conn = Nothing
End Sub
---------------
參考資料:
https://stackoverflow.com/questions/42509154/accessing-a-sqlite-database-in-vba-in-excel/42512968
http://forum.twbts.com/viewthread.php?tid=9984&extra=pageD1&page=1
沒有留言:
張貼留言