如何在EXCEL中,按条件自动提取ACCESS数据库中的数据

如何在EXCEL中,按条件自动提取ACCESS数据库中的数据

问题描述:

img

img

用access自动获取excel里的数据的方法就是通过代码的方式导入。
首先,新建一个工程,在工程中引用如下对象ADO对象(用于连接ACCESS数据库,可用其它方式),添加一个窗体(from1),在窗体上添加如下控件:
两个文本框,用来显示EXCEL文件路径和ACCESS路径;
四个扭钮,两个用来游览,另两个是导入和退出;
两个通用对话框控件,用来打开ACCESS和EXCEL文件,一个进度条控件,用来显示导入进程。
该实例的导入是将ACCESS数据库中表的字段名单独存放在另外一个表中,导入时按表中所存字段名的顺序进行导入,不是按EXCEL表的顺序导入,这样在实际中是很实用的.
因为好多时候EXCEL表中字段顺序和ACCESS中字段顺序有可能不是一致的.
代码如下:

Dim v
Option Explicit
Private Sub cmdLoad_Click()
Dim excel_app As Object
Dim excel_sheet As Object
If txtExcelFile.Text = "" Then
MsgBox "请选择EXCEL表"
Else
Dim new_value As String
Label2.Caption = "正在导入,请稍候..."
Screen.MousePointer = vbHourglass
DoEvents
'' Create the Excel application.
Set excel_app = CreateObject("Excel.Application")
'' Uncomment this line to make Excel visible.
excel_app.Visible = True
'' Open the Excel spreadsheet.
excel_app.Workbooks.open FileName:=txtExcelFile.Text
'' Check for later versions.
If Val(excel_app.Application.Version) >= 8 Then
Set excel_sheet = excel_app.ActiveSheet
Else
Set excel_sheet = excel_app
End If

Dim u ''求EXCEL表中记录的条数,以便控制进度条
u = 1
Do
If Trim$(excel_sheet.Cells(u, 1)) = "" Then Exit Do
u = u + 1
Loop
bar.Max = u - 1
strSQL = "select * from TestValues"
yourRecord.open strSQL, myConn, adOpenDynamic, adLockOptimistic ''打开记录集
Dim sql As String

sql = "select * from fields order by xue"
myRecord.open sql, myConn, adOpenDynamic, adLockBatchOptimistic ''打开字段记录集
myRecord.MoveFirst
'' Get data from the Excel spreadsheet and insert
'' it into the TestValues table.

Dim v ''导入记录,用了两层循环
v = 1
Do
If Trim$(excel_sheet.Cells(v, 1)) = "" Then Exit Do ''外层,
yourRecord.AddNew
Dim i
For i = 1 To myRecord.RecordCount
'' Get the next value.
new_value = Trim$(excel_sheet.Cells(v, i))
'' See if it''s blank.
''If Len(new_value) = 0 Then Exit Do
'' Insert the value into the database.
Dim bb As String
bb = myRecord("name")
yourRecord(bb) = new_value
myRecord.MoveNext
Next i
bar.Value = v
v = v + 1
myRecord.MoveFirst
Loop
yourRecord.Update

'' Comment the rest of the lines to keep
'' Excel running so you can see it.
'' Close the workbook without saving.
excel_app.ActiveWorkbook.Close False
'' Close Excel.
excel_app.Quit
Set excel_sheet = Nothing
Set excel_app = Nothing
myRecord.Close
yourRecord.Close
Set myRecord = Nothing
Set yourRecord = Nothing
Label2.Caption = "导入完毕"
Screen.MousePointer = vbDefault
MsgBox "共导入" & Format$(v - 1) & "条记录"

End If
End Sub
Private Sub Command1_Click()
Unload Me
End Sub
Private Sub Command2_Click(Index As Integer)
''寻找ACCESS数据库
CommonDialog1.Filter = "ACCESS 文件(*.mdb)|*.mdb"
CommonDialog1.CancelError = True
CommonDialog1.ShowOpen
txtAccessFile.Text = CommonDialog1.FileName
End Sub
Private Sub Command3_Click()
''寻找excel数据库
CommonDialog2.Filter = "excel 文件(*.xls)|*.xls"
CommonDialog2.CancelError = True
CommonDialog2.ShowOpen
txtExcelFile.Text = CommonDialog2.FileName
End Sub

