繁体   English   中英

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

[英]SSIS package on server cannot export to Excel

我已经创建了一个SSIS包,可以从我的BIDS上很好地工作。 但是从服务器执行时失败。

该软件包执行以下操作:

  • 将数据(从SQL数据库读取)插入Excel文件
  • 通过VB脚本,它打开Excel文件并删除第2行

我可以看到该程序包可以在Excel文件中正确插入数据。 在名为“删除第二行”的步骤中执行VB脚本时,它失败:

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 Server 2012-11.0上执行。

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

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

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

有什么原因我们不能使用常规的Excel Connection Manager插入数据,而不是删除第2行,而从不发送数据? 使用条件拆分将其虹吸。 这是一个简单的例子 这是使用SSIS默认Excel连接管理器的高级示例 我也想反对在脚本任务中使用Interop.Excel。 它肯定会导致这些类型的“但是,它在我的机器上工作”方案。 阅读KB257757,以了解Microsoft为什么不鼓励此类事情。 但是,只有Interop.Excel可以执行某些操作,例如保存为PDF或运行宏。 您没有做任何一件事情,那么为什么不尝试使用普通的Excel连接器呢?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM