private void ApplyInteriorStyle(ExcelFormat cellFormat, ExcelApp.Interior interior) { if (cellFormat.BackColor.HasValue) { interior.Color = ColorTranslator.ToOle(cellFormat.BackColor.Value); } }
private void HighlightSelection(ExcelInterop.Range selectedCell) { ExcelInterop.Range viewSelectedRange = null; ExcelInterop.Worksheet sheet = (ExcelInterop.Worksheet)RenderedRange.Parent; if (TemplateDefinition.Orientation == Orientation.Vertical) { viewSelectedRange = sheet.Cells[selectedCell.Row, RenderedRange.Column]; viewSelectedRange = viewSelectedRange.Resize[1, RenderedRange.Columns.Count]; currentSelectedRange = viewSelectedRange; } else { viewSelectedRange = sheet.Cells[RenderedRange.Row, selectedCell.Column]; viewSelectedRange = viewSelectedRange.Resize[RenderedRange.Rows.Count, 1]; currentSelectedRange = viewSelectedRange; } for (int i = 1; i <= currentSelectedRange.Cells.Count; i++) { ExcelInterop.Range cell = currentSelectedRange.Cells[1, i]; if (CurrentSelectedCell.Column != cell.Column || CurrentSelectedCell.Row != cell.Row) { ExcelInterop.Interior interior = cell.Interior; try { if (interior.Gradient != null) { currentSelectedRangePattern.Add(null); } else { currentSelectedRangePattern.Add(new SelectionPattern(ref interior)); interior.Pattern = ExcelInterop.XlPattern.xlPatternGray8; interior.PatternColor = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.DimGray); } } catch { } ExcelApplication.ReleaseComObject(interior); interior = null; } else { currentSelectedRangePattern.Add(null); } } // Redraw the borders of the current selection if (((TemplateDefinition)TemplateDefinition).AddBorder) { Renderer.BorderAround(currentSelectedRange, ExcelInterop.XlLineStyle.xlContinuous, ExcelInterop.XlBorderWeight.xlThin, 1); } viewSelectedRange = null; ExcelApplication.ReleaseComObject(sheet); sheet = null; }
public void SetBackColor(Color color) { AssertNotDisposed(); Excel.Range _range = _GetRange(); Excel.Interior _interior = _range.Interior; var oleColor = ColorTranslator.ToOle(color); _interior.Color = oleColor; Marshal.ReleaseComObject(_interior); Marshal.ReleaseComObject(_range); }
public SelectionPattern(ref ExcelInterop.Interior interior) { try { Pattern = (ExcelInterop.XlPattern)interior.Pattern; PatternColor = interior.PatternColor; PatternColorIndex = interior.PatternColorIndex; PatternThemeColor = interior.PatternThemeColor; PatternTintAndShade = interior.PatternTintAndShade; } catch { } }
private void UnhighlightSelection() { // If not the first selection, redraw the borders of the previously selected range if (currentSelectedRange != null) { int cpt = 0; foreach (ExcelInterop.Range cell in currentSelectedRange.Cells) { try { SelectionPattern selectionPattern = currentSelectedRangePattern[cpt++]; if (selectionPattern != null) { ExcelInterop.Interior interior = cell.Interior; cell.Interior.Pattern = selectionPattern.Pattern; if (selectionPattern.PatternColorIndex >= 0) { cell.Interior.PatternColorIndex = selectionPattern.PatternColorIndex; } if (selectionPattern.PatternColor != 0) { cell.Interior.PatternColor = selectionPattern.PatternColor; } if (selectionPattern.PatternThemeColor != 0) { cell.Interior.PatternThemeColor = selectionPattern.PatternThemeColor; } cell.Interior.PatternTintAndShade = selectionPattern.PatternTintAndShade; ExcelApplication.ReleaseComObject(interior); interior = null; } } catch { } } currentSelectedRangePattern.Clear(); currentSelectedRange = null; } }
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); } } }