【问题标题】:Access table data to Excel将表数据访问到 Excel
【发布时间】:2010-09-26 09:28:03
【问题描述】:

我遇到了一个问题。

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

【问题讨论】:

  • 什么样的格式?如果是数据格式,也许您可​​以创建一个查询,使用 Format() 和其他函数来格式化输出,然后使用 TransferSpreadsheet 将 that 导出到 Excel。如果您想进行 Excel 格式设置,但您不清楚您想要/需要什么。

标签: excel ms-access


【解决方案1】:

我对这个域没有任何经验,但请注意 Excel 的行数限制远小于 Access。

【讨论】:

  • “新”excel 已取消该限制。仅供参考。
  • 没有。仍然有一个行限制(可能是 1 048 576)远远低于数据库表/记录容量(我什至不记得了),这意味着必须考虑到这一点
【解决方案2】:

从预先配置了格式的 Excel 电子表格开始可能会更容易。在 excel 中使用 VBA 并从 Access 中提取数据(并对其进行格式化)而不是从 Access 中推送数据也可能更容易。 Excel VBA 将更好地为您进行 Excel 格式设置。一个和另一个一样容易。如果您尝试仅使用宏来执行此操作,那么使用 Excel 宏可能仍然比使用 Access 宏更容易。

【讨论】:

    【解决方案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
    

    【讨论】:

      【解决方案4】:

      我们有一个通用函数,可以将我们的连续表单直接导出到 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 结果。希望你喜欢。

      【讨论】:

      • 我在这一行遇到错误columnCount = fc().Section(0).Controls.Count Sub or function not defined。 fc() 是什么?谢谢
      【解决方案5】:

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

      但是,如果有人随后通过添加新选项卡或添加计算等来编辑文件,则 DoCmd.TransferSpreadsheet 将不再工作,并且会失败并显示丑陋的错误消息。所以我们在我们的环境中所做的是DoCmd.TransferSpreadsheet 到一个带有格式的原始文件,然后通过将文件复制到用户桌面,然后打开该副本,这样用户就不会打扰原始文件。

      这种方法是代码最少、干净且易于维护的解决方案。但它确实需要一个额外的“源”或原始文件。在 Access 2007 中工作。

      【讨论】:

        猜你喜欢
        • 2019-01-15
        • 2011-05-09
        • 2010-09-27
        • 1970-01-01
        • 2013-04-03
        • 1970-01-01
        • 2023-03-05
        • 1970-01-01
        相关资源
        最近更新 更多