请教如何将excel数据导入到sql server已有的表中

2024-12-16 01:18:22
推荐回答(1个)
回答1:

先在SQL中创建个table
create table student3
(
[sno] int not null primary key,
[sname] varchar(20) not null,
[sage] int,
);

打开一个新的excel,并添加些测试用的数据

添加Microsoft ActiveX Data Objects的引用,运行下面的代码

Sub DataFromExceltoSQLbyADO()
Dim Conn As New ADODB.Connection
Dim Sql As String
Dim i As Long
'change to your connection string
Conn.Open "Provider=SQLNCLI10;Server=xxx;Database=xxx;User Id=xxx;Password=xxx"
For i = 2 To 3
Sql = "insert into student3 values(" & Cells(i, 1) & ",'" & Cells(i, 2) & "', " & Cells(i, 3) & ")"
Conn.Execute (Sql)
Next i
Set Conn = Nothing
End Sub