Пример #1
0
        private void ExecuteNotify(ExcelNotityPropertyContext context)
        {
            if (isDisposed || context.ContextItem.IsDisposed || !context.View.IsRendered)
            {
                return;
            }

            ExcelInterop.Worksheet worksheet = null;
            ExcelInterop.Range     range     = null;
            bool enableEvent = ExcelApplication.Application.EnableEvents;

            try
            {
                worksheet = context.View.FirstOutputCell.Worksheet;
                KeyValuePair <int, int> kvp = context.Param;
                range = worksheet.Cells[context.View.FirstOutputCell.Row + kvp.Key, context.View.FirstOutputCell.Column + kvp.Value];
                if (range != null)
                {
                    object value = context.ContextItem.ResolveBinding();
                    if (value is Enum)
                    {
                        value = ((Enum)value).ToString();
                    }

                    if (!object.Equals(range.Value2, value))
                    {
                        if (enableEvent)
                        {
                            ExcelApplication.Application.EnableEvents = false;
                        }
                        range.Value2 = value;
                        if (context.ContextItem is ExcelContextItemWithFormula)
                        {
                            range.Calculate();
                            ((ExcelContextItemWithFormula)context.ContextItem).UpdateTarget(range.Value2);
                        }
                        context.View.CurrentSelectedCell?.Select();
                    }
                    context.ContextItem.BindingDefinition.DecoratorDefinition?.Resolve(range, context.ContextItem);
                }
                sleepTime = 0;
            }
            catch (COMException comEx)
            {
                waitExcelBusy = true;
                NotifyPropertyChanged(context);
                if (sleepTime < 1000)
                {
                    sleepTime += 10;
                }
            }
            catch (Exception ex)
            {
                string message = $"'ExecuteNotify' failed.{ex.Message}";
                Logger.Instance.LogException(LogType.Error, ex, message);
            }
            finally
            {
                if (worksheet != null)
                {
                    ExcelApplication.ReleaseComObject(worksheet);
                    worksheet = null;
                }
                try
                {
                    if (ExcelApplication.Application.EnableEvents != enableEvent)
                    {
                        ExcelApplication.Application.EnableEvents = enableEvent;
                    }
                }
                catch (COMException comEx)
                { }
            }
            range = null;
        }
Пример #2
0
        public void ClearRange(ExcelInterop.Range from, ExcelInterop.Range to, ExcelInterop.Range with)
        {
            if (from == null)
            {
                return;
            }

            ExcelInterop.Worksheet concernedSheet = null;
            bool isProtected = false;

            try
            {
                concernedSheet = from.Worksheet;

                isProtected = concernedSheet.ProtectContents;
                if (isProtected)
                {
                    concernedSheet.Unprotect(Type.Missing);
                }

                if (to == null)
                {
                    to = concernedSheet.UsedRange;
                }

                from = from.Resize[to.Rows.Count - from.Rows.Count - 1, to.Columns.Count - from.Columns.Count - 1];
                from.Clear();

                if (with != null)
                {
                    ExcelInterop.Interior withInterior = with.Interior;
                    ExcelInterop.Font     withFont     = with.Font;

                    ExcelInterop.Interior interior = from.Interior;
                    ExcelInterop.Font     font     = from.Font;

                    font.Color     = withFont.Color;
                    interior.Color = withInterior.Color;

                    ExcelApplication.ReleaseComObject(interior);
                    ExcelApplication.ReleaseComObject(font);
                    ExcelApplication.ReleaseComObject(withInterior);
                    ExcelApplication.ReleaseComObject(withFont);
                    interior     = null;
                    font         = null;
                    withInterior = null;
                    withFont     = null;
                }
            }
            catch
            {
                if (concernedSheet != null)
                {
                    ExcelApplication.ReleaseComObject(concernedSheet);
                }
            }
            finally
            {
                if (concernedSheet != null && isProtected)
                {
                    ProtectSheet(concernedSheet);
                }
            }
        }