首页 热点资讯 义务教育 高等教育 出国留学 考研考公

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

发布网友 发布时间:2022-04-21 21:43

我来回答

2个回答

热心网友 时间:2024-10-14 21:52

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

这段代码估计对你有用。

热心网友 时间:2024-10-14 21:54

是什么数据库

热心网友 时间:2024-10-14 21:55

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

这段代码估计对你有用。

热心网友 时间:2024-10-14 21:48

是什么数据库

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com