我正在尝试在SSIS中运行VBA脚本,它打开一个excel文件并将其保存为1997-2003 excel文件,但每次执行时都会收到运行时错误。我一直在为我的脚本使用这两个答案的组合:1和2,我想出了这个:
Public Sub Main()
Dim oExcel As Excel.Application = Nothing
Dim oBooks As Excel.Workbooks = Nothing
Dim oBook As Excel.Workbook = Nothing
'
oExcel = CreateObject("Excel.Application")
oExcel.Visible = False
oBooks = oExcel.Workbooks
oBook = oBooks.Open(Dts.Variables("Filename.xls").Value.ToString())
oBook.SaveAs("Filename.xls")
oExcel.Quit
Dts.TaskResult = ScriptResults.Success
End Sub
我引用了Microsoft Excel 16.0对象库和C#,有人可以帮忙吗?
这是我得到的错误:
DTS脚本任务:运行时错误
调用目标抛出了异常。
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
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()
答案 0 :(得分:0)
这是我最终使用的。它在Excel中打开文件并以Unicode文本格式保存。另外,请务必转换“嵌入互操作类型”。为假。
Public Sub Main()
Dim oMissing As Object = System.Reflection.Missing.Value
Dim xl As New Microsoft.Office.Interop.Excel.ApplicationClass()
Dim xlBook As Microsoft.Office.Interop.Excel.Workbook
Dim xlSheet As Microsoft.Office.Interop.Excel.Worksheet
Dim laPath As String = "C:\Filename.xls"
xlBook = DirectCast(xl.Workbooks.Open(laPath, oMissing, oMissing, oMissing, oMissing, oMissing,
oMissing, oMissing, oMissing, oMissing, oMissing, oMissing,
oMissing, oMissing, oMissing), Workbook)
xlSheet = DirectCast(xlBook.Worksheets.Item(1), Worksheet)
xlBook.SaveAs("C:\Filename.txt", XlFileFormat.xlUnicodeText, oMissing, oMissing, oMissing, oMissing,, oMissing, oMissing, oMissing, oMissing, oMissing)
xl.Application.Workbooks.Close()
Dts.TaskResult = ScriptResults.Success
End Sub