I am trying to use SSIS to open an Excel file and refresh the data before importing the data into SQL Server. My dtsx package is failing on the line of code that tries to open the Excel file.
The code is straightforward and taken from here.
This is the code I am using:
public void Main()
{
// TODO: Add your code here
Dts.TaskResult = (int)ScriptResults.Success;
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook wb;
wb = excel.Workbooks.Open(@"C:\Test.xlsx",0, false, null, null, null, true, null, null, null, null, null, null, null, null);
//wb.RefreshAll();
//wb.Save();
//wb.Close(null, null, null);
excel.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);
}
The error message is as follows:
Error: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Runtime.InteropServices.COMException (0x800A03EC): Exception from HRESULT: 0x800A03EC
at Microsoft.Office.Interop.Excel.Workbooks.Open(String Filename, Object UpdateLinks, Object ReadOnly, Object Format, Object Password, Object WriteResPassword, Object IgnoreReadOnlyRecommended, Object Origin, Object Delimiter, Object Editable, Object Notify, Object Converter, Object AddToMru, Object Local, Object CorruptLoad)
at ST_2177878595ee4288864728b04a894c16.csproj.ScriptMain.Main()
--- End of inner exception stack trace ---
at System.RuntimeMethodHandle._InvokeMethodFast(Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Obje开发者_如何学Goct[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams)
at Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTATaskScriptingEngine.ExecuteScript()
I am currently working on my test machine with the following configuration:
- Visual Studio 2008 BIDS
- Office 2010
- Office 2010 PIAs
Does anyone have any suggestions as to how to get this working?
Use Type.Missing
in place of null
. And also ref
before each variable. See http://msdn.microsoft.com/en-us/library/system.type.missing(v=VS.90).aspx
精彩评论