从 Access 中提取的数据 Excel 会自动转移到比定义的 sheet 额外的 sheet

Data extracted from Acces to Excel is automatically getting over to extra sheet than the defined sheet

我正在尝试从 MS Access 中提取一些数据到 Excel sheet,这是一种已经定义的模板。例如,我使用一个 excel 文件,其中一个 sheet 名为 Result(带有一些预定义数据)作为源文件,然后我复制相同的文件到输出文件。然后使用 DoCmd.TransferSpreadsheet 在输出文件中提取来自 MS Access 的一些 OutputTable,并明确提到要在结果 sheet 中提取。数据正常,但未使用 sheet 结果,尽管使用与 Ms Access 的 OutputTable 相同的数据创建了一个名为 Result1 的额外 sheet。

我使用的代码如下:

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

我在我的系统上创建了这些文件夹模板(具有 Input_Template.xlsx 文件)和输出文件夹,位于数据库所在的相同路径下。

任何人都可以告诉我是否以错误的方式进行操作,或者是否需要任何配置,或者我可能遗漏了什么。任何帮助将不胜感激。

谢谢!!! 亲爱的

TransferSpreadsheet 不适合导出到预先格式化的模板。请改用 Excel 库中的 CopyFromRecordset 方法。下面的示例会将 OutputTable 的内容转储到目标模板文件的 "Result" 电子表格,开始于 "B2" 单元格。

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