如何从Excel文件中读取数据到数据库中?

xwlovesh 2003-01-15 03:12:53
给出一个从sql server数据库中导出的Excel文件,如何通过asp程序将此Excel文件中读取数据到数据库中?多谢!请帮忙给出例子。
...全文
360 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
xwlovesh 2003-01-15
  • 打赏
  • 举报
回复
注意:是在客户端将Excel文件导入到数据库服务器中
xwlovesh 2003-01-15
  • 打赏
  • 举报
回复
我说的是将Excel文件直接导入到数据库中,不是上传Excel文件
coo_key 2003-01-15
  • 打赏
  • 举报
回复
转一个给你:
<%
'适用于有固定格式的Excel表格

Dim xlsApp,xlsWb,xlsWs
Dim Rs,Sql,i,str
Dim SavePath
Dim message
Dim fso
message=""

'=====以下若干句为上传文件并保存=============
……
SavePath= server.MapPath(".") & "\tempXls.Xls"
Obj.SaveFile userFile, SavePath
……
'=============================================

on error resume next

set fso=Server.CreateObject("scripting.FileSystemObject")

If fso.FileExists(SavePath) then '如果文件存在

Set xlsApp = CreateObject("Excel.application") '建立对象
xlsApp.Visible = False
Set xlsWb = xlsApp.Workbooks.Open(SavePath) '打开Excel
set xlsWs=xlsApp.Worksheets(1)

With xlsWs
'循环读取excel中的行现在设置只读25行。
'亦可设为do while 循环然后根据读取的某单元格数据为空即跳出循环。
for i=2 to 27

'读取表格各列数据
mkh=Trim(.Range("B" & i ))
jjjdk=Trim(.Range("C" & i ))
jx=Trim(.Range("E" & i ))
txh=Trim(.Range("F" & i ))
xlbh=Trim(.Range("G" & i ))
bz =Trim(.Range("H" & i ))

Sql="insert into gywgzzb (xm,sbmc,mkh,jjjdk,jx,txh,xlbh,bz) values ('" & _
xm & "','" & _
sbmc & "','" & _
mkh & "','" & _
jjjdk & "','" & _
jx & "','" & _
txh & "','" & _
xlbh & "','" & _
bz & "')"
Rs.close
conn.Execute(Sql)
Next
End With
message="数据成功导入"

xlsApp.Quit '关闭对象
Set xlsWs = Nothing
Set xlswk = Nothing
Set xlsApp = Nothing
conn.close
set conn=nothing

'删除临时xls文件
fso.DeleteFile SavePath,true
end if

set fso=nothing

if err<>0 then
Response.Write err.Description
else
Response.Write "<script>"
Response.Write "alert('" & message & "');"
Response.Write "window.close();"
Response.Write "</script>"
end if
%>
genuis 2003-01-15
  • 打赏
  • 举报
回复
ODBC Driver for Excel
 
oConn.Open "Driver={Microsoft Excel Driver (*.xls)};" & _
          "DriverId=790;" & _
          "Dbq=c:\somepath\mySpreadsheet.xls;" & _
          "DefaultDir=c:\somepath;"

For more information, see: Microsoft Excel Driver Programming Considerations

You can also open an Excel Spreadsheet using the "OLE DB Provider for Microsoft Jet"

oConn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
          "Data Source=c:\somepath\myExcelSpreadsheet.xls;" & _
          "Extended Properties=""Excel 8.0;HDR=Yes;"";"

Where "HDR=Yes" means that there is a header row in the cell range
(or named range), so the provider will not include the first row of the
selection into the recordset. If "HDR=No", then the provider will include
the first row of the cell range (or named ranged) into the recordset.

For more information, see: Q278973


其它的不用说了吧

28,391

社区成员

发帖
与我相关
我的任务
社区描述
ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
社区管理员
  • ASP
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