0

データを取得して一般的なリストに保存した後、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;
    . . .
}

「 System.Runtime.InteropServices.COMException was unhandled ... StackTrace: at System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, 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 が処理されませんでした。 、MessageData& msgData) の Microsoft.Office.Interop.Excel.Range.get__Default(Object RowIndex, Object ColumnIndex) at Pivotal.FormMain.AddData(PriceVarianceData _pvd) in c:\Projects\Pivotal\Pivotal\Form1.cs:line 155 at c:\Projects\Pivotal\Pivotal\Form1.cs の Pivotal.FormMain.GenerateAndSaveSpreadsheetFile(): c:\Projects\Pivotal\Pivotal\Form1.cs の Pivotal.FormMain.buttonRun_Click(オブジェクト送信者、EventArgs e) の 134 行目: System.Windows.Forms.Control.OnClick(EventArgs e) の 77 行目。Windows.Forms.Button.OnClick(EventArgs e) で System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) で System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons ボタン, Int32 クリック) で System.Windows. Forms.Control.WndProc(Message& m) で System.Windows.Forms.ButtonBase.WndProc(Message& m) で System.Windows.Forms.Button.WndProc(Message& m) で System.Windows.Forms.Control.ControlNativeWindow.OnMessage( Message& m) System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) で System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) で System.Windows.Forms.UnsafeNativeMethods System.Windows.Forms.Application.ComponentManager.System.Windows.Forms の .DispatchMessageW(MSG& msg)。UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData) at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, System.Windows.Forms.Application.Run(Form mainForm) at Pivotal.Program.Main() in c:\Projects\Pivotal\Pivotal\Program.cs:line 19 at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly) 、System.AppDomain.ExecuteAssembly の String[] args) (String assemblyFile、Evidence assemblySecurity、String[] args) at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() at System.Threading.ThreadHelper.ThreadStart_Context(Object state) at System .スレッド。System.Threading.ExecutionContext.Run(ExecutionContext 実行コンテキスト、ContextCallback コールバック、オブジェクト状態、ブール値 preserveSyncCtx) での ExecutionContext.RunInternal(ExecutionContext 実行コンテキスト、ContextCallback コールバック、オブジェクト状態、ブール値 preserveSyncCtx)コールバック、オブジェクト状態) で System.Threading.ThreadHelper.ThreadStart() InnerException:オブジェクトの状態) System.Threading.ThreadHelper.ThreadStart() InnerException:オブジェクトの状態) System.Threading.ThreadHelper.ThreadStart() InnerException:

ここで何が問題になる可能性がありますか? thisを見ましたが、この状況に当てはまる提案はないようでした。

アップデート

私は今朝、約 40 個の Excel プロセスが実行されていることに気付きました。それでも同じ結果が得られましたが、その Excel プロセスもハングアップしました (予想どおり、実行が突然停止したため)。

4

1 に答える 1