數據庫從VB導入到Excel

类别:.NET开发 点击:0 评论:0 推荐:

'請你自己加個CommonDialog控件

Private Sub Command3_Click()
    Dim objFileSystem As Object
    Dim objExcelText As Object
    Dim strTableString As String, i As Integer, strFileName As String
    Dim pubConn As New ADODB.Connection
    Dim rsTable As New ADODB.Recordset
    Dim strConn As String
    Dim strSQL As String

    strConn = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=develop; password=12345;Data Source=ServerNmae"
    pubConn.Open strConn
    rsTable.CursorLocation = adUseClient
    strSQL = "select top 10 * from gate_register"
    rsTable.Open strSQL, pubConn, adOpenDynamic, adLockOptimistic
   
    For i = 0 To rsTable.Fields.Count - 1
        strTableString = strTableString & rsTable.Fields(i).Name & Chr(9)  '獲取字段名
    Next
    strTableString = strTableString & rsTable.GetString     '字段名+數據庫的記錄
   
    cmDialog.CancelError = False
    cmDialog.FileName = "FileName"  '默認生成的文件名
    cmDialog.DialogTitle = "Save Export File"
    cmDialog.Filter = "Excel (*.xls)|*.xls|文本文件(*.DBF)|*.DBF|檔案文件(*.doc)|*.doc|所有文件(*.*)|*.*"
    cmDialog.DefaultExt = "*.xls"
    cmDialog.ShowSave
    strFileName = cmDialog.FileName
   
    Set objFileSystem = CreateObject("Scripting.FileSystemObject")
    Set objExcelText = objFileSystem.createtextfile(strFileName, True)
    objExcelText.writeline (strTableString)
   
    objExcelText.Close
    Set objFileSystem = Nothing
End Sub

本文地址:http://com.8s8s.com/it/it42322.htm