如何将一个EXCEL文件作为二进制文件存入数据库,再把它读取打开?
程序员文章站
2023-11-09 08:09:04
下面的代码把中的image类型字段还原为excel文件。 数据库用的是sql server自带的pubs dim ...
下面的代码把中的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