访问表格数据到Excel

时间:2008-12-11 06:52:12

标签: excel ms-access

我有一个问题我被困住了。

我想将Access表导出为Excel文件。目前,我使用DoCmd.TransferSpreadsheet执行此操作,但我希望对导出的数据执行某种格式设置。我可以格式化我发送到Excel的数据,还是必须在Excel中编写一个宏,在从Access导出数据后对其进行格式化?

5 个答案:

答案 0 :(得分:3)

Excel 宏从MS Access数据库中检索数据:

Sub Makro1()
''
Const sDB = "c:\db1.mdb"
Const sSQL = "SELECT * FROM Table1"

    With ActiveSheet.QueryTables.Add(Connection:= _
        "ODBC;DSN=MS Access-database;DBQ=" + sDB + ";FIL=MS Access;MaxBufferSize=2048;PageTimeout=5;" _
        , Destination:=Range("A1"))
        .CommandText = Array(sSQL)
        .Name = "Query1"
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .BackgroundQuery = True
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = True
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .PreserveColumnInfo = True
        .Refresh BackgroundQuery:=False
    End With
End Sub

答案 1 :(得分:3)

我们有一个通用函数,可以将我们的连续表单直接导出到Excel。您需要在可用工具中添加Microsoft Excel库,以使其在您的代码中运行。您可以在所有表​​单中使用此功能。如果要直接从表中导出,可以轻松地进行调整。

Public Function ExportToExcel(x_frm as Form)

Dim ctl As Control, _
    xlApp As Excel.Application, _
    xlBook As Excel.Workbook, _
    xlSheet As Excel.Worksheet, _
    columnName() As String, _
    columnCount as Integer

'suppose Excel is not opened. You can add an extra control for that'
Set xlApp = CreateObject("Excel.Application")

'create an Excel workbook, declare the first sheet'
Set xlBook = xlApp.Workbooks.Add
Set xlSheet = xlBook.Worksheets(1)
xlApp.Visible = False

columnCount = fc().section(0).Controls.Count

'use array columnName() to collect the column names of detail section in tabbed order'
ReDim columnName(columnCount)

For Each ctl In fc().section(0).Controls
    columnName(ctl.TabIndex) = ctl.Name
Next ctl

'This function will add a title to the excel sheet with my favorite formating'
'I can for example decide this title to be bold/Arial/16 on cell(B2)'
addTitleToExcelSheet xlSheet, x_frm.Name

'This function will add the column names to my Excel sheet with my favorite formating'
'for example column names will be added on row 4, columns B to B + columnCount'
addColumnNameToExcelSheet xlSheet, columnName()

'This function will add the column values to my Excel sheet with specific format (date, number, etc)'
'Data will be added to the range defined by '
'row 5 to row 5 + x_frm.recordset.recordcount, '
'columns B to B + columnCount.'
'Recordset values will have to be read according to tab order'
'exported data can depend on recordset filter and orderBy property: your choice'
addColumnValueToExcelSheet xlSheet, columnName(), x_frm.Recordset

'The Excel sheet is made visible and saved under the current folder with the forms name'
xlApp.Visible = True
xlBook.SaveAs x_frm.Name & ".xls"

Set xlBook = Nothing
Set xlSheet = Nothing
Set xlApp = Nothing

End Function

以下是结果的视图。左边是Access窗体,右边是Excel exportToExcel结果。希望你喜欢它。

alt text

答案 2 :(得分:2)

我对此域名没有任何经验,但请注意Excel的行限制小于Access的行限制。

答案 3 :(得分:2)

使用预先配置了格式的Excel电子表格可能更容易。从excel中使用VBA也可能更容易,并从Access中提取数据(并格式化)而不是从Access推送。 Excel VBA将更好地为您准备Excel格式。一个就像另一个一样容易。如果您只是尝试使用Macros,那么Excel宏可能比Access宏更容易。

答案 4 :(得分:1)

如果您使用DoCmd.TransferSpreadsheet并创建原件然后对其进行编辑以使格式正确,则可以再次运行DoCmd.TransferSpreadsheet,它将使用值更新文件但保留格式。

但是,如果某人通过添加新标签或添加计算等来编辑文件,那么DoCmd.TransferSpreadsheet将不再有效,并且会因丑陋的错误消息而失败。因此,我们在环境中所做的是DoCmd.TransferSpreadsheet到具有格式化的原始文件,并通过将文件复制到用户桌面,然后打开该副本以便用户不会打扰原始文件来进行跟进。

这种方法是最简单的代码,简洁且易于维护的解决方案。但它确实需要额外的“源”或原始文件。适用于Access 2007。

豫ICP备18024241号-1