繁体   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