我正在尝试格式化Excel 2010 doc并且文件创建正常并且数据存在,但是当脚本任务去格式化文件时它会被破坏。我也注意到它只会读取2个工作表中的第一个。这是代码:
Dim lintLastRow As Integer
Dim lstrLastCol, lstrHeader As String
Dim mobjWorkbook As Workbook
Dim mappExcel As New Microsoft.Office.Interop.Excel.Application
Dim lobjWorksheet As Worksheet
Dim emptyBytes(0) As Byte
Dim oldCI As System.Globalization.CultureInfo = System.Threading.Thread.CurrentThread.CurrentCulture
System.Threading.Thread.CurrentThread.CurrentCulture = New System.Globalization.CultureInfo("en-US")
Try
mappExcel.Visible = False
mappExcel.DisplayAlerts = False
mappExcel.AskToUpdateLinks = False
mappExcel.AlertBeforeOverwriting = False
mobjWorkbook = mappExcel.Workbooks.Open(Dts.Variables("FilePath").Value + Dts.Variables("FileName").Value)
mobjWorkbook.RefreshAll()
Dts.Log("worksheet count: " & mobjWorkbook.Sheets.Count, 0, emptyBytes)
For Each lobjWorksheet In mobjWorkbook.Sheets
If lobjWorksheet.Name = "Compare" Then
lstrLastCol = "T"
lstrHeader = "- Compare Results"
Else
lstrLastCol = "H"
lstrHeader = "- Trans UnMatch"
End If
'Dts.Log("Set worksheet: " & lintCount, 0, emptyBytes)
'lobjWorksheet = mobjWorkbook.Sheets.Item(lintCount)
Dts.Log("worksheet select", 0, emptyBytes)
lobjWorksheet.Select()
lobjWorksheet.Range("A1").Select()
If lobjWorksheet.Cells(2, 1).value = Nothing Then
lintLastRow = 4
Else
lobjWorksheet.Application.Cells.End(XlDirection.xlDown).Select()
lintLastRow = lobjWorksheet.Application.ActiveCell.Row + 2
End If
Dts.Log("last row: " & lintLastRow, 0, emptyBytes)
lobjWorksheet.Rows(1).Insert(XlInsertShiftDirection.xlShiftDown, False)
lobjWorksheet.Rows(1).Insert(XlInsertShiftDirection.xlShiftDown, False)
lobjWorksheet.Cells(1, 1).value = "PAM - GL Portfolio Level " + lstrHeader + " for " + Today
lobjWorksheet.Range("A1", lstrLastCol + "1").Merge()
lobjWorksheet.Range("A1").RowHeight = 27
lobjWorksheet.Range("A1", lstrLastCol + "3").Font.Bold = True
lobjWorksheet.Range("C4", lstrLastCol & lintLastRow).NumberFormat = "0.00" ' = FormatNumber("#####.00").
lobjWorksheet.Range("A3", lstrLastCol & lintLastRow).EntireColumn.AutoFit()
lobjWorksheet.Range("A1", lstrLastCol & lintLastRow).Font.Name = "Arial"
lobjWorksheet.Range("A1").Select()
Dts.Log("Complete Update", 0, emptyBytes)
Next
mobjWorkbook.Sheets("Compare").Select()
Dts.TaskResult = ScriptResults.Success
Catch ex As Exception
Dts.Events.FireError(0, "Format Excel Failed: ", _
ex.Message & ControlChars.CrLf & ex.StackTrace, _
String.Empty, 0)
Dts.TaskResult = ScriptResults.Failure
Finally
System.Threading.Thread.CurrentThread.CurrentCulture = oldCI
mobjWorkbook.Save()
mobjWorkbook.Close()
ReleaseCom(lobjWorksheet)
ReleaseCom(mobjWorkbook)
mappExcel.Quit()
ReleaseCom(mappExcel)
GC.Collect()
GC.WaitForPendingFinalizers()
End Try
这在我的机器和试验环境中工作正常。当它投入生产时,事情会迅速下滑。对我所缺少的任何想法都会很棒。
由于
答案 0 :(得分:0)
所以我认为原始问题是由于Excel转换器没有安装在服务器上引起的。安装完成后,我开始收到以下错误:
OnError,PGIDSMSQL045,PRINCIPALUSA\TWSPGIAC,Formate Excel,{610A1615-E16E-4B62-8A93-F692A478FCF9},{EF8F3734-6467-423C-A9D3-BF2463EEFD44},04/18/2013 11:52:09 AM,04/18/2013 11:52:09 AM,0,0x,The server threw an exception. (Exception from HRESULT: 0x80010105 (RPC_E_SERVERFAULT)) 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_0391e3bc80224a34819c14be208559c5.vbproj.ScriptMain.Main()
OnError,PGIDSMSQL045,PRINCIPALUSA\TWSPGIAC,PAM-GLPortLvlRecon,{008D2838-612E-4DBB-AEE7-65145ED0BAAC},{EF8F3734-6467-423C-A9D3-BF2463EEFD44},04/18/2013 11:52:09 AM,04/18/2013 11:52:09 AM,0,0x,The server threw an exception. (Exception from HRESULT: 0x80010105 (RPC_E_SERVERFAULT))
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_0391e3bc80224a34819c14be208559c5.vbproj.ScriptMain.Main()
OnError,PGIDSMSQL045,PRINCIPALUSA\TWSPGIAC,Formate Excel,{610A1615-E16E-4B62-8A93-F692A478FCF9},{EF8F3734-6467-423C-A9D3-BF2463EEFD44},04/18/2013 11:52:09 AM,04/18/2013 11:52:09 AM,8,0x,The script returned a failure result.
OnError,PGIDSMSQL045,PRINCIPALUSA\TWSPGIAC,PAM-GLPortLvlRecon,{008D2838-612E-4DBB-AEE7-65145ED0BAAC},{EF8F3734-6467-423C-A9D3-BF2463EEFD44},04/18/2013 11:52:09 AM,04/18/2013 11:52:09 AM,8,0x,The script returned a failure result.
OnError,PGIDSMSQL045,PRINCIPALUSA\TWSPGIAC,Formate Excel,{610A1615-E16E-4B62-8A93-F692A478FCF9},{EF8F3734-6467-423C-A9D3-BF2463EEFD44},04/18/2013 11:52:09 AM,04/18/2013 11:52:09 AM,1,0x,System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.NullReferenceException: Object reference not set to an instance of an object.
at ST_0391e3bc80224a34819c14be208559c5.vbproj.ScriptMain.Main()
--- End of inner exception stack trace ---
at System.RuntimeMethodHandle._InvokeMethodFast(Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
at System.RuntimeMethodHandle.InvokeMethodFast(Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] 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 System.Type.InvokeMember(String name, BindingFlags invokeAttr, Binder binder, Object target, Object[] args, CultureInfo culture)
at Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTATaskScriptingEngine.ExecuteScript()
OnError,PGIDSMSQL045,PRINCIPALUSA\TWSPGIAC,PAM-GLPortLvlRecon,{008D2838-612E-4DBB-AEE7-65145ED0BAAC},{EF8F3734-6467-423C-A9D3-BF2463EEFD44},04/18/2013 11:52:09 AM,04/18/2013 11:52:09 AM,1,0x,System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.NullReferenceException: Object reference not set to an instance of an object.
at ST_0391e3bc80224a34819c14be208559c5.vbproj.ScriptMain.Main()
--- End of inner exception stack trace ---
at System.RuntimeMethodHandle._InvokeMethodFast(Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
at System.RuntimeMethodHandle.InvokeMethodFast(Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] 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 System.Type.InvokeMember(String name, BindingFlags invokeAttr, Binder binder, Object target, Object[] args, CultureInfo culture)
at Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTATaskScriptingEngine.ExecuteScript()
这比我想要的更有趣......但是一个很好的挑战。 我检查了变量,他们有正确的信息。用户具有对输出目录的完全访问权限,因为它在之前的步骤中创建了该文件。所以我不确定我错过了什么。注意:整个过程在格式化.xls文件时有效,所以我不确定.xlsx是怎么回事。 感谢
答案 1 :(得分:0)
转换器已安装,必须重新启动,因为本周末服务器已重启,一切似乎都在运行。所以我认为我们现在很好。