代码之家  ›  专栏  ›  技术社区  ›  honey

从Acces to Excel中提取的数据会自动转到比定义的工作表更多的工作表

  •  0
  • honey  · 技术社区  · 10 年前

    我正在尝试从MS Access to Excel表中提取一些数据,这是一种已经定义的模板。例如,我使用一个excel文件,其中包含一个名为 后果 明确提到要在结果表中提取。数据很好,但表Result未被使用,尽管名为 结果1 使用与Ms-Access的OutputTable相同的数据创建。

    我使用的代码如下:

        SourceFile = CurrentProject.Path & "\Template\" & "Input_Template.xlsx"
        DestinFile = CurrentProject.Path & "\Output\" & "Output_" & sDateTimeStamp & ".xlsx"
        FileCopy SourceFile, DestinFile
        DoCmd.TransferSpreadsheet acExport, , "OutputTable", DestinFile, False, "Result"
    

    我在系统上创建了这些文件夹Template(具有Input_Template.xlsx文件)和Output文件夹,它们位于放置数据库的同一路径下。

    有人能告诉我是不是用了错误的方法,还是需要配置,或者我可能遗漏了什么吗。任何帮助都将不胜感激。

    非常感谢。 蜂蜜

    1 回复  |  直到 6 年前
        1
  •  1
  •   Sergey S.    10 年前

    TransferSpreadsheet 不适合导出到预先格式化的模板。使用 CopyFromRecordset 方法。下面的示例将转储的内容 OutputTable

    Dim xlApp As Object
    Dim xlWork As Object
    Dim xlSheet As Object
    Dim rsExportResults As Recordset
    
    Set rsExportResults = CurrentDb.OpenRecordset("OutputTable")
    
    Set xlApp = CreateObject("Excel.Application")
    xlApp.Visible = True
    Set xlWork = xlApp.Workbooks.Open(DestinFile)
    Set xlSheet = xlWork.Sheets("Result")
    
    xlSheet.Range("B2").CopyFromRecordset rsExportResults