SSIS C# 脚本任务打开 Excel 会导致调用错误



我有一个SSIS package,它有一个脚本任务,从Excel读取。 我收到以下错误,似乎找不到问题。

调用目标已引发异常。

atSystem.RuntimeMethodHandle.InvokeMethod(Object target, Object[]
arguments, Signature sig, Boolean constructor) at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, 对象[] 参数,对象[] 参数) at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] 参数, CultureInfo culture) at System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] 提供Args, ParameterModifier[] 修饰符, CultureInfo culture, String[] 命名参数) 在 Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTATaskScriptingEngine.ExecuteScript()

我已将问题缩小到这 2 行,我正在设置Excel应用程序。

Excel.Application xlApp;
xlApp = new Excel.ApplicationClass();

似乎是xlApp值的设置导致了错误。

我正在使用Visual Studio 2017。

谁能帮我解决这个问题?

调用的目标已引发异常。

是发生错误时由脚本任务引发的常规异常

读取真正的错误消息

要阅读主要错误消息,您可以在代码中添加一个 try catch 子句,并使用 Dts.FireError() 方法抛出真正的异常。

public void Main()
{
try{
Excel.Application xlApp;
xlApp = new Excel.ApplicationClass();
//...rest of code here
Dts.TaskResult = (int)ScriptResult.Success;
}catch(Exception ex){
Dts.FireError(0,"An error occured", ex.Message,String.Empty, 0);
Dts.TaskResult = (int)ScriptResult.Failure;
}

}

试图找出问题所在

  1. 尝试使用Excel.Application而不是ApplicationClass

    Excel.Application xlApp = new Excel.Application();
    
  2. 确保计算机上安装了Microsoft Excel,并且互操作程序集已在 GAC 中注册

最新更新