0

我正在尝试格式化 Excel 2010 文档并且文件创建良好并且数据在那里,但是当脚本任务去格式化文件时它被损坏。我还注意到它只会读取 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

这在我的机器和试验环境上运行良好。当它投入生产时,事情会迅速走下坡路。关于我所缺少的任何想法都会很棒。

谢谢

4

2 回答 2

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 发生了什么。谢谢

于 2013-04-18T17:03:17.393 回答
0

转换器已安装,并且必须重新启动,因为本周末服务器重新启动并且一切似乎都在工作。所以我认为我们现在很好。

于 2013-04-22T16:44:15.710 回答