Like an Access database, open an Excel file, read it, and write it in Access if you want to write to SQL Server, change the writing process.
Here's the code:
Dim conn
Dim conn2
Set Conn=createobject ("ADODB. Connection ")
Conn. Open "Provider=Microsoft.Jet.OLEDB.4.0; Jet oledb:database password=;D ata Source=c:\book1.mdb "
Set Conn2=createobject ("ADODB. Connection ")
Conn2. Open "Provider=Microsoft.Jet.OLEDB.4.0; Jet oledb:database password=; Extended properties=excel 5.0;data Source=c:\book1.xls "
sql = "SELECT * FROM [sheet1$]"
Set rs = conn2.execute (SQL)
While not rs.eof
sql = "INSERT into XXX ([a],[b],[c],[d]) VALUES ('" & Fixsql (RS (0)) & "', '" & Fixsql (RS (1)) & "', '" & Fixsql (RS (2)) & "', '" & Fixsql (RS (3)) & "')"
Conn.execute (SQL)
Rs.movenext
Wend
Conn.close
Set conn = Nothing
Conn2.close
Set conn2 = Nothing
function Fixsql (str)
Dim newstr
NEWSTR = str
If IsNull (NEWSTR) Then
Newstr = ""
Else
NEWSTR = replace (newstr, "'", "")
End If
Fixsql = Newstr
End Function |