使用SSIS运行Excel宏

时间:2016-03-31 13:02:30

标签: vb.net excel vba ssis

我有一个SSIS包,它运行一堆SSRS报告,并将它们作为.xlsx文件保存在文件夹中。我还有一个带有宏的Excel文件,该宏通过保存的每个.xlsx文件,合并到一个文件中,格式化并保存在另一个文件夹中。

现在这是一个两步过程,所以我的目标是让它只有一步。为此,我正在尝试将一个脚本任务添加到我的SSIS包的末尾,以使用宏打开我的Excel文件并运行它。

我已经在网上寻找解决方案,并发现许多似乎适用于人,但不适合我。具体来说,我正在使用this site中的代码。

现在,当我填充代码时,添加我对Microsoft Excel 15.0 Object Library的引用并将Imports Microsoft.Office.Interop添加到我的脚本顶部,我在某些代码上遇到错误。请看下面的截图:

enter image description here

我得到的错误是:

当使用No-PIA模式链接其程序集时,不允许引用类'ApplicationClass'。

我发现this site某人似乎有类似的问题,但这对我没有帮助。

在某个地方的脚本中我有什么问题吗?请参阅下面的脚本本身。

' Microsoft SQL Server Integration Services Script Task
' Write scripts using Microsoft Visual Basic 2008.
' The ScriptMain is the entry point class of the script.

Imports System
Imports System.Data
Imports System.Math
Imports Microsoft.SqlServer.Dts.Runtime
Imports Microsoft.Office.Interop

 _
 _
Partial Public Class ScriptMain
    Inherits Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase

    Enum ScriptResults
        Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success
        Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
    End Enum


    ' The execution engine calls this method when the task executes.
    ' To access the object model, use the Dts property. Connections, variables, events,
    ' and logging features are available as members of the Dts property as shown in the following examples.
    '
    ' To reference a variable, call Dts.Variables("MyCaseSensitiveVariableName").Value
    ' To post a log entry, call Dts.Log("This is my log text", 999, Nothing)
    ' To fire an event, call Dts.Events.FireInformation(99, "test", "hit the help message", "", 0, True)
    '
    ' To use the connections collection use something like the following:
    ' ConnectionManager cm = Dts.Connections.Add("OLEDB")
    ' cm.ConnectionString = "Data Source=localhost;Initial Catalog=AdventureWorks;Provider=SQLNCLI10;Integrated Security=SSPI;Auto Translate=False;"
    '
    ' Before returning from this method, set the value of Dts.TaskResult to indicate success or failure.
    ' 
    ' To open Help, press F1.


    Public Sub Main()



        Dim oExcel As Excel.ApplicationClass = Nothing
        Dim oBook As Excel.WorkbookClass = Nothing
        Dim oBooks As Excel.Workbooks = Nothing

        Try


            'Start Excel and open the workbook.
            oExcel = CreateObject("Excel.Application")
            oExcel.Visible = False
            oBooks = oExcel.Workbooks
            oBook = oBooks.Open(Dts.Variables("StrFilePath").Value.ToString()) ' Change your variable name here.


            'Run the macros.
            oExcel.Run("Format") ' Change the name of your Macro here.


            'Clean-up: Close the workbook and quit Excel.
            oBook.Save()
            oExcel.Quit()



            Dts.TaskResult = ScriptResults.Success

        Finally
            If oBook IsNot Nothing Then System.Runtime.InteropServices.Marshal.ReleaseComObject(oBook)
            If oBooks IsNot Nothing Then System.Runtime.InteropServices.Marshal.ReleaseComObject(oBooks)
            If oExcel IsNot Nothing Then System.Runtime.InteropServices.Marshal.ReleaseComObject(oExcel)


            oBook = Nothing
            oBooks = Nothing
            oExcel = Nothing

        End Try
    End Sub

End Class

非常感谢任何帮助/建议!

0 个答案:

没有答案