第2个回答 2019-11-26
用VBA实现
Sub 连接数据库1()
Set Cnn = CreateObject("ADODB.Connection")
Set rt = CreateObject("ADODB.Recordset")
ID = "localhost"
Cnn.ConnectionString = "Provider = SQLOLEDB;server=" & ID & ";User ID= sa;password=sa;Data Source=" & test & ";" & "Initial Catalog = test"
Cnn.Open
SQL = "select * from 成绩表 "
Set rt = Cnn.Execute(SQL)
With Sheet1
For i = 0 To rt.Fields.Count - 1
.Cells(1, i + 1) = rt.Fields(i).Name
Next i
.Range("a2").CopyFromRecordset rt
.Cells.EntireColumn.AutoFit