如何用VB读写EXCEL中数据

2024-11-07 16:52:10
推荐回答(1个)
回答(1):

Private Sub Form_Load()
Text1.Text = App.Path & "\123.xls"
Text2.Text = App.Path & "\123.mdb"
Text3.Text = "sheet1"
Text4.Text = "sheet1"
Data1.DatabaseName = App.Path & "\123.mdb"
End Sub
Private Sub Command1_Click()
Dim db As Database
Dim sheet As String, excelpath As String, AccessPath As String, AccessTable As String
AccessPath = Text2.Text '数据库路径
excelpath = Text1.Text '电子表格路经
AccessTable = Text4.Text '数据库内表格
sheet = Text3.Text '电子表格内工作表
Set db = OpenDatabase(excelpath, True, False, "Excel 5.0") '打开电子表格文件
SQL = ("Select * into [;database=" & AccessPath & "]." & AccessTable & " FROM [" & sheet & "$]")
db.Execute (SQL) '将电子表格导入数据库
Data1.RecordSource = "sheet1"
Data1.Refresh
DBGrid1.Refresh '显示电子表格导入到数据库的数据
End Sub
上面的是把EXCEL读取到数据库中,你做点修改就行了.一个Data控件.