Public Sub OutDataToExcel(Flex As SSDBGrid)    '导出至Excel
    Dim sValue As String
    Dim i As Integer
    Dim j As Integer
    Dim k As Integer
    On Error GoTo Ert
    Me.MousePointer = 11
    Dim Excelapp As Excel.Application
    Set Excelapp = New Excel.Application
    On Error Resume Next
    DoEvents
    Excelapp.SheetsInNewWorkbook = 1
    Excelapp.Workbooks.Add
    With Flex
        k = .Rows
        For i = 0 To k - 1
            For j = 0 To 4
'               DoEvents
               sValue = "'" & .Columns(j).Value
               Excelapp.ActiveSheet.Cells(i, j + 1) = sValue
            Next j
        Next i
        
    End With
    Me.MousePointer = 0
    Excelapp.Visible = True
    Excelapp.Sheets.PrintPreview   
Ert:
    If Not (Excelapp Is Nothing) Then
        Excelapp.Quit
    End If
End Sub
当有10行记录时,上面这段语句只能将指针所在处的行导10边。指针不会下移。如何解决。

解决方案 »

  1.   

    用你的方法較難,換個方法吧
    '請你自己加個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