VBA Excel - 将列名保存到MS Access的电子表格中

时间:2015-04-04 11:59:49

标签: excel vba excel-vba ms-access export-to-excel

我在Visual Studio Express中使用VBA。我想要做的是给我通过VB导出MS Access DB创建的excel spreedsheet的顶行,列名,即我在DB中的名称。

有10列第9个被跳过,我也将spreedsheet分开以允许第一行有标题,我如何用我的数据库的列名填充我的spreedsheet的第一行? 如果直接通过代码分配名称而不是从DB传递列标题也可以。

我的代码:

Public Sub ExportEx()
    Dim strSQL_Query As String
    Dim oCN As ADODB.Connection
    Dim oCMD As ADODB.Command
    Dim oRecords As ADODB.Recordset
    Dim strDBPath As String
    Dim varValues As Object
    Dim lngRows As Long
    Dim lngCols As Long
    Dim strCN As String



    strDBPath = Application.StartupPath & "\SCO_Leaderboard.accdb"

    strCN = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & strDBPath & ";" & _
                    "Persist Security Info=False;"


    strSQL_Query = "Select top 10 Rank, Username, Time_Played, Lv, EXP, Floor, Col, Logins, Status FROM tblUsers ORDER BY Rank ASC"

    Dim oExcel As Object
    Dim oBook As Object
    Dim oSheet As Object
    oExcel = CreateObject("Excel.Application")
    oBook = oExcel.Workbooks.Add
    oSheet = oBook.Worksheets(1)

    oCN = New ADODB.Connection
    oCN.ConnectionString = strCN
    oCN.Open()


    oCMD = New ADODB.Command
    oCMD.ActiveConnection = oCN
    oCMD.CommandText = strSQL_Query
    oRecords = oCMD.Execute

    varValues = oRecords.GetRows


    lngCols = UBound(varValues, 2)
    lngRows = UBound(varValues, 1)
    oSheet.Range("A2", oSheet.Range("A2").Offset(lngRows, lngCols)) = varValues


    oBook.SaveAs(Application.StartupPath & "\Top_10_All_Time.xls")
    oExcel.Quit()

    MsgBox("An Excel spreadsheet has been created under:" & vbNewLine & vbNewLine & Application.StartupPath & "\Top_10_All_Time.xls")
    '' Clean up...

    oCMD = Nothing
    oCN.Close()
    oCN = Nothing

另一方面,如何在Excel中将字段分隔开以使所有数据都适合列?

感谢您的帮助,

安迪

3 个答案:

答案 0 :(得分:2)

在VBA中,有两种方法可以将Access表/查询数据导出到Excel电子表格:

1)TransferSpreadsheet方法

此命令将导出所有字段和记录。因此,将您的VBA字符串保存为存储的查询对象,并在以下命令中引用它:

DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml, _
               "yourtableorqueryname", "fullpathtoExcelFile", True

2)CopyFromRecordset方法

此命令仅导出记录。但是,您可以使用记录集的Fields属性填充第一行。请注意,下面的代码假定您使用ADODB连接创建名为rst的ADO记录集。

Dim rst As ADODB.Recordset

Set rst = New ADODB.Recordset

rst = "Select top 10 Rank, Username, Time_Played, Lv, EXP, Floor, Col, Logins, Status" _
  & " FROM tblUsers ORDER BY Rank ASC", oCN

oSheet.Range("A1").Select
For Each fld In rst.Fields
   oExcel.ActiveCell = fld.Name
   oExcel.ActiveCell.Offset(0, 1).Select
Next
'REMOVE BELOW IF YOU WANT ONLY COLUMN HEADERS NOT DATA
oSheet.Range("A2").CopyFromRecordset rst
'TO AUTO FIT (SPACE OUT) COLUMNS
osheet.Range("A1:I" & rst.RecordCount + 1).Columns.AutoFit

答案 1 :(得分:0)

这适用于Access,我不确定它是否适用于您的情况:

Select top 10 Rank As Header1, Username As Header2, Time_Played As Header3 ...

答案 2 :(得分:0)

您必须从oRecords的示意图中检索字段集合:

oRecords = oCMD.Execute

或者只是从strSQL中解析字段名...

或 - 非常简单,因为您定义字段名称并从字段名称构建SQL - 将它们放在一个数组中,然后从该数组中构建范围的第一行。