用vb将Excel中的数据写入数据库

用vb将Excel中的数据写入数据库,第1张

Private Sub Command1_Click()

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

Call Dosql("INSERT INTO CB_JiXieFeiYong (danwei_name) VALUES (" &LTrim(RTrim(xlBook.Worksheets(1).Cells(R, 1))) &")")

Else

R = 99999 + 1

End If

Next R

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

Set xlSheet = Nothing '

Set xlBook = Nothing '

xlApp.Quit '

Set xlApp = Nothing

Unload Me

End Sub

Private Sub Dosql(ByVal tn As String) '执行SQL语句

Dim sql As String

Set conn = New ADODB.Connection

conn.ConnectionString = condstr

conn.Open

conn.Execute tn

conn.Close

End Sub

这段代码估计对你有用。

方法一:在数据库中建一个表,把当前最后的输入值保存在该表里,,每次执行程序都从数据库里读取,

方法二:在程序中建一个文本文件或xml文件,每次都写入文件中,下次从文件中读取

方法三:把输入值存入注册表的某个位置,下次从注册表里读取!


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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存