服务器上的 SSIS 包无法导出到 Excel

SSIS package on server cannot export to Excel

我已经创建了一个 SSIS 包,它可以在我的 BIDS 中完美运行。但是从服务器执行时失败了。

该包执行以下操作:

我可以看到包可以在Excel文件中正确插入数据。在名为 "Remove 2nd line":

的步骤中执行 VB-Script 时失败
Remove 2ndline: Error: Exception has been thrown by the target of an invocation.

脚本是:

    #Region "Help:  Introduction to the script task"
    'The Script Task allows you to perform virtually any operation that can be accomplished in
    'a .Net application within the context of an Integration Services control flow. 

    'Expand the other regions which have "Help" prefixes for examples of specific ways to use
    'Integration Services features within this script task.
    #End Region
    Option Strict Off

    #Region "Imports"
    Imports System
    Imports System.Data
    Imports System.Math
    Imports Microsoft.SqlServer.Dts.Runtime
    Imports Microsoft.Office.Interop.Excel

    #End Region

    'ScriptMain is the entry point class of the script.  Do not change the name, attributes,
    'or parent of this class.
    <Microsoft.SqlServer.Dts.Tasks.ScriptTask.SSISScriptTaskEntryPointAttribute()> _
    <System.CLSCompliantAttribute(False)> _
    Partial Public Class ScriptMain
        Inherits Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase

        Private _app As Microsoft.Office.Interop.Excel.Application
        Private _books As Microsoft.Office.Interop.Excel.Workbooks
        Private _book As Microsoft.Office.Interop.Excel.Workbook
        Protected _sheets As Microsoft.Office.Interop.Excel.Sheets
        Protected _sheet1 As Microsoft.Office.Interop.Excel.Worksheet


    #Region "Help:  Using Integration Services variables and parameters in a script"
        'To use a variable in this script, first ensure that the variable has been added to 
        'either the list contained in the ReadOnlyVariables property or the list contained in 
        'the ReadWriteVariables property of this script task, according to whether or not your
        'code needs to write to the variable.  To add the variable, save this script, close this instance of
        'Visual Studio, and update the ReadOnlyVariables and 
        'ReadWriteVariables properties in the Script Transformation Editor window.
        'To use a parameter in this script, follow the same steps. Parameters are always read-only.

        'Example of reading from a variable:
        ' startTime = Dts.Variables("System::StartTime").Value

        'Example of writing to a variable:
        ' Dts.Variables("User::myStringVariable").Value = "new value"

        'Example of reading from a package parameter:
        ' batchId = Dts.Variables("$Package::batchId").Value

        'Example of reading from a project parameter:
        ' batchId = Dts.Variables("$Project::batchId").Value

        'Example of reading from a sensitive project parameter:
        ' batchId = Dts.Variables("$Project::batchId").GetSensitiveValue()
    #End Region

    #Region "Help:  Firing Integration Services events from a script"
        'This script task can fire events for logging purposes.

        'Example of firing an error event:
        ' Dts.Events.FireError(18, "Process Values", "Bad value", "", 0)

        'Example of firing an information event:
        ' Dts.Events.FireInformation(3, "Process Values", "Processing has started", "", 0, fireAgain)

        'Example of firing a warning event:
        ' Dts.Events.FireWarning(14, "Process Values", "No values received for input", "", 0)
    #End Region

    #Region "Help:  Using Integration Services connection managers in a script"
        'Some types of connection managers can be used in this script task.  See the topic 
        '"Working with Connection Managers Programatically" for details.

        'Example of using an ADO.Net connection manager:
        ' Dim rawConnection As Object = Dts.Connections("Sales DB").AcquireConnection(Dts.Transaction)
        ' Dim myADONETConnection As SqlConnection = CType(rawConnection, SqlConnection)
        ' <Use the connection in some code here, then release the connection>
        ' Dts.Connections("Sales DB").ReleaseConnection(rawConnection)

        'Example of using a File connection manager
        ' Dim rawConnection As Object = Dts.Connections("Prices.zip").AcquireConnection(Dts.Transaction)
        ' Dim filePath As String = CType(rawConnection, String)
        ' <Use the connection in some code here, then release the connection>
        ' Dts.Connections("Prices.zip").ReleaseConnection(rawConnection)
    #End Region

        'This method is called when this script task executes in the control flow.
        'Before returning from this method, set the value of Dts.TaskResult to indicate success or failure.
        'To open Help, press F1.

        Public Sub Main()

            Try
                Dim FileName As String = ""
                If Dts.Variables.Contains("DestinationPath") = True Then
                    FileName = CType(Dts.Variables("DestinationPath").Value, String)
                End If

                OpenExcelWorkbook(FileName)

                _sheet1 = CType(_sheets(1), Microsoft.Office.Interop.Excel.Worksheet)
                _sheet1.Activate()

                Dim range As Microsoft.Office.Interop.Excel.Range = _sheet1.Rows(2)
                range.Delete(Microsoft.Office.Interop.Excel.XlDeleteShiftDirection.xlShiftUp)
                DoRelease(range)
                DoRelease(_sheet1)
                CloseExcelWorkbook()
                DoRelease(_book)
                _app.Quit()
                DoRelease(_app)
                Dts.TaskResult = ScriptResults.Success
            Catch ex As Exception

                MsgBox(ex.ToString())
            End Try


        End Sub
        Protected Sub OpenExcelWorkbook(ByVal fileName As String)
            'try
            '{
            _app = New Microsoft.Office.Interop.Excel.Application()
            If _book Is Nothing Then
                _books = _app.Workbooks
                _book = _books.Open(fileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing)
                _sheets = _book.Worksheets
            End If
            '}
            'catch(Exception ex)
            '{
            ' Console.WriteLine(ex.ToString());
            '}

        End Sub
        Protected Sub CloseExcelWorkbook()
            _book.Save()
            _book.Close(False, Type.Missing, Type.Missing)
        End Sub
        Protected Sub DoRelease(ByVal o As Object)
            Try
                If Not o Is Nothing Then
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(o)
                End If
            Finally
                o = Nothing
            End Try
        End Sub

    #Region "ScriptResults declaration"
        'This enum provides a convenient shorthand within the scope of this class for setting the
        'result of the script.

        'This code was generated automatically.
        Enum ScriptResults
            Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success
            Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
        End Enum

    #End Region

    End Class

