Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 服务器上的SSIS包无法导出到Excel_Sql Server_Excel_Ssis - Fatal编程技术网

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

Sql server 服务器上的SSIS包无法导出到Excel,sql-server,excel,ssis,Sql Server,Excel,Ssis,我已经创建了一个SSIS包,从我的出价完美地工作。但从服务器执行时失败 该包执行以下操作: 将数据(从SQL数据库读取)插入Excel文件 通过VB脚本打开Excel文件并删除第2行 我可以看到该软件包可以在Excel文件中正确插入数据。在名为“删除第二行”的步骤中执行VB脚本时失败: 剧本是: #Region "Help: Introduction to the script task" 'The Script Task allows you to perform vir

我已经创建了一个SSIS包,从我的出价完美地工作。但从服务器执行时失败

该包执行以下操作:

  • 将数据(从SQL数据库读取)插入Excel文件
  • 通过VB脚本打开Excel文件并删除第2行
我可以看到该软件包可以在Excel文件中正确插入数据。在名为“删除第二行”的步骤中执行VB脚本时失败:

剧本是:

    #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
#地区“帮助:脚本任务简介”
'脚本任务允许您执行几乎任何可以在中完成的操作
'集成服务控制流上下文中的.Net应用程序。
'展开具有“Help”前缀的其他区域,以获取使用特定方法的示例
'此脚本任务中的集成服务功能。
#末端区域
选择严格关闭
#区域“进口”
导入系统
导入系统数据
导入系统。数学
导入Microsoft.SqlServer.Dts.Runtime
导入Microsoft.Office.Interop.Excel
#末端区域
'ScriptMain是脚本的入口点类。不要更改名称、属性、,
'或该类的父级。
_
_
部分公共类ScriptMain
继承Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
私有应用程序,如Microsoft.Office.Interop.Excel.Application
私有书籍,如Microsoft.Office.Interop.Excel.Workbooks
专用手册,如Microsoft.Office.Interop.Excel.Workbook
受保护的工作表,如Microsoft.Office.Interop.Excel.sheets
受保护的表1为Microsoft.Office.Interop.Excel.Worksheet
#区域“帮助:在脚本中使用Integration Services变量和参数”
'要在此脚本中使用变量,请首先确保已将该变量添加到
'ReadOnlyVariables属性中包含的列表或
'此脚本任务的ReadWriteVariables属性,具体取决于
'代码需要写入变量。若要添加变量,请保存此脚本,然后关闭的此实例
,并更新ReadOnlyVariables和
'脚本转换编辑器窗口中的ReadWriteVariables属性。
'要在此脚本中使用参数,请执行相同的步骤。参数总是只读的。
'从变量读取的示例:
'startTime=Dts.Variables(“System::startTime”).Value
'写入变量的示例:
'Dts.Variables(“用户::myStringVariable”).Value=“新值”
'从包参数读取的示例:
'batchId=Dts.Variables(“$Package::batchId”).Value
'从项目参数读取的示例:
'batchId=Dts.Variables(“$Project::batchId”).Value
'从敏感项目参数读取的示例:
'batchId=Dts.Variables(“$Project::batchId”).GetSensitiveValue()
#末端区域
#区域“帮助:从脚本触发Integration Services事件”
'此脚本任务可以出于日志记录目的触发事件。
'触发错误事件的示例:
'Dts.Events.FireError(18,“进程值”、“错误值”、“0”)
'触发信息事件的示例:
'Dts.Events.FireInformation(3,“进程值”,“处理已开始”,“0,再次启动)”
'触发警告事件的示例:
'Dts.Events.FireWarning(14,“进程值”,“未收到输入值”,“0”)
#末端区域
#区域“帮助:在脚本中使用Integration Services连接管理器”
'此脚本任务中可以使用某些类型的连接管理器。参见主题
“以编程方式使用连接管理器”了解详细信息。
'使用ADO.Net连接管理器的示例:
'Dim rawConnection As Object=Dts.Connections(“Sales DB”).AcquireConnection(Dts.Transaction)
'Dim myADONETConnection As SqlConnection=CType(rawConnection,SqlConnection)
' 
'Dts.Connections(“销售数据库”).ReleaseConnection(rawConnection)
'使用文件连接管理器的示例
'Dim rawConnection As Object=Dts.Connections(“Prices.zip”).AcquireConnection(Dts.Transaction)
'Dim filePath As String=CType(原始连接,字符串)
' 
'Dts.Connections(“Prices.zip”).ReleaseConnection(rawConnection)
#末端区域
'在控制流中执行此脚本任务时调用此方法。
'从该方法返回之前,请将Dts.TaskResult的值设置为指示成功或失败。
'要打开帮助,请按F1。
公用分干管()
尝试
Dim FileName As String=“”
如果Dts.Variables.Contains(“DestinationPath”)=True,则
FileName=CType(Dts.Variables(“DestinationPath”)。值,字符串)
如果结束
OpenExcelWorkbook(文件名)
_sheet1=CType(_sheets(1),Microsoft.Office.Interop.Excel.Worksheet)
_表1.激活()
尺寸范围为Microsoft.Office.Interop.Excel.range=\u sheet1.行(2)
range.Delete(Microsoft.Office.Interop.Excel.XlDeleteShiftDirection.xlShiftUp)
多雷莱斯(射程)
DoRelease(图1)
CloseExcelWorkbook()
DoRelease(_book)
_app.Quit()
DoRelease(_应用程序)
Dts.TaskResult=ScriptResults.Success
特例
MsgBox(例如ToString())
结束尝试
端接头
受保护的子OpenExcelWorkbook(ByVal文件名为字符串)
“试试看
'{
_app=新的Microsoft.Office.Interop.Excel.Application()
如果你的书什么都不是
_书籍=\u应用程序工作簿
_书。
    #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