简体   繁体   English

如何将数据从 Excel 导出到 mysql?

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

Can this code be run on personal.xlsb (MS Excel) to transport data to mysql?此代码可以在personal.xlsb(MS Excel)上运行以将数据传输到mysql吗?

I get blank rows.我得到空白行。

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

This seems to not work since active sheet here keeps referring to my personal.xlsb and not the other document I am planning to export the data with as both documents are opened at the same time.这似乎不起作用,因为这里的活动表一直引用我的personal.xlsb,而不是我计划导出数据的其他文档,因为这两个文档同时打开。

I think you need to change this:我认为你需要改变这一点:

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 &'");"

to this:对此:

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 & "');"

Your Personal xlsb probably have other functions/vba code that is interfering.您的个人 xlsb 可能有其他干扰的功能/vba 代码。 I am only guessing.我只是猜测。

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

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