怎么将多个excel表导入一个access数据库

发布网友 发布时间:2022-04-21 03:35

我来回答

1个回答

热心网友 时间:2022-04-11 20:48

B实现
此一模块共有四个参数:
1、sSheetName:要导出资料的文件名称 (Sheet name),例如 Sheet1
2、sExcelPath:要导出资料的 Excel 档案路径名称 (Workbook path),例如 C:\\book1.xls
3、sAccessTable:要导入的 Access Table 名称,例如 TestTable
4、sAccessDBPath:要导入的 Access 档案路径名称,例如 C:\\Test.mdb
在声明中加入以下:
Private Sub ExportExcelSheetToAccess(sSheetName As String, _
sExcelPath As String, sAccessTable As String, sAccessDBPath As String)
Dim db As Database
Dim rs As Recordset
Set db = OpenDatabase(sExcelPath, True, False, "Excel 5.0")
Call db.Execute("Select * into [;database=" & sAccessDBPath & "]." & _
sAccessTable & " FROM [" & sSheetName & "$]")
MsgBox "Table exported successfully.", vbInformation, "Yams"
End Sub
使用范例如下:将 C:\\book1.xls 中的 Sheet1 导入 C:\\Test.mdb 成为 TestTable
ExportExcelSheetToAccess "Sheet1", "C:\\book1.xls", "TestTable", "C:\\Test.mdb"

--------------------------------------------------------------------------------
ASP
以下代码优点,不用打开EXCEL进程,不会出现EXCEL进程无法结束而死机.缺点:可能会出现导入空格的现像.
<%
sub dataIntoSqlServer_ceritificate(strFileName,strSheetName,myConn)
'定义
dim myConnection
dim strName
dim rsXsl,rsSql
dim str_Xsl,str_Sql
dim myConn_Xsl
dim cmd
dim i,j
dim maxId
strName=strFileName
set myConnection=server.createobject("adodb.connection")
set rsXsl=Server.Createobject("ADODB.Recordset")
set rsSql=Server.CreateObject("ADODB.Recordset")
set cmd=Server.CreateObject("ADODB.Command")
cmd.ActiveConnection=myConn

myConn_Xsl="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strName & ";Extended Properties=Excel 8.0"
'myConn_Xsl="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Application("ASP_Directory") & "chapter05\database\" & strName & ";Extended Properties=Excel 8.0"
'打开连接
myConnection.open myConn_Xsl
'打开表
str_Xsl="select * from ["& strSheetName &"$]"
rsXsl.open str_Xsl,myConnection,1,1
j=1
Do While not rsXsl.eof
'取出最大值
str_Sql="select Max(id) as maxId from new"
rsSql.open str_Sql,myConn,1,3
If Not rsSql.Eof Then
If not isNull(rsSql("maxId")) Then
maxId=Clng(rsSql("maxId"))+1
Else
maxId=1
End if
else
maxId=1
End if
rsSql.close'//关闭对象
'加入数据库
strqbsr=""
yssr=""
str_Sql="insert into new values("&maxId&",'"&rsXsl(1)&"','"&rsXsl(2)&"','"&rsXsl(3)&"','"&rsXsl(4)&"','"&rsXsl(5)&"','"&rsXsl(6)&"','"&rsXsl(7)&"','"&rsXsl(8)&"','"&rsXsl(9)&"','"&rsXsl(10)&"','"&rsXsl(11)&"','"&rsXsl(12)&"','"&rsXsl(13)&"','"&rsXsl(14)&"','"&rsXsl(15)&"','"&rsXsl(16)&"','"&strqbsr&"','"&yssr&"')"
cmd.CommandText=str_Sql
cmd.Execute()
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
j=j+1
rsXsl.movenext
Loop
response.write "共导入<font color='red'>" & j-1 & "</font>条记录.<br/>"
response.write "<a href=javascript:history.back()>确定</a>"
set rsXsl=nothing
set rsSql=nothing
set myconnection=nothing
set cmd=nothing
end sub
file1="'"&request.form("filename2")&"'"
strtj=mid(request.form("filename2"),instrrev(file1,"\"),(instrrev(file1,".")-instrrev(file1,"\")-1))
if file1="" then
response.write "请选择您要导入的Excel表!<p></p>"
%>
<a href=javascript:history.back()>上一页</a>
<%
response.end
end if
'respons.write "'"&request.form("filename")&"'"
myconn="DRIVER={SQL SERVER};SERVER=(local);uid=sa;pwd=sa;DATABASE=qjgsj_data"
dataIntoSqlServer_ceritificate file1,""&strtj&"",myconn
%>
声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。
E-MAIL:11247931@qq.com