在VB中调用EXCEL,先引用MS EXCEL 10。0 APPLICATION Dim xlApp As Excel.Application
Dim wbzhishu As Excel.Workbook
Dim shtzhishu As Excel.Worksheet
On Error Resume Next
Set xlApp = GetObject(, "Excel.Application")
If Err.Number <> 0 Then
Set xlApp = CreateObject("Excel.Application")
End If
Err.Clear
Set wbzhishu = xlApp.Workbooks.Open("文件名")
Set shtzhishu = wbzhishu.Sheets("sheet1")
shtzhishu.Cells(1, 1) = "1"
shtzhishu1.Cells(1, 2) = "2"这样赋值下去就行了
Dim wbzhishu As Excel.Workbook
Dim shtzhishu As Excel.Worksheet
On Error Resume Next
Set xlApp = GetObject(, "Excel.Application")
If Err.Number <> 0 Then
Set xlApp = CreateObject("Excel.Application")
End If
Err.Clear
Set wbzhishu = xlApp.Workbooks.Open("文件名")
Set shtzhishu = wbzhishu.Sheets("sheet1")
shtzhishu.Cells(1, 1) = "1"
shtzhishu1.Cells(1, 2) = "2"这样赋值下去就行了
Dim CN As New Connection
Dim FileName As String
FileName = App.Path + "\gsb2.mdb" '加入你的数据库文件
CN.Open "provider=microsoft.jet.oledb.4.0;data source=" + FileName + ";jet oledb:database password='9702024'"
Dim Irowcount As Integer
Dim Icolcount As Integer
Dim xlApp As New Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Dim xlQuery As Excel.QueryTable
With Rs_Data
If .State = adStateOpen Then
.Close
End If
.ActiveConnection = CN
.CursorLocation = adUseClient
.CursorType = adOpenStatic
.LockType = adLockReadOnly
'strOpen 为你要打开的表或查询
.Source = strOpen
.Open
End With
Set xlApp = CreateObject("Excel.Application")
Set xlBook = Nothing
Set xlSheet = Nothing
Set xlBook = xlApp.Workbooks().Add
Set xlSheet = xlBook.Worksheets("sheet1")
xlApp.Visible = True
'添加查询语句,导入EXCEL数据
Set xlQuery = xlSheet.QueryTables.Add(Rs_Data, xlSheet.Range("b1"))
End Sub
'这样可以将查询的大量数据直接到入excel文件中