如何将在VB窗体中查询excel中内容 20
3个回答
展开全部
首先需要在工程中引用Microsoft Excel Object Library
代码参照下边的例子
Dim sA, sB, sC, sD, sE, sF, sG, sH, sName, sDistrictName, sDepName, sDepNameOld, sDepID, sParentID, sFeeID
Dim oRdA As New ADODB.Recordset
Dim oRdB As New ADODB.Recordset
Dim oXl As Excel.Application
Dim oWb As Workbook
Dim oWs As Excel.Worksheet
Dim iA, iB, iC, iD, iError
Dim lCount
Dim bExcelRunning 'Excel是否已运行
If mlErrors <> 0 Then
MsgBox "请先检查导入数据的正确性!检查通过后方可进行导入!", vbOKOnly, "系统提示"
Exit Sub
End If
'0 要求对数据导入进行确认
'1 获得导入的数据源并打开
'1.1获得导入的数据源
sName ="你的excel文件名全路径"
If sName = "" Then Exit Sub
sA = Dir(sName, vbNormal)
If sA = "" Then Exit Sub
'1.2 打开数据源
Screen.MousePointer = vbHourglass
On Error GoTo ConnectError
lblInfo.Caption = "打开数据源"
Set oXl = GetObject("", "Excel.Application")
Set oWb = oXl.Workbooks.Open(sName)
lblInfo.Caption = "连接数据库"
'2 打开数据库连接
sA = "Provider=SQLOLEDB; Data Source=数据库服务器ID,1433; Network Library=DBMSSOCN;" & _
";Initial Catalog=数据库名;User ID=用户ID;Password=密码"
'oracle方式
' sA = "Provider=OraOLEDB.Oracle;Data Source=net服务名;User Id=用户ID;Password=密码"
goConnect.ConnectionString = sA
goConnect.Open
On Error GoTo Morn
'4 清理数据库
's_ClearAllTable '调用清理数据库过程把以前数据清理掉
'5 导入信息
Set oWs = oXl.Worksheets(1)
'.2开始数据导入 1万行一提交
goConnect.BeginTrans
With oWs
For iA = 2 To Prb.Max
lCount = lCount + 1
sA = "insert into department(DEPID,DEPNAME,DEPCODE,depCompleteName,depAddress,deleted) values('" & _
CStr(lCount) & "','" & .Cells(iA, 2) & "','" & .Cells(iA, 1) & "','" & .Cells(iA, 4) & _
"','" & .Cells(iA, 3) & "',0)"
goConnect.Execute sA
Next
End With
goConnect.CommitTrans
代码参照下边的例子
Dim sA, sB, sC, sD, sE, sF, sG, sH, sName, sDistrictName, sDepName, sDepNameOld, sDepID, sParentID, sFeeID
Dim oRdA As New ADODB.Recordset
Dim oRdB As New ADODB.Recordset
Dim oXl As Excel.Application
Dim oWb As Workbook
Dim oWs As Excel.Worksheet
Dim iA, iB, iC, iD, iError
Dim lCount
Dim bExcelRunning 'Excel是否已运行
If mlErrors <> 0 Then
MsgBox "请先检查导入数据的正确性!检查通过后方可进行导入!", vbOKOnly, "系统提示"
Exit Sub
End If
'0 要求对数据导入进行确认
'1 获得导入的数据源并打开
'1.1获得导入的数据源
sName ="你的excel文件名全路径"
If sName = "" Then Exit Sub
sA = Dir(sName, vbNormal)
If sA = "" Then Exit Sub
'1.2 打开数据源
Screen.MousePointer = vbHourglass
On Error GoTo ConnectError
lblInfo.Caption = "打开数据源"
Set oXl = GetObject("", "Excel.Application")
Set oWb = oXl.Workbooks.Open(sName)
lblInfo.Caption = "连接数据库"
'2 打开数据库连接
sA = "Provider=SQLOLEDB; Data Source=数据库服务器ID,1433; Network Library=DBMSSOCN;" & _
";Initial Catalog=数据库名;User ID=用户ID;Password=密码"
'oracle方式
' sA = "Provider=OraOLEDB.Oracle;Data Source=net服务名;User Id=用户ID;Password=密码"
goConnect.ConnectionString = sA
goConnect.Open
On Error GoTo Morn
'4 清理数据库
's_ClearAllTable '调用清理数据库过程把以前数据清理掉
'5 导入信息
Set oWs = oXl.Worksheets(1)
'.2开始数据导入 1万行一提交
goConnect.BeginTrans
With oWs
For iA = 2 To Prb.Max
lCount = lCount + 1
sA = "insert into department(DEPID,DEPNAME,DEPCODE,depCompleteName,depAddress,deleted) values('" & _
CStr(lCount) & "','" & .Cells(iA, 2) & "','" & .Cells(iA, 1) & "','" & .Cells(iA, 4) & _
"','" & .Cells(iA, 3) & "',0)"
goConnect.Execute sA
Next
End With
goConnect.CommitTrans
展开全部
Dim xlapp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSHEET As Excel.Worksheet
Set xlapp = CreateObject("excel.application") '这里是小写,如果出错的话,下面还有大写的创建。
Set xlBook = xlapp.Workbooks.Open(Str_xls) 'Str_xls是excel的文件路径
Set xlSHEET = xlBook.Worksheets(1)
'下面是获取excel的值
x= xlSHEET.Cells(hang_bianliang,lie_bianliang)
Dim xlBook As Excel.Workbook
Dim xlSHEET As Excel.Worksheet
Set xlapp = CreateObject("excel.application") '这里是小写,如果出错的话,下面还有大写的创建。
Set xlBook = xlapp.Workbooks.Open(Str_xls) 'Str_xls是excel的文件路径
Set xlSHEET = xlBook.Worksheets(1)
'下面是获取excel的值
x= xlSHEET.Cells(hang_bianliang,lie_bianliang)
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
如果你的EXCEL是第一行是表头,下面则数据内容,则,可以用ADO连接表用select * from [sheet1$]
方法2:
也可以引用microsoft excel,用excel.application来打开excel文件,再操作excel。
20分的简要回答如上。哈
方法2:
也可以引用microsoft excel,用excel.application来打开excel文件,再操作excel。
20分的简要回答如上。哈
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |