gpt4 book ai didi

.net - 这个异常是什么?

转载 作者:行者123 更新时间:2023-12-02 01:44:33 32 4
gpt4 key购买 nike

我在用 C# 读取 Excel 工作表中的形状时遇到此异常:在代码行

if (worksheet.Shapes.Count >= iCurrentRowIndex)
{ }

Unable to cast COM object of type 'System.__ComObject' to interface type 'Microsoft.Office.Interop.Excel._Worksheet'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{000208D8-0000-0000-C000-000000000046}' failed due to the following error: The application called an interface that was marshalled for a different thread. (Exception from HRESULT: 0x8001010E (RPC_E_WRONG_THREAD)).

编辑:

这个应用程序在本地运行完美,但是当我将其部署到 IIS 上时,它会抛出异常。应该是什么原因呢?我将线程代码用作

string strImageSavedPath = string.Empty;
ThreadStart cbThreadStater = delegate { strImageSavedPath = CopyToClipBoard(worksheet, (int)iRowindex, strApplicationPath); };
Thread thrd = new Thread(cbThreadStater);
thrd.SetApartmentState(ApartmentState.STA);
thrd.Start();
thrd.Join();

其中CopyToClipBoard方法读取提供的行索引的图像,将图像保存在文件系统中并返回路径。

System.InvalidCastException was unhandled Message="Unable to cast COM object of type 'System.__ComObject' to interface type 'Microsoft.Office.Interop.Excel._Worksheet'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{000208D8-0000-0000-C000-000000000046}' failed due to the following error: The application called an interface that was marshalled for a different thread. (Exception from HRESULT: 0x8001010E (RPC_E_WRONG_THREAD))." Source="Microsoft.Office.Interop.Excel"
StackTrace: at Microsoft.Office.Interop.Excel._Worksheet.get_Shapes() at KGD6ExcelReader.ExcelManager.CopyToClipBoard(Worksheet worksheet, Int32 iRowindex, String strApplicationPath) in D:\ParallelMinds\Shared\KGD6ExcelReader\KGD6ExcelReader\ExcelManager.cs:line 522 at KGD6ExcelReader.ExcelManager.<>c__DisplayClass3.b__0() in D:\ParallelMinds\Shared\KGD6ExcelReader\KGD6ExcelReader\ExcelManager.cs:line 376 at System.Threading.ThreadHelper.ThreadStart_Context(Object state) at System.Threading.ExecutionContext.runTryCode(Object userData) at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData) at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart() InnerException:

最佳答案

不建议不支持使用 IIS 中的 Office 自动化对象。因此,您的应用程序在本地成功运行而在 IIS 上运行失败这一事实并不完全出乎意料。

Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.

这并不意味着您不能尝试,但在继续之前请确保您了解“Considerations for server-side Automation of Office ”支持文章中的所有内容。

关于.net - 这个异常是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2810906/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com