用VB做怎么将Excel数据导入SQL数据库中

用VB做怎么将Excel数据导入SQL数据库中,第1张

这里是我用的代码,估计对你有用:

'On Error Resume Next

Dim fileadd As String

CommonDialog1.ShowOpen

CommonDialog1.Filter = "xls文件(*.xls)|*.xls" '选择你要的文件

fileadd = CommonDialog1.FileName

If fileadd = "" Then Exit Sub

Set xlApp = CreateObject("Excel.Application") '创建EXCEL对象

Set xlBook = xlApp.Workbooks.Open(fileadd) '打开已经存在的EXCEL工件簿文件

xlApp.Visible = False ' = True '设置EXCEL对象可见(或不可见)

Set xlSheet = xlBook.Worksheets(1) '设置活动工作表

For R = 1 To 99999 '行循环

If LTrim(RTrim(xlBook.Worksheets(1).Cells(R, 1))) <>"" Then

sybw.Adodc3.Refresh

sybw.Adodc3.Recordset.Find "ShiGongBuWei_Name='" &LTrim(RTrim(xlBook.Worksheets(1).Cells(R, 1))) &"'"

If sybw.Adodc3.Recordset.EOF Then

sybw.Adodc3.Recordset.AddNew

sybw.Adodc3.Recordset!ShiGongBuWei_Name = LTrim(RTrim(xlBook.Worksheets(1).Cells(R, 1)))

sybw.Adodc3.Recordset!FenXiangGongCheng_ID = bb

sybw.Adodc3.Recordset.Update

sybw.Adodc3.Refresh

'Call log(MM_Users_NameTrue, "增加了施工部位", MM_Companys_ID)

Else

'MsgBox "施工部位重复!", vbOKOnly, "用户信息"

End If

Else

R = 99999 + 1

End If

Next R

xlApp.DisplayAlerts = False '不进行安全提示 '

Set xlSheet = Nothing '

Set xlBook = Nothing '

xlApp.Quit '

Set xlApp = Nothing

VB可将使用Object 数据类型对EXCEL对象进行引用。

Object 数据类型,Object 变量存储为 32 位(4 

个字节)的地址形式,其为对象的引用。利用 Set 语句,声明为 Object 

的变量可以赋值为任何对象的引用。

注意 虽然以 Object 

类型声明的变量足以适应包含对各种对象的引用,但是绑定到变量引用的对象总是在晚期(运行时)绑定。要强迫在早期(编译时间)绑定的话,须将对象的引用赋值给用特定类名称声明的变量。

新建VB的EXE工程,使用如下代码在第一行第一列写入vb *** 作excel:

Option Explicit

  Dim xlapp As Object

  Dim xlBook As Object

  Dim xlSheet As Object

  Dim A As String

Private Sub Command1_Click()

  Set xlapp = CreateObject("excel.application")

  xlapp.Visible = True

  Set xlBook = xlapp.Workbooks.Add

  Set xlSheet = xlBook.worksheets(1)

  A = "vb *** 作excel"

  xlSheet.Cells(1, 1) = A

End Sub


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/sjk/9662815.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-30
下一篇 2023-04-30

发表评论

登录后才能评论

评论列表(0条)

保存