Private Sub Form_Load()
Call Module1.lianjie
txtAccessFile.Text = datapath
End Sub
模块(module1)中的代码如下:
Public myConn As New ADODB.Connection ''定义连接字符串
Public myRecord As New ADODB.Recordset ''定义记录集(字段)
Public yourRecord As New ADODB.Recordset ''定义记录集(数据)
Public cntoad As Boolean ''是否正常连接
Public ml ''姓名字段所在列
Public strSQL ''查询字符串
Public MyDatabase As Database ''定义数据库名
Public MyTable As TableDef, MyField As Field ''定义表名和字段名
Public xuehao ''读取字段序号
Public goshiRecord As New ADODB.Recordset ''定义记录集(公式)
Public hxfyn As Boolean
Public hxfbds '' 公式或条件
Public duan ''要统计的字段
Public islinshi As Boolean ''是否为临时公式
Public leiRecord As New ADODB.Recordset ''定义记录集(工资类别)
Public datapath As String ''数据库路径及名
Public table As String ''工资表名
Public lei As String '' 工资类别
Public Sub lianjie() ''打开数据库
On Error Resume Next
myConn.Close
Dim mySQL As String
''设定连接字符串
mySQL = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;"
mySQL = mySQL + "Data Source=" & datapath
myConn.ConnectionString = mySQL ''设定连接
myConn.open ''打开连接
myRecord.ActiveConnection = myConn ''设定RecordSeet的连接对象为Connection
myRecord.CursorLocation = adUseClient
goshiRecord.ActiveConnection = myConn ''设定RecordSeet的连接对象为Connection
goshiRecord.CursorLocation = adUseClient
yourRecord.ActiveConnection = myConn ''设定RecordSeet的连接对象为Connection
yourRecord.CursorLocation = adUseClient
End Sub

Dim mydata As String, SQL As String, i As Integer
Dim cnn As ADODB.Connection
Dim rs As ADODB.Recordset
mydata = "d:\123\DB1.mdb"'----这里是数据库路径
Set cnn = New ADODB.Connection
cnn.Provider = "microsoft.jet.oledb.4.0"
cnn.Open mydata
SQL = "select * from 数据"
Set rs = New ADODB.Recordset
rs.Open SQL, cnn, 3, 2
For i = 1 To [A65535].End(xlUp).Row - 6
rs.AddNew
If Cells(i + 6, 1) & Cells(i + 6, 2) & Cells(i + 6, 3) _
& Cells(i + 6, 4) & Cells(i + 6, 5) & Cells(i + 6, 6) _
& Cells(i + 6, 7) & Cells(i + 6, 8) <> "" Then
rs.Fields("托盘号") = Cells(3, 4)
rs.Fields("产品编号") = Cells(2, 2)
rs.Fields("规格型号") = Cells(4, 8)
rs.Fields("色号") = Cells(2, 8)'----------这个直接是合格-C2
rs.Fields("NAME") = Cells(i + 6, 1)
rs.Fields("VOC") = Round(Cells(i + 6, 2), 3)四舍五入保存3位小数,可以去掉
rs.Fields("ISC") = Round(Cells(i + 6, 3), 3)
rs.Fields("PMP") = Round(Cells(i + 6, 4), 3)
rs.Fields("WMP") = Round(Cells(i + 6, 5), 3)
rs.Fields("IMP") = Round(Cells(i + 6, 6), 3)
rs.Fields("FF") = Round(Cells(i + 6, 7), 3)
rs.Fields("DATE") = Cells(i + 6, 8)
End If
rs.Update
Next i
rs.Close
Set rs = Nothing
Set cnn = Nothing

在excel增加一个按钮,然后复制上面的code至按钮单击事件里。大体思路是这样,具体需要你按照实际业务需求改改。

我也不会,在线等大佬回答,我也很好奇怎么实现