本人使用以下语句: Dim xlApp As Excel.Application
Dim xlBook As Excel.WorkBook
Dim xlSheet As Excel.WorkSheet
Dim strtable As String
'open the excel file through MSExcel
If Trim(strFile) = "" Then Exit Sub Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Open(strFile)
Set xlSheet = xlBook.Worksheets("sheet1")引用EXCEL文件后
虽然最后使用 SET xlsheet=nothing
set xlbook=nothing
set xlapp=nothing
释放了对象,但程序结束后,EXCEL文件总是被'只读'锁定,不知哪为兄弟知道如何解决该问题
Dim xlBook As Excel.WorkBook
Dim xlSheet As Excel.WorkSheet
Dim strtable As String
'open the excel file through MSExcel
If Trim(strFile) = "" Then Exit Sub Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Open(strFile)
Set xlSheet = xlBook.Worksheets("sheet1")引用EXCEL文件后
虽然最后使用 SET xlsheet=nothing
set xlbook=nothing
set xlapp=nothing
释放了对象,但程序结束后,EXCEL文件总是被'只读'锁定,不知哪为兄弟知道如何解决该问题
加个 xlBook.Quit 就可以了。
例:
将Excel中的数据导入到Access中,前提是在Access中的表已经建好:
dim conn
dim conn2
set conn=CreateObject("ADODB.Connection")
conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Jet OLEDB:Database Password=;Data 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
wendconn.close
set conn = nothing
conn2.close
set conn2 = nothingfunction fixsql(str)
dim newstr
newstr = str
if isnull(newstr) then
newstr = ""
else
newstr = replace(newstr,"'","''")
end if
fixsql = newstr
end function
set conn=CreateObject("ADODB.Connection")
conn.Open ("driver={SQL Server};server=localhost;uid=sa;pwd=sa;database=hwtemp;")
sql = "SELECT * into newtable FROM OpenDataSource( 'Microsoft.Jet.OLEDB.4.0','Data Source=""c:\book1.xls"";User ID=Admin;Password=;Extended properties=Excel 5.0')...[Sheet1$] "
conn.execute(sql) conn.close
set conn = nothing