用ASP把EXCEL数据导入SQLSERVER数据库中,哪位知道,请接分

哪位知道在ASP把EXCEL数据导入SQLSERVER数据库中,请给点思路或代码.谢谢啦.
---------------------------------------------------------------

没有原代码,给你点提示,你可把EXCEL表当成一个数据库,从数据库里取数。再提交到SQL到保存。如果从EXCEL取数。论坛和技术文档里都有这方面的资料。
---------------------------------------------------------------

Set fso = CreateObject("Scripting.FileSystemObject")
Set of = fso.GetFile(Server.mappath(IMPORTDATAFILE & "/" & savedfilename))

Set ts = oF.OpenAsTextStream(1, -2)
do while ts.AtEndOfStream <> true
i = i + 1
strline = ts.ReadLine
arrLine = split(strline,",")
值的数组
sqlinsert = " insert into(...) values(...) "

oConn.execute(sqlinsert)
loop
---------------------------------------------------------------

excel.xsl:

字段: aa bb cc

值: a1 b1 c1

a2 b2 c2

注意,excel中字段名和字段的值都是同样作为数据输入的,只是说

你把excel表的第一行作为是字段名就是。

test.asp

 1   
 2  
 3dim myconnection   
 4  
 5set myconnection=server.createobject("adodb.connection")   
 6  
 7dim myconnectionstr   
 8  
 9myconnectionstr="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=e:\test\test.x   
10  
11ls;Extended Properties=Excel 8.0;"   
12  
13myconnection.open myconnectionstr   
14  
15dim myrecordset   
16  
17set myrecordset=server.createobject("adodb.recordset")   
18  
19dim mysql   
20  
21  
22  
23mysql="select * from [sheetname$]"   
24  
25myrecordset.open mysql,myconnection,1,1   
26  
27do while not myrecordset.eof   
28  
29response.write myrecordset("aa") & "

<br/>

 1"   
 2  
 3myrecordset.movenext   
 4  
 5loop   
 6  
 7set myrecordset=nothing   
 8  
 9set myconnection=nothing   
10  

打印结果:

a1

a2
然后存入数据库你应该会了吧
---------------------------------------------------------------

EXCEL可以当数据库用的

Function CreateExcelRecordset( XLS文件名,Sheet名 )

Dim conn.Driver,DBPath

‘ 建立Connection对象
Set conn = Server.CreateObject("ADODB.Connection")
Driver = "Driver={ Microsoft Excel Driver (*.xls)};"
DBPath = "DBQ=" & Server.MapPath( "XLS文件名" )

‘ 调用Open 方法打开数据库
conn.Open Driver & DBPath
Set CreateExcelRecordset = Server.CreateObject("ADODB.Recordset")
‘ 打开Sheet,参数二为Connection对象,因为Excel ODBC驱动程序无法直接用
‘sheet名来打开sheet,所以请注意以下的select语句
CreateExcelRecordset.Open "Select * From ["&sheet&”$]”, conn, 2, 2

End Function
---------------------------------------------------------------

excel可以把他看作一个数据库,用asp当然只能操作服务器端的数据。
对于客户端是没有办法操作了,当然如果你的客户信任你,你可以做成activex控件来操作客户端,这又是其他的问题了。

Published At
Categories with Web编程
Tagged with
comments powered by Disqus