变量 "DestinationPath" 到达 Excel 文件。此路径在脚本外部有效(即它可以在上一步中将数据填充到文件中)。它在脚本中的 BIDS 中也有效。我还确保将其作为 ReadOnlyVariable 提供给脚本。所以我非常有信心这不是变量的问题。

我的服务器是 Windows 2012 R2(64 位)

包正在 SQL 服务器 2012 - 11.0 上执行。

我已经在服务器上安装了Excel 2013。

我创建了以下文件夹,因为这也可能导致问题(我已阅读):C:\Windows\SysWOW64\config\systemprofile\Desktop

我的脚本的调用者是我的管理员用户 ID - 这个用户 ID 能够在服务器上使用 Excel。

我们有什么理由不能使用常规 Excel 连接管理器来插入数据,而不是删除第 2 行,只是从不发送它?使用条件拆分将其吸走。这里有一个 simple example. Here is an advanced example of using SSIS default Excel Connection manager. I'd also like to grant a pearl of wisdom against using Interop.Excel inside of script tasks. It can certainly lead to these types of "But, it works on my Machine" scenarios. Read KB257757 来了解为什么 Microsoft 不鼓励这种类型的事情。但是,有些事情只有 Interop.Excel 可以做,例如保存为 PDF 或 运行 宏。您没有做任何一件事情,那么为什么不试试常规的 Excel 连接器呢?