我将ACCESS数据导入EXCEL中,其代码为:
    Dim Conn As New ADODB.Connection
        Dim Rs As New ADODB.Recordset
        Dim ExcelApp As New Excel.Application
        Dim WorkBookObj As Workbook
        Dim SheetObj As Worksheet
              
        Conn.ConnectionString = "Provider=Microsoft.jet.OLEDB.3.51;" & "Data Source=D:\txxy\转速表.mdb"
    Conn.Open
        Rs.Open "Select 显示转速,检测转速1,检测转速2,检测转速3,三次平均值,误差 From 转速表  where 转速表编号='" & Text2.Text & "'", Conn, adOpenKeyset, adLockOptimistic, adCmdText
        Set WorkBookObj = ExcelApp.Workbooks.Open(App.Path & "\abc.xls")
        Set SheetObj = WorkBookObj.Worksheets(1)
           SheetObj.Range("A1").CopyFromRecordset Rs
             Set SheetObj = Nothing
        WorkBookObj.Save
        WorkBookObj.Close
        Set WorkBookObj = Nothing
        'ExcelApp.Quit
        'Set ExcelApp = Nothing
        Rs.Close
        Set Rs = Nothing
        Conn.Close
        Set Conn = Nothing
        MsgBox "OK!请您打开abc.xls文件察看!"
        ExcelApp.Visible = True
但存在以下几个问题:
 1、没有表头 如 检测记录
 2、数据导入到excel时如何控制数据的位置。
 3、如何将abc.xls显示出来。
请各位大侠赐教,最好有代码.
  [email protected]

解决方案 »

  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
      

  2.   

    to hdhai9451(Water Space--海洋空間
    你的程序不能完全控制数据的位置。