簡體   English   中英

為什么使用此代碼會得到“ InteropServices.COMException / ForwardCallToInvokeMember”?

[英]Why would I get, “InteropServices.COMException / ForwardCallToInvokeMember” with this code?

檢索數據並將其存儲在通用列表中之后,我得到了以下代碼,以使用Excel Interop代碼填充Excel電子表格:

. . .
InitializeExcelObjects();
_currentTopRow = DATA_STARTING_ROW;
foreach (PriceVarianceData _pvd in _pvdList)
{
    AddData(_pvd);
    _currentTopRow = _currentTopRow + 1;
}
. . .

private void AddData(PriceVarianceData _pvd)
{
    var UnitCell = (Excel.Range)_xlSheet.Cells[_currentTopRow, 0];
    UnitCell.Value2 = _pvd.Unit;
    var ShortNameCell = (Excel.Range)_xlSheet.Cells[_currentTopRow, 1];
    ShortNameCell.Value2 = _pvd.ShortName;
    . . .
}

它在AddData()方法的第一行(嘗試分配給UnitCell的文件)上崩潰, 未處理System.Runtime.InteropServices.COMException ... StackTrace:位於System.RuntimeType.ForwardCallToInvokeMember(String memberName,BindingFlags標志,Object ...

我不知道為什么會這樣。 在嘗試添加數據之前,我先打電話給我:

private void InitializeExcelObjects()
{
    _xlApp = new Excel.Application
    {
        SheetsInNewWorkbook = 1,
        StandardFont = "Calibri",
        StandardFontSize = 11
    };
    Thread.Sleep(2000);
    //var apartmentSt8 = Thread.CurrentThread.GetApartmentState(); <= STA, as it should be

    _xlBook = _xlApp.Workbooks.Add(Type.Missing);
    _xlSheets = _xlBook.Worksheets;
    _xlSheet = (Excel.Worksheet)_xlSheets.Item[1];
}

完整的例外是:

未處理System.Runtime.InteropServices.COMException HResult = -2146827284消息=來自HRESULT的異常:0x800A03EC Source =“” ErrorCode = -2146827284 StackTrace:位於System.RuntimeType.ForwardCallToInvokeMember(String memberName,BindingFlags標志,對象目標,Int32 [] aWrapperTypes ,Microsoft.Office.Interop.Excel.Range.get__Default(Object RowIndex,Object ColumnIndex)處的MessageData和msgData)在c:\\ Projects \\ Pivotal \\ Pivotal \\ Form1.cs:line 155處的C:\\ Projects.Pivotal \\ Pivotal \\ Form1.cs:line c:\\ Projects \\ Pivotal \\ Pivotal \\ Form1.cs中的Pivotal.FormMain.GenerateAndSaveSpreadsheetFile():c:\\ Projects \\ Pivotal \\ Pivotal \\ Form1.cs中Pivotal.FormMain.buttonRun_Click(Object sender,EventArgs e)的第134行: System.Windows.Forms.Button.OnClick(EventArgs e)在System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)在System.Windows.Forms的第77行.System.Windows.Forms.C中的.Control.WmMouseUp(Message&m,MouseButtons button,Int32 clicks) System.Windows.Forms.ButtonBase上的ontrol.WndProc(Message&m).System.Windows.Forms.Button.WndProc(Message&m)在System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&m) )在System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,Int32 msg,IntPtr wparam,IntPtr lparam)在System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW處的System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&m) (MSG和msg),位於System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32原因,在System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32原因,ApplicationContext上下文)在System.Windows.Forms.Application.Run(Form mainForm)在Pivotal.Program.Main()在c:\\ Projects \\ Pivotal \\ Pivotal \\ Program.cs:S的第19行 ystem.AppDomain._nExecuteAssembly(RuntimeAssembly程序集,String [] args)在System.AppDomain.ExecuteAssembly(String assemblyFile,證據AssemblySecurity,String [] args)在Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()在System.Threading.ThreadHelper System.Threading.ExecutionContext.Run(ExecutionContext執行上下文,ContextCallback回調,對象狀態,布爾型saveSyncCtx)在System.Threading.ExecutionContext.RunInternal(ExecutionContext執行上下文,ContextCallback回調,對象狀態,布爾值saveSyncCtx) System.Threading.ThreadHelper.ThreadStart()上的.Threading.ExecutionContext.Run(ExecutionContext executeContext,ContextCallback回調,對象狀態)InnerException:

這里可能是什么問題? 我看着這個 ,但沒有建議似乎適用於這種情況。

更新

我今天早上注意到我正在運行大約40個Excel進程,所以想知道這是否是問題所在(我想它們沒有被關閉/處置)並殺死了所有這些進程。 但是,我仍然得到相同的結果-而且Excel流程也仍然存在(可以預期,突然停止執行該怎么辦)。

在您的AddData()方法的第一行中,您嘗試使用基於0的索引訪問Cells ,但是excel中的列索引和行索引都是基於1的索引(實際上Excel中的許多索引都是1-基於-但並非全部)。

要解決此問題,您必須根據自己的情況做兩件事:

  1. 確保_currentTopRow是從1開始的索引(即最小有效值為1)
  2. 更改您的AddData()方法,如下所示:

     private void AddData(PriceVarianceData _pvd) { // Note changed column index (from 0 to 1) var UnitCell = (Excel.Range)_xlSheet.Cells[_currentTopRow, 1]; UnitCell.Value2 = _pvd.Unit; // Note changed column index (from 1 to 2) var ShortNameCell = (Excel.Range)_xlSheet.Cells[_currentTopRow, 2]; ShortNameCell.Value2 = _pvd.ShortName; . . . } 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM