简体   繁体   中英

How can I export data from Excel to mysql?

Can this code be run on personal.xlsb (MS Excel) to transport data to 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.

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. I am only guessing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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