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

我已经创build了一个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 

Excel文件由variables“DestinationPath”访问。 这个path在脚本之外工作(即它可以在上一步中填充文件中的数据)。 它也适用于在脚本中的BIDS。 我也确保将它作为ReadOnlyVariable提供给脚本。 所以我很有信心,这不是一个variables的问题。

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

该包正在SQL Server 2012-11.0上执行。

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

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

我的脚本的调用者是我的pipe理员用户标识 – 而且这个用户标识能够在服务器上使用Excel。

是否有任何理由,我们不能使用常规的Excel连接pipe理器来插入数据,而不是删除第2行,只是从来没有发送它? 使用条件拆分将其虹吸掉。 这是一个简单的例子 。 以下是使用SSIS默认Excel连接pipe理器的高级示例 。 我也想授予一个智慧的明珠,以防止在脚本任务中使用Interop.Excel。 它肯定会导致这些types的“但是,它在我的机器上”的情况。 阅读KB257757 ,了解Microsoft为什么不鼓励这种types的东西。 但是,有些东西只有Interop.Excel才能做到,比如保存为PDF或运行macros。 你没有做这些事情之一,所以为什么不尝试常规的Excel连接器呢?