使用SSIS运行Excelmacros

我有一个运行一系列SSRS报告的SSIS包,并将它们作为.xlsx文件保存在一个文件夹中。 我也有一个Excel文件,通过保存每个.xlsx文件的macros,合并成一个文件,格式并保存在另一个文件夹。

现在这是一个两步的过程,所以我的目标是只做一步。 要做到这一点,我试图添加一个脚本任务到我的SSIS包的末尾打开我的Excel文件与macros并运行它。

我在网上寻找解决scheme,发现很多似乎为人们工作,但不为我工作。 具体来说,我正在使用这个网站的代码。

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

在这里输入图像说明

我得到的错误是:

使用No-PIA模式链接组件时,不允许引用类“ApplicationClass”。

我发现这个网站似乎有人有类似的问题,但它并没有帮助我与我的。

有什么我在脚本的地方做错了吗? 请参阅下面的脚本本身。

 ' 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 

任何帮助/build议,非常感谢!