且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将批量数据插入现有数据库?

更新时间:2023-02-10 13:30:30

我将向您展示两种方法。第一种方法只从一张Excel工作表中检索

数据到一个Access表。第二种方法使用

数组,以便您可以从多个Excel表格中检索数据,并且

填充多个Access表格。

----- -----------------------------------------------

'' - 方法1 - 一张Excel表到一个访问表

Sub GetDataFromExcel()

Dim dbsTemp作为数据库

Dim tdfLinked As TableDef


设置dbsTemp = CurrentDb

设置tdfLinked = dbsTemp.CreateTableDef(" tblSheet1")

tdfLinked.Connect = _

" Excel 5.0; HDR = YES; IMEX = 2; DATABASE = C:\ Dir1 \ Book1.xls"

tdfLinked.SourceTableName =" Sheet1
I will show you two methods for this. The first methods only retrieves
data from one Excel sheet to one Access table. The second method uses
array so that you can retrieve data from multiple Excel Sheets and
populate multiple Access tables.
----------------------------------------------------
''--method 1 -- one Excel Sheet to one Access Table
Sub GetDataFromExcel()
Dim dbsTemp As Database
Dim tdfLinked As TableDef

Set dbsTemp = CurrentDb
Set tdfLinked = dbsTemp.CreateTableDef("tblSheet1")
tdfLinked.Connect = _
"Excel 5.0;HDR=YES;IMEX=2;DATABASE=C:\Dir1\Book1.xls"
tdfLinked.SourceTableName = "Sheet1


"

dbsTemp.TableDefs.Append tdfLinked

DoCmd.RunSql" select * into tbl1 from tblSheet1" ;

End Sub

------------------------------- ---------------------


基本上你使用
$ b连接到相应的Excel表格$ b连接字符串(将C:\Dir1 \ Book1.xls替换为实际路径

到Excel工作簿)。哦,HDR = YES表示

Excel表格中的第一行是标题行。


Excel 5.0; HDR = YES; IMEX = 2; DATABASE = C:\Dir1 \ Book1.xls"


此相同的连接字符串适用于Excel97或Excel2000或

Excel2002。注意:当您引用Excel工作表名称(

我的示例中的Sheet1)时,您必须向其附加
"
dbsTemp.TableDefs.Append tdfLinked
DoCmd.RunSql "Select * Into tbl1 From tblSheet1"
End Sub
----------------------------------------------------

Basically you are connecting to a respective Excel sheet using the
connection string here (replace C:\Dir1\Book1.xls with the actual path
to your Excel Workbook). Oh, and HDR=YES means the first row in the
Excel sheet is a header row.

"Excel 5.0;HDR=YES;IMEX=2;DATABASE=C:\Dir1\Book1.xls"

This same connection string works with Excel97 or Excel2000 or
Excel2002. Note: when you reference your Excel Sheet name ("Sheet1" in
my example) you have to append a


符号。实际的工作表名称是

" Sheet1" (或者你的工作表名称)。您附加
sign to it. The actual sheet name is
"Sheet1" (or whatever your sheet name is). You append the