vb将excel数据导入mysql_VB将excel数据导入数据库以及将数据库数据导入excel源代码 .: NOVOTS KMS...

本文介绍了如何使用VBA在Excel和Access之间进行数据操作,包括从Excel表格中读取数据并插入到Access数据库的合同管理表中。通过创建连接、执行SQL查询和更新,展示了数据处理的完整过程。
摘要由CSDN通过智能技术生成

Private Sub Command11_Click()

Dim conn

Dim conn2

Dim a As String

With CommonDialog1

.Filter = "EXCEL文件[*.XLS]|*.xls"

.ShowOpen

a = .FileName

Text2.Text = a

End With

Set conn = CreateObject("ADODB.Connection")

conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Jet OLEDB:Database Password=;Persist Security Info=False;Data Source=" & App.Path + "\database.mdb " & ""

Set conn2 = CreateObject("ADODB.Connection")

conn2.Open "Provider=Microsoft.Jet.OLEDB.4.0;Jet OLEDB:Database Password=;Extended properties='Excel 8.0;HDR=Yes;IMEX=1';Data Source=" & Text2.Text & ""

SQL = "SELECT * FROM [Sheet1$]"

Set rs = conn2.Execute(SQL)

While Not rs.EOF

SQL = "insert into 合同管理表 ([姓名],[性别],[部门],[签订时间],[签订次数],[合同期限]) values('" & fixsql(rs(0)) & "','" & fixsql(rs(1)) & "','" & fixsql(rs(2)) & "','" & fixsql(rs(3)) & "','" & fixsql(rs(4)) & "','" & fixsql(rs(5)) & "')"

conn.Execute (SQL)

rs.MoveNext

Wend

conn.Close

Set conn = Nothing

conn2.Close

Set conn2 = Nothing

Adodc1.Refresh

DataGrid1.Refresh

End Sub

Function fixsql(str)

Dim newstr

newstr = str

If IsNull(newstr) Then

newstr = ""

Else

newstr = Replace(newstr, "'", "''")

End If

fixsql = newstr

End Function

将数据库数据导入excel:

Private Sub Command6_Click()

Set xlApp = CreateObject("excel.application")

Set xlBook = xlApp.Workbooks.Add

Set xlSheet = xlBook.Worksheets(1)

xlApp.Visible = True '设置EXCEL可见

On Error Resume Next

If Err.Number <> 0 Then Set xlApp = CreateObject("Excel.Application")

Set xlBook = xlApp.Workbooks.Add

Set xlSheet = xlBook.ActiveSheet

For k = 1 To DataGrid1.Columns.Count

xlSheet.Cells(1, k) = DataGrid1.Columns(k - 1).Caption

Next k

For I = 1 To Adodc1.Recordset.RecordCount + 1

For j = 0 To DataGrid1.Columns.Count

xlSheet.Cells(I + 1, j + 1) = Adodc1.Recordset(j) '

Next j

Adodc1.Recordset.MoveNext

Next I

end sub

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值