乔山办公网我们一直在努力
您的位置:乔山办公网 > excel表格制作 > <em>ASP</em>代码中如何把EXCEL数据导入到SQL SERVER数...-asp 导

<em>ASP</em>代码中如何把EXCEL数据导入到SQL SERVER数...-asp 导

作者:乔山办公网日期:

返回目录:excel表格制作


根据你说的情况判断,可以是下面的原因:
1. 你在你的笔记本电脑上设置了Excel连接驱动,而在另外一台电脑上并没有设置对应的Excel连接驱动。
2. 你的Excel数据文件在另外一台电脑上并不存在。

'定义打开Excel表格的函数
Function OpenExcel(path)
dim excel,rs,strsql
On Error Resume Next
Set rs = Server.CreateObject("ADODB.RecordSet")
Set excel = Server.CreateObject("ADODB.Connection")
excel.Open "Driver={Microsoft Excel Driver (*.xls)};DBQ=" & path
If Err.number<> 0 Then
Response.Write "请检查上传的Excel文件内部格式,文件无法打开,导入失败!"
Response.End
End If
strsql = "SELECT * FROM [Sheet1$]" '在这里指定工作薄名称,默认是Sheet1$
Set rs = excel.Execute(strsql)
Set OpenExcel = rs
End Function

'读取文件中的内容
Dim rsInfo
Set rsInfo = Server.CreateObject("ADODB.RecordSet")
Set rsInfo = OpenExcel("E:/a.xls") '这里的文件路径请用Server.Path来获取

'检查读取结果
If rsInfo.State<> 1 Then
Response.Write "请检查Excel文件中的工作表命名是否为Sheet1,导入失败!"
Response.End
End If

If rsInfo.EOF And rsInfo.BOF Then
Response.Write "没有找到Excel表中的数据,导入失败!"
Response.End
End If

If IsNull(rsInfo.Fields(0)) or Trim(rsInfo.Fields(0))="" Then
Response.Write "没有找到Excel表中的数据,导入失败!"
Response.End
End If

'这里指定导入数据的列数,列数少了退出
If rsInfo.Fields.Count< 7 Then
Response.Write "Excel表中的数据列数不正确,导入失败!"
Response.End
End If

'创建数据库连接
dim dbrs,conn,sql
Set conn = Server.CreateObject("ADODB.Connection")
Set dbrs = Server.CreateObject("ADODB.Recordset")
'注: G_DB_ConnectString是连接数据库的字符串,自己定义
conn.ConnectionString = G_DB_ConnectString
conn.Open '打开数据库连接

'创建临时表
sql = "IF EXISTS (SELECT * FROM sysobjects WHERE xtype='U' and name='tmp_PartRes') "
sql = sql & "BEGIN Drop table tmp_PartRes END "
sql = sql & "Create table tmp_PartRes([ID] int identity(1,1),"
sql = sql & "PartID varchar(100),Brand varchar(100),[Package] varchar(100),"
sql = sql & "BatchNo varchar(100),[Price] varchar(100),[Stock] varchar(100) default('0'),"
sql = sql & "Brief varchar(100),StockFlag int default(1),"
sql = sql & "SuperFlag int default(1),SaleFlag int default(1))"
conn.execute sql

'取表结构 注意: 只取表的结构, 不要数据, 因为我这个是刚创建的临时表, 没有数据,
'如果表中存在数据, 要注e79fa5e98193e78988e69d83366意加上条件句, 防止取到数据 如: where ID = -1
sql = "SELECT * FROM tmp_PartRes"
dbrs.CursorLocation = 3 '这一定要设置为3
dbrs.Open sql,conn, 3, 4 '这里的参数必须是3和4

'取到表结构后, 必须要把活动连接及数据库连接关闭,这个很重要, 否则导入速度特慢.
Set dbrs.ActiveConnection = Nothing
conn.close

'提取Excel中的数据, 将excel中的数据放入到数据库表中.
While Not rsInfo.EOF
If Trim(rsInfo.Fields(0))<> "" Then
dbrs.AddNew
dbrs("PartID") = Ucase(Trim(rsInfo.Fields(0)))
dbrs("Brand") = Trim(rsInfo.Fields(1))
dbrs("Package") = Trim(rsInfo.Fields(2))
dbrs("BatchNo") = Trim(rsInfo.Fields(3))
dbrs("Price") = Trim(rsInfo.Fields(4))
If Trim(rsInfo.Fields(5))<>"" Then
dbrs("Stock") = Trim(rsInfo.Fields(5))
Else
dbrs("Stock") = "0"
End If
dbrs("Brief") = Trim(rsInfo.Fields(6))
End If
rsInfo.MoveNext
Wend

'更新记录集到数据库临时表
conn.Open '打开连接
dbrs.ActiveConnection = conn
dbrs.UpdateBatch '批量更新函数

'更新完成后, 关闭连接
dbrs.Close
Set dbrs = Nothing
rsInfo.Close
Set rsInfo = Nothing


Set db = Server.CreateObject("ADODB.Connection")
db.Open "Driver={Microsoft Excel Driver (*.xls)};Dbq=" & Server.MapPath(FileName)
'打开记录集,表名一定要以"[表名$]"的格式
strSql="Select * From [Sheet1$]"
Set rsexcel=db.Execute(strSql)
Set rs = Server.CreateObject("ADODB.RecordSet")
SQL = "Select * From [users]"
rs.Open SQL, Conn, 1, 3
N = rs.fields.count-1'数据表字段数
M = rsexcel.fields.count-1'excel表里字段数
Redim L(N)'定义数据表数组
Redim arr(M)'定义excel表数组

'把数据表里的字段表存入数组L中
For k=0 To N
L(k) = Rs(k).name
next
'把excel表里的字段存入数组arr中
for p=0 to M
arr(p) = rsexcel(p).name
next
'取数组长度
x=UBound(l)
y=UBound(arr)

j=0
startime=timer() '开始时间
Do While Not rsexcel.EOF

rs.AddNew
'循环读取所有行
for i=6 to x'对数据表进行循环 rs(0)是 id 所以跳过 e5a48de588b6e799bee5baa6e997aee7ad94361
if l(i)=arr(j) then'通过数组判断两表字段是否相同,如相同对其进行赋值,不相同置0
if Trim(rsexcel(j))<>"" then
'response.write Trim(rsexcel(j))&"|"
rs(i)=Trim(rsexcel(j))
else
rs(i)=0
end if
if j< M then '当execl表数据是最后一项时不能在加1了
j=j+1
else
j=j
end if
else
rs(i)=0
end if
rs(3)=typeid
Next
rsexcel.MoveNext
rs.update
j=0
Loop
endtime=timer()
'关闭对象
rsexcel.Close
Set rsexcel=nothing
db.Close
Set db=Nothing

相关阅读

关键词不能为空
极力推荐

ppt怎么做_excel表格制作_office365_word文档_365办公网