VBA 将 Excel 电子表格逐行导入 Access
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5310582/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
VBA to import Excel Spreadsheet into Access line-by-line
提问by MSpeed
I'm debugging some code and need to find out where aDoCmd.TransferSpreadsheet acImport, , ".....
fails so I've decided to import it 'manually' line-by-line to see where it falls over.
我正在调试一些代码,需要找出DoCmd.TransferSpreadsheet acImport, , ".....
失败的地方,所以我决定“手动”逐行导入它以查看它在哪里失败。
I suppose something like this is what I'm looking for:
我想这样的事情就是我要找的:
mySpreadSheet = ConnectTo(Spreadsheet.xlsx)
while(!mySpreadSheet.EOF)
get(mySpreadSheet.nextLine)
SQL("UPDATE MyTable with mySpreadSheet.nextLine")
I've tried Googling to no avail. Any help is much appreciated!
我试过谷歌搜索无济于事。任何帮助深表感谢!
Additional info:
附加信息:
- The column names of the spreadsheet and the Access table are identical.
- Every data type is nvarchar(MAX) (Or "Memo" as Access calls it)
- The table is a linked table to SQL Server 2008
- 电子表格和 Access 表的列名是相同的。
- 每个数据类型都是 nvarchar(MAX) (或 Access 称之为“备忘录”)
- 该表是到 SQL Server 2008 的链接表
回答by Fink
ADO works well if you have a well defined sheet layout of your data (HansUp answer). If you need added control before loading the objects, you can hook into the excel workbook, and then pull out whatever data you like. It really depends on the level of control you need.
如果您的数据有明确定义的工作表布局(HansUp 答案),ADO 效果很好。如果您需要在加载对象之前添加控件,您可以挂钩到 excel 工作簿,然后拉出您喜欢的任何数据。这实际上取决于您需要的控制级别。
Public Sub LoadExcelToAccess(xlPath As String)
'uses late binding to open excel workbook and open it line by line
'make reference to Microsoft Excel xx.x Object Model to use native functions, requires early binding however
Dim xlApp As Object 'Excel.Application
Dim xlWrk As Object 'Excel.Workbook
Dim xlSheet As Object 'Excel.Worksheet
Dim i As Long
Dim sql As String
Set xlApp = VBA.CreateObject("Excel.Application")
'toggle visibility for debugging
xlApp.Visible = False
Set xlWrk = xlApp.Workbooks.Open(xlPath)
Set xlSheet = xlWrk.Sheets("Sheet1") 'modify to your perticular sheet
'depends on what your trying to do with the sheet
For i = 1 To 10
'quick and dirty: best to load items into custom class collection, then do processing there
sql = "Insert Into [Temp] (Col1) VALUES (" & xlSheet.Cells(i, 1).Value & ")"
DoCmd.RunSQL sql
Next i
'make sure to dispose of objects
xlWrk.Close
xlApp.Quit
Set xlSheet = Nothing
Set xlWrk = Nothing
Set xlApp = Nothing
End Sub
回答by HansUp
You can create an ADO connection to your spreadsheet (see Connection strings for Excel 2007), then open an ADO recordset with that connection (see StackOverflow: ADODB recordset in VBA says excel field is empty when it's notfor example).
您可以创建到电子表格的 ADO 连接(请参阅Excel 2007 的连接字符串),然后使用该连接打开 ADO 记录集(例如,请参阅StackOverflow:VBA 中的 ADODB 记录集说 excel 字段为空)。
Then move through the recordset rows, and create a SQL INSERT statement using the row's values.
然后在记录集行中移动,并使用该行的值创建 SQL INSERT 语句。
strInsert = "INSERT INTO MyTable (first_field, second_field) VALUES ('" & -
rs2.Field(0).Value & "', '" & rs2.Field(1).Value & "');"
Debug.Print strInsert
CurrentDb.Execute strInsert, dbFailonerror
That code snipped assumes first_field and second_field are text data types. If they are numeric, lose the single quotes.
剪断的代码假定 first_field 和 second_field 是文本数据类型。如果它们是数字,则丢失单引号。
I think that does roughly what you asked. However, before resorting to code I would check whether the spreadsheet imports cleanly into a new native Access table. (Also, check whether the data types and constraints on that new table are compatible with those of the linked SQL Server table.) If that works, maybe try importing the spreadsheet directly into SQL Server from the Management Studio, or whatever tool is appropriate.
我认为这大致符合您的要求。但是,在使用代码之前,我会检查电子表格是否干净地导入到新的本机 Access 表中。(此外,检查该新表的数据类型和约束是否与链接的 SQL Server 表的数据类型和约束兼容。)如果可行,可以尝试将电子表格从 Management Studio 或任何合适的工具直接导入 SQL Server。
回答by Alex
Two additional options:
两个附加选项:
Link the spreadsheet in Access like a table. In Access 2007, go to "external data" pane and select "Import Excel Spreadsheet". You should import to an existing datatable, a new datatable or just link to Excel file. Then, you would work with this new "Excel" table like an Access table (regarded the performance issues, in last case).
Try to fix the
Docmd.TransferSpreadsheet
problem. I've been using this method for some years, and it works fine, despite it ought to be a little tricky in some cases (I belive its your case). Please, its worthy if you give more information about your problem with this method, including your Access and Excel version.
像表格一样链接 Access 中的电子表格。在 Access 2007 中,转到“外部数据”窗格并选择“导入 Excel 电子表格”。您应该导入到现有数据表、新数据表或仅链接到 Excel 文件。然后,您可以像使用 Access 表一样使用这个新的“Excel”表(关于性能问题,在最后一种情况下)。
尝试解决
Docmd.TransferSpreadsheet
问题。我已经使用这种方法好几年了,它工作得很好,尽管在某些情况下它应该有点棘手(我相信它是你的情况)。请提供有关此方法问题的更多信息,包括您的 Access 和 Excel 版本,这是值得的。
I hope I've helped. Good luck.
我希望我有所帮助。祝你好运。
回答by David-W-Fenton
For troubleshooting purposes, try linking to the spreadsheet and see what problems you encounter, including data displaying wrong when you view it in datasheet view.
出于故障排除目的,请尝试链接到电子表格并查看您遇到的问题,包括在数据表视图中查看时数据显示错误。
回答by Beth
You can also try just copying your Excel data to the clipboard and pasting it into your Access table. Whatever fails will get written into a 'Paste Errors' table by Access.
您也可以尝试将 Excel 数据复制到剪贴板并将其粘贴到 Access 表中。任何失败都会被 Access 写入“粘贴错误”表。