写入Excel单元格时如何正确处理COMException?

时间:2014-01-29 12:08:45

标签: c# excel interop

我有一个应用程序,它将值写入Excel工作表中的4个单元格之一。该片材大约为30×30个细胞。通常情况下这很好但如果用户在我的应用程序运行时碰巧点击了工作表,则会抛出COMException请注意,单击的单元格不一定是应用程序正在写入的单元格之一。我尝试了3次重试(见下面的代码),但这没有帮助。

可能导致此错误的原因是什么?我可以做些什么来更好地处理它?<​​/ p>

public bool WriteString(string id, string value)
{
    for (int i = 0; i < RETRY; i++)
    {
        try
        {
            log.Debug(string.Format("XL write '{0}' to '{1}'", value, id));
            Range range = GetCell(id);
            range.Value2 = value;
            return true;
        }
        catch (System.Runtime.InteropServices.COMException x)
        {
            log.Error(x);
            Thread.Sleep(1);
        }
    }
    return false;
}

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_Value2(Object value)
   at Pruef.Net.TgtManager.StdTarget.Excel.ExcelTarget.WriteString(String id, String value)
   at Pruef.Net.TgtManager.StdTarget.Excel.ExcelTarget.WriteDouble(String id, Double value)
   at Pruef.Net.Model.IOManaged.DoOutput(OutputAction action)
   at Pruef.Net.ViewModel.ExecuteViewModel.PerformAction(BaseAction action, Boolean ignoreStop)
   at Pruef.Net.ViewModel.ExecuteViewModel.PerformAction(BaseAction action, Boolean ignoreStop)
   at Pruef.Net.ViewModel.ExecuteViewModel.PerformActions(ObservableCollection`1 actions, Boolean ignoreStop)
   at Pruef.Net.ViewModel.ExecuteViewModel.Execute(UnitOfTest uot)

刚发现这个问题Exception from HRESULT: 0x800A03EC Error。也许'睡得更久'可能会有所帮助......

0 个答案:

没有答案