我正在尝试将一些样式应用到工作簿中的单元格。我想在后台线程中做到这一点,所以我的GUI可以保持响应。这项工作应该需要几秒钟,如果我点击文档中的一些随机单元格,我会得到一个异常。这里是我的代码:在后台运行时Excel互操作COM异常
public void ApplyStyles()
{
BackgroundWorker bw = new BackgroundWorker();
bw.DoWork += DoWork;
bw.RunWorkerAsync();
}
private void DoWork(object sender, DoWorkEventArgs e)
{
try
{
foreach (ICell xcell in cells)
{
Microsoft.Office.Interop.Excel.Range cell = cellUtility.GetCell(xcell);
if (styles.ContainsKey(styleIds[xcell.Style]))
{
Style s = styles[xcell.Style];
cell.Style = s;
}
}
}
catch (Exception ex)
{
if (Logger.IsErrorEnabled)
{
Logger.Error(ex.ToString());
}
messageBox.ShowErrorMessage(localizationMessages.ApplyingErrorText, localizationMessages.ApplyingErrorCaption);
}
}
当异常发生时,这是我收到的消息;
System.Runtime.InteropServices.COMException (0x800AC472): Exception from HRESULT: 0x800AC472
at System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData)
at Microsoft.Office.Interop.Excel.Range.set_Style(Object value)
at ABZ.ReportFactory.OfficeAddin.Excel.BatchLinking.BackgroundStyleApplier.DoWork() in C:\ABZ\ABZ ReportFactory Office Addin\ABZ.ReportFactory.OfficeAddin.Excel\BatchLinking\BackgroundStyleApplier.cs:line 86
是否有可能做这种风格在后台线程应用操作?我该怎么做?
我遇到的第二个问题是,当这种风格的应用程序在后台运行时,我的光标不断地从繁忙状态变为常规状态,直到此操作结束。我希望光标正常。该用户完全不知道这种背景操作。
欢呼声, 弗拉基米尔
http://www.myomron.com/index.php?action=kb&article=1324 – 2012-03-01 12:29:04
因此,Excel对象是可见的,而这是发生? – SeanCocteau 2012-03-01 12:38:09