将数据从 Excel 提交到 Access
Posted
技术标签:
【中文标题】将数据从 Excel 提交到 Access【英文标题】:Submiting data from Excel to Access 【发布时间】:2017-06-16 18:37:40 【问题描述】:我每天都会收到多个 Excel 文件。每一个都是相同的,并包含建筑工头的日常日志。在这些 excel 表上,我有一个表格,工头每天都会用他们的新信息更新。我希望能够在访问中汇集这些表中的所有数据,并使用访问报告功能为工头组创建每日报告。
如果没有更好的方法来做到这一点,我如何在访问时将这些信息从多个电子表格导入或导出到一个表中?
如果这也可以由工头远程在 SQL Server 中完成,那也很高兴。
编辑:我面临的特殊困难是只允许我选择表格所在的工作表而不是表格本身。除了表格之外,工作表上有更多信息。有没有办法在导入时选择特定的表?另外,我希望能够在 VBA 中对此进行编程,这样我就可以按 Excel 工作表上的一个按钮来导出到我的访问数据库
【问题讨论】:
用T-SQL写一个存储过程,放在定时器上。 如果这些工作表符合表格的布局,Access 的import
功能很简单。你面临的特殊困难是什么?
我面临的特殊困难是只允许我选择表格所在的工作表而不是表格本身。除了表格之外,工作表上有更多信息。有没有办法在导入时选择特定的表?另外,我希望能够在 VBA 中对此进行编程,这样我就可以按 Excel 工作表上的一个按钮来导出到我的访问数据库。
工作表上的“表格”是什么意思 - 是指范围吗?
【参考方案1】:
我使用这个代码。
Sub ExportToAccess()
Dim PathOfAccess As String, myFn As String
Dim strConn As String, strSQL As String
PathOfAccess = "C:\Users\USER\Documents\Database1.accdb"
myFn = ThisWorkbook.FullName
strConn = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=" & PathOfAccess & ";"
Set cn = CreateObject("ADODB.Connection")
cn.Open strConn
strSQL = "INSERT INTO table1 select * from [Sheet1$] IN '' " _
& "[Excel 8.0;HDR=YES;IMEX=2;DATABASE=" & myFn & "]"
cn.Execute strSQL
End Sub
Sub ImportFromAccess()
Dim Rs As Object
Dim strConn As String, strSQL As String
Dim i As Integer
Dim Ws As Worksheet
Dim PathOfAccess As String
PathOfAccess = "C:\Users\USER\Documents\Database1.accdb"
strConn = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=" & PathOfAccess & ";"
Set Rs = CreateObject("ADODB.Recordset")
strSQL = "SELECT * FROM table1 "
Rs.Open strSQL, strConn
Set Ws = ActiveSheet
If Not Rs.EOF Then
With Ws
.Range("a1").CurrentRegion.ClearContents
For i = 0 To Rs.Fields.Count - 1
.Cells(1, i + 1).Value = Rs.Fields(i).Name
Next
.Range("a" & 2).CopyFromRecordset Rs
End With
End If
Rs.Close
Set Rs = Nothing
End Sub
【讨论】:
【参考方案2】:您可以轻松地将数据从 Excel 导出到 Access。
Sub DAOFromExcelToAccess()
' exports data from the active worksheet to a table in an Access database
' this procedure must be edited before use
Dim db As Database, rs As Recordset, r As Long
Set db = OpenDatabase("C:\FolderName\DataBaseName.mdb")
' open the database
Set rs = db.OpenRecordset("TableName", dbOpenTable)
' get all records in a table
r = 3 ' the start row in the worksheet
Do While Len(Range("A" & r).Formula) > 0
' repeat until first empty cell in column A
With rs
.AddNew ' create a new record
' add values to each field in the record
.Fields("FieldName1") = Range("A" & r).Value
.Fields("FieldName2") = Range("B" & r).Value
.Fields("FieldNameN") = Range("C" & r).Value
' add more fields if necessary...
.Update ' stores the new record
End With
r = r + 1 ' next row
Loop
rs.Close
Set rs = Nothing
db.Close
Set db = Nothing
End Sub
'这在 Excel 中运行
现在,如果你想从多个 Excel 文件中导入数据(我猜是在一个文件夹中),你可以试试下面的脚本。
Dim strPathFile As String, strFile As String, strPath As String
Dim strTable As String
Dim blnHasFieldNames As Boolean
' Change this next line to True if the first row in EXCEL worksheet
' has field names
blnHasFieldNames = False
' Replace C:\Documents\ with the real path to the folder that
' contains the EXCEL files
strPath = "C:\Documents\"
' Replace tablename with the real name of the table into which
' the data are to be imported
strTable = "tablename"
strFile = Dir(strPath & "*.xls")
Do While Len(strFile) > 0
strPathFile = strPath & strFile
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, _
strTable, strPathFile, blnHasFieldNames
' Uncomment out the next code step if you want to delete the
' EXCEL file after it's been imported
' Kill strPathFile
strFile = Dir()
Loop
' THIS RUNS IN ACCESS
【讨论】:
以上是关于将数据从 Excel 提交到 Access的主要内容,如果未能解决你的问题,请参考以下文章
jsp编写网站中,如何将数据库中的表导出到txt或者word,excel