如何将一个EXCEL文件作为二进制文件存入数据库,再把它读取打开?
2007-12-09 13:38:47 来源:WEB开发网核心提示:下面的代码把数据库中的image类型字段还原为excel文件, 数据库用的是sql server自带的pubs Dim cn As ADODB.Connection Dim rs As ADODB.Recordset Dim mstream As
下面的代码把数据库中的image类型字段还原为excel文件。
数据库用的是sql server自带的pubs
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim mstream As ADODB.Stream
Set cn = New ADODB.Connection
cn.ConnectionString = "Provider=SQLOLEDB.1;Persist Security Info=True;User ID=sa;Password=sa;" _
& "Initial Catalog=pubs;Data Source=MyServer"
cn.OpenSet rs = New ADODB.Recordset
rs.Open "Select * from pub_info", cn, adOpenKeyset, adLockOptimistic
Set mstream = New ADODB.Stream
mstream.Type = adTypeBinary
mstream.Open
mstream.Write rs.Fields("logo").Value
mstream.SaveToFile "c:\publogo.xls", adSaveCreateOverWrite
rs.Close
cn.Close
下面的代码把一Excel保存到数据库中Set cn = New ADODB.Connection
cn.Open "Provider=SQLOLEDB.1;Persist Security Info=True;User ID=sa;Password=sa;" _
& "Initial Catalog=pubs;Data Source=MyServer"
Set rs = New ADODB.Recordset
rs.Open "Select * from pub_info", cn, adOpenKeyset, adLockOptimistic
rs.AddNew
Set mstream = New ADODB.Stream
mstream.Type = adTypeBinary
mstream.Open
mstream.LoadFromFile txtEXcelPath
rs.Fields("pub_id") = "……" '在pubs库中,这个字段是主键,需要修改一个有效值
rs.Fields("logo").Value = mstream.Read
rs.Update
rs.Close
cn.Close
该代码来自MSDN
数据库用的是sql server自带的pubs
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim mstream As ADODB.Stream
Set cn = New ADODB.Connection
cn.ConnectionString = "Provider=SQLOLEDB.1;Persist Security Info=True;User ID=sa;Password=sa;" _
& "Initial Catalog=pubs;Data Source=MyServer"
cn.OpenSet rs = New ADODB.Recordset
rs.Open "Select * from pub_info", cn, adOpenKeyset, adLockOptimistic
Set mstream = New ADODB.Stream
mstream.Type = adTypeBinary
mstream.Open
mstream.Write rs.Fields("logo").Value
mstream.SaveToFile "c:\publogo.xls", adSaveCreateOverWrite
rs.Close
cn.Close
下面的代码把一Excel保存到数据库中Set cn = New ADODB.Connection
cn.Open "Provider=SQLOLEDB.1;Persist Security Info=True;User ID=sa;Password=sa;" _
& "Initial Catalog=pubs;Data Source=MyServer"
Set rs = New ADODB.Recordset
rs.Open "Select * from pub_info", cn, adOpenKeyset, adLockOptimistic
rs.AddNew
Set mstream = New ADODB.Stream
mstream.Type = adTypeBinary
mstream.Open
mstream.LoadFromFile txtEXcelPath
rs.Fields("pub_id") = "……" '在pubs库中,这个字段是主键,需要修改一个有效值
rs.Fields("logo").Value = mstream.Read
rs.Update
rs.Close
cn.Close
该代码来自MSDN
[]
更多精彩
赞助商链接