簡體   English   中英

如何將數據從 Excel 導出到 mysql?

[英]How can I export data from Excel to mysql?

此代碼可以在personal.xlsb(MS Excel)上運行以將數據傳輸到mysql嗎?

我得到空白行。

Public Sub Insert_Testing()
Dim con as adodb.connection
Dim lastrow as long
Set ws = ThisWorkbook.ActiveSheet
Set con = New Adodb.connection
Con.open = "Provider=MSDASQL.1;Data Source=MySQL_db;"
Dim rng as range
Lastrow = ws.Range("B" & Rows.count).End(x1Up).row
Set rng = ws.Range("A2:G" & Lastrow)
Dim row as range

For each row in rng.rows
    SQL = "Insert into skynet_msa.ALU_testing (Area, Min_C, Max_C, Avg_C, Emis, Ta_C, Area_Px) values ('" & row.Cells(1).Value & '", '" & row.Cells(2).Value & '", '" & row.Cells(3).Value & '", '" & row.Cells(4).Value & '", '" & row.Cells(5).Value & '", '" & row.Cells(6).Value & '", '" & row.Cells(7).Value &'");"
    Con.Execute SQL
Next row

Con.close

MsgBox "Done"

End Sub

這似乎不起作用,因為這里的活動表一直引用我的personal.xlsb,而不是我計划導出數據的其他文檔,因為這兩個文檔同時打開。

我認為你需要改變這一點:

SQL = "Insert into skynet_msa.ALU_testing (Area, Min_C, Max_C, Avg_C, Emis, Ta_C, Area_Px) values ('" & row.Cells(1).Value & '", '" & row.Cells(2).Value & '", '" & row.Cells(3).Value & '", '" & row.Cells(4).Value & '", '" & row.Cells(5).Value & '", '" & row.Cells(6).Value & '", '" & row.Cells(7).Value &'");"

對此:

SQL = "Insert into skynet_msa.ALU_testing (Area, Min_C, Max_C, Avg_C, Emis, Ta_C, Area_Px) values ('" & row.Cells(1).Value & "', '" & row.Cells(2).Value & "', '" & row.Cells(3).Value & "', '" & row.Cells(4).Value & "', '" & row.Cells(5).Value & "', '" & row.Cells(6).Value & "', '" & row.Cells(7).Value & "');"

您的個人 xlsb 可能有其他干擾的功能/vba 代碼。 我只是猜測。

暫無
暫無

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

